Chapter VI decorator mode
The behavior of class or its algorithm can be changed at run time, which belongs to behavioral mode
Application: different implementation classes have different functions, one policy structure, one context class (interface parameters) and n interface implementation classes
public class StrategyPatternDemo { public static void main(String[] args) { Context context = new Context(new OperationAdd()); System.out.println("10 + 5 = " + context.executeStrategy(10, 5)); context = new Context(new OperationSubtract()); System.out.println("10 - 5 = " + context.executeStrategy(10, 5)); context = new Context(new OperationMultiply()); System.out.println("10 * 5 = " + context.executeStrategy(10, 5)); } }
Chapter VII Agency Model
Do some control when accessing a class, and realize the access and control of the class through the agent
Implement the combination with the proxy class method, assign a value to null, and then execute
It includes: interface, implementation class and proxy class. During testing, the parent class reference (Interface) points to the child class object
public class ProxyImage implements Image{ private RealImage realImage; private String fileName; public ProxyImage(String fileName){ this.fileName = fileName; } @Override public void display() { if(realImage == null){ realImage = new RealImage(fileName); } realImage.display(); } }
Chapter VIII factory mode
Create the interface of the object and let its subclass decide which factory class to instantiate. The factory mode delays the creation process to the subclass.
The instance factory class generates the object of the corresponding entity class according to the passed parameters
Returns the specified subclass using the factory class
Purpose: logging, database access
public class ShapeFactory { //use getShape Method to get an object of shape type public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } }
call
ShapeFactory shapeFactory = new ShapeFactory(); //obtain Circle Object and call its draw method Shape shape1 = shapeFactory.getShape("CIRCLE"); //call Circle of draw method shape1.draw();
Difference from the simple factory mode: the simple factory mode is to make selection judgment in the test class, while the factory mode is to write the factory class and return the specified object in the factory class
Chapter 9 prototype model
It is used to create duplicate objects while ensuring performance. It belongs to the creation mode
Implement the clonable interface, and its clone method is used to create a clone of the current object
implements Cloneable public Object clone() { Object clone = null; try { clone = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; }
According to the id of the key string, store it into the hashmap method, take values from the map, and return the method of the corresponding object
Chapter 10 template method mode
An abstract class exposes a method / template that defines how its methods are executed. Its subclasses can override the method implementation as needed, but the call will be made in the way defined in the abstract class
Scenario: there are methods common to multiple subclasses, and the logic is the same
Note: the general template method adds the final keyword to prevent malicious operations
Defect: multiple subclasses need to define their own subclass implementation, which is complex
public abstract class Game { abstract void initialize(); abstract void startPlay(); abstract void endPlay(); //Template public final void play(){ //Initialize game initialize(); //Start the game startPlay(); //End the game endPlay(); } }