instructions
This article only describes scenarios where policy enumerations can be used. Provide additional implementation ideas.
The factory pattern
The normal factory pattern has a basic abstract class or interface followed by a concrete implementation class. And a factory class returns the implementation class. Reference:The factory pattern
transform
Here we modify the reference code:
- Java, Rectangle. Java, Square. Java, Circle.
- In the shapeFactory.java class, there is a type: “shapeType”. This can be replaced with an enumeration. So let’s create a new enumeration, shapeTypeEn.java. As follows:
- Java: shapeFactory. Java: shapeTypeen. Java: valueOf: shapeTypeen. Java: getShape The transformation from factory pattern to policy factory can be implemented.
public enum ShapeTypeEnum {
CIRCLE(new Circle()),
RECTANGLE(new Rectangle()),
SQUARE(new Square()),
;
private Shape shape;
ShapeTypeEnum(Shape shape) {
this.shape = shape;
}
public Shape getShape(a) {
returnshape; }}Copy the code
- Where used, the code is changed to:
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
// Get the Circle object and call its draw method
Shape shape1 = shapeFactory.getShape("CIRCLE");
// Call Circle's draw method
shape1.draw();
// Get the Rectangle object and call its draw method
Shape shape2 = shapeFactory.getShape("RECTANGLE");
// Call the Rectangle draw method
shape2.draw();
// Get the Square object and call its draw method
Shape shape3 = shapeFactory.getShape("SQUARE");
// Call Square's draw method
shape3.draw();
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
// There may be null pointer exceptions that can further optimize the ShapeTypeEnum class
ShapeTypeEnum.valueOf("CIRCLE").getShape().draw();
ShapeTypeEnum.valueOf("RECTANGLE").getShape().draw();
ShapeTypeEnum.valueOf("SQUARE").getShape().draw(); }}Copy the code
The final result is consistent with the original.
advantages
- If shapeType is increased, we can immediately see that we need to add a corresponding implementation by modifying the enumeration.
- A natural singleton implementation. If you want to implement Dolly, you can also do it by modifying ShapeTypeEnum.
disadvantages
The initial amount of code will be a bit large, but it is basically framework code
To optimize the
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public enum ShapeTypeEnum {
DEFAULT("".new Default()) {
@Override
public Shape getShapeImpl(a) {
return new Default();
}
},
CIRCLE("CIRCLE".new Circle()) {
@Override
public Shape getShapeImpl(a) {
return new Circle();
}
},
RECTANGLE("RECTANGLE".new Rectangle()) {
@Override
public Shape getShapeImpl(a) {
return new Rectangle();
}
},
SQUARE("SQUARE".new Square()) {
@Override
public Shape getShapeImpl(a) {
return newSquare(); }};<br/> **@return "com.csdn.enums4.Shape"
*/
public abstract Shape getShapeImpl(a);
/**
* 功能描述: 单例 <br/>
*
* @paramType type *@return "com.csdn.enums4.Shape"
*/
public static Shape getSingletonShape(String type) {
return indexOf(type).getShape();
}
/** * <br/> **@paramType type *@return "com.csdn.enums4.Shape"
*/
public static Shape getNonSingletonShape(String type) {
return indexOf(type).getShapeImpl();
}
private final static Map<String, ShapeTypeEnum> map = Stream.of(values()).collect(Collectors.toMap(ShapeTypeEnum::getType, e -> e));
public static ShapeTypeEnum indexOf(String type) {
// Avoid null Pointers
return Optional.ofNullable(map.get(type)).orElse(DEFAULT);
}
private final String type;
private final Shape shape;
ShapeTypeEnum(String type, Shape shape) {
this.type = type;
this.shape = shape;
}
public String getType(a) {
return type;
}
public Shape getShape(a) {
returnshape; }}Copy the code
The output of each draw implementation is appended to the output of the current object this
@Override
public void draw(a) {
System.out.println("Inside Default::draw() method." + this);
}
Copy the code
use
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
// Get the Circle object and call its draw method
Shape shape1 = shapeFactory.getShape("CIRCLE");
// Call Circle's draw method
shape1.draw();
// Get the Rectangle object and call its draw method
Shape shape2 = shapeFactory.getShape("RECTANGLE");
// Call the Rectangle draw method
shape2.draw();
// Get the Square object and call its draw method
Shape shape3 = shapeFactory.getShape("SQUARE");
// Call Square's draw method
shape3.draw();
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * singleton * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
ShapeTypeEnum.getSingletonShape("CIRCLE").draw();
ShapeTypeEnum.getSingletonShape("CIRCLE").draw();
ShapeTypeEnum.getSingletonShape("RECTANGLE").draw();
ShapeTypeEnum.getSingletonShape("RECTANGLE").draw();
ShapeTypeEnum.getSingletonShape("SQUARE").draw();
ShapeTypeEnum.getSingletonShape("SQUARE").draw();
System.out.println("* * * * * * * * * * * * * * * * * * * * * * the singleton * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
ShapeTypeEnum.getNonSingletonShape("CIRCLE").draw();
ShapeTypeEnum.getNonSingletonShape("CIRCLE").draw();
ShapeTypeEnum.getNonSingletonShape("RECTANGLE").draw();
ShapeTypeEnum.getNonSingletonShape("RECTANGLE").draw();
ShapeTypeEnum.getNonSingletonShape("SQUARE").draw();
ShapeTypeEnum.getNonSingletonShape("SQUARE").draw(); }}Copy the code
The results of
Inside Circle::draw() method.com.csdn.enums4.Circle@677327b6 Inside Rectangle::draw() method.com.csdn.enums4.Rectangle@14ae5a5 Inside Square::draw() method.com.csdn.enums4.Square@7f31245a * * * * * * * * * * * * * * * * * * * * * * * * * singleton * * * * * * * * * * * * * * * * * * * * * * * * * * * * Inside Circle: : the draw () method.com.csdn.enums4.Circle@5b480cf9 Inside Circle::draw() method.com.csdn.enums4.Circle@5b480cf9 Inside Rectangle::draw() method.com.csdn.enums4.Rectangle@6f496d9f Inside Rectangle::draw() method.com.csdn.enums4.Rectangle@6f496d9f Inside Square::draw() method.com.csdn.enums4.Square@723279cf Inside Square::draw() method.com.csdn.enums4.Square@723279cf * * * * * * * * * * * * * * * * * * * * * * the singleton * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Inside Circle: : the draw () method.com.csdn.enums4.Circle@10f87f48 Inside Circle::draw() method.com.csdn.enums4.Circle@b4c966a Inside Rectangle::draw() method.com.csdn.enums4.Rectangle@2f4d3709 Inside Rectangle::draw() method.com.csdn.enums4.Rectangle@4e50df2e Inside Square::draw() method.com.csdn.enums4.Square@1d81eb93 Inside Square::draw() method.com.csdn.enums4.Square@7291c18fCopy the code