Design Patterns Study Notes (four) - type of model (Behavioral Pattern)
Others
2019-09-02 09:46:36
views: null
Behavioral patterns (Behavioral Pattern)
- Duty chain (Chain of Responsibility)
- A plurality of object a chance to handle the request, in order to avoid the coupling between the sender and receiver of the request, the object will be connected into a chain, and pass the request along the chain until an object handles up his
- Typical applications: servlet filter
- Command Mode (Command)
- Interpreter pattern (Interpreter)
- Interpreter pattern is a kind of behavior.
- After a given language, the interpreter mode can be defined one of its grammar representation, while providing an interpreter.
- The client can use the interpreter to interpret this statement sentence.
- Typical applications: Parsing four mixed computing, parse xml document
- Iterator pattern (Iterator)
- A method for providing access inside a sequential polymerization object various elements, without exposing the object representation
- Typical applications: The Java Iterator
- Intermediary model (Mediator)
- Intermediary model, also known mediator model, it is an object behavioral patterns.
- Use a mediation object that encapsulates a set of objects interact intermediary keeping objects explicitly referring to each other, so that it loosely coupled, and can be changed independently of the interaction between them.
- Typical applications: Multiplayer chat rooms, MVC architecture (controller is an intermediary), and references to control multiple interface GUI development
- Four roles :( colleagues in the incoming class method intermediary objects)
- Mediator: Mediator abstract
- ConcreteMediator: specific mediator
- Colleague: abstract class colleagues
- ConcreteColleague: Specific class colleagues
- Memo mode (Memento)
- Memo object is further used to store a snapshot of the internal state of an object (Snapshot) object.
- Is intended in the memo mode without destroying the package, it will capture the state of an object, and external of, stored, so that the object can be restored to the state stored in the future when appropriate.
- typical application:
- 0, Session and Cookie usage is memo mode.
- 1, regret it later.
- 2, the archive when playing games.
- 3, Windows in the ctrl + z.
- 4, IE is retracted.
- 5, transaction management database.
- Mode observer (Observer)
- Four roles :( observers hold the target object and register itself with the goal at initialization time. When the target changes to inform the viewer)
- Subject: goal
- ConcreteSubject: Target
- Observer: observer
- ConcreteObserver: specific observer
- Java native interfaces: Observable, Observer
- vs intermediary model:
- Mediator (mediator) to emphasize that the interaction between colleagues (colleague) class
- The observer (observer) in the target class (subject) stressed that the unified communications observer goal after change
- Mode state (State)
- Strategy Mode (Strategy)
- Define a series of algorithms, each algorithm is a package together, and so that they can be replaced with each other.
- Strategy mode allows algorithm independent of changes in the use of its customers, also known as policy mode (Policy).
- Strategy Mode is an object behavioral patterns.
- vs Status Mode:
- Strategy mode specific strategies to expose the class, the caller needs to understand the specific differences at each policy to correct.
- Mode state changes state is determined by its internal condition change, regardless of the outside
- State object is a class member variable environment, policy environment parameter object is a method of class
- Three roles:
- Context: Environmental
- Strategy: Strategy abstract class
- ConcreteStrategy: Specific Strategy
- Template Method pattern (Template Method)
- A public abstract class defines the implementation of its method of fashion / template, which can be implemented by subclasses need to override the method, but the call will be defined in the abstract class way
- Typical examples: servlet in service () doGet () doPost () method
- Visitor pattern (Visitor)
- Five roles:
- Visitors abstract: for each specific element class declaration object structure of an access operation.
- Specific Visitors: access method to achieve each specific element.
- Abstract element: accept method declarations, receiving the visitor access operation, usually abstract classes and interfaces.
- Specific elements: Implement accept method, call the access method corresponding to the visitors.
- Object structure: storing elements of the object and provide a traversal method may be implemented using a combination of mode
- A data structure of the separation operation of the design pattern data
- Java-like overloaded, a different approach to the Senate is a different way to achieve
interface Visitor {
void visit(Games games);
void visit(Photos photos);
}
interface Computer {
void accept(Visitor visitor);
}
class ZhangSan implements Visitor {
public void visit(Games games) {
games.play();
}
public void visit(Photos photos) {
photos.watch();
}
}
class LiSi implements Visitor {
public void visit(Games games) {
games.play();
}
public void visit(Photos photos) {
photos.watch();
}
}
class Games implements Computer {
public void accept(Visitor visitor) {
visitor.visit(this);
}
public void play() {
System.out.println("play lol");
}
}
class Photos implements Computer {
public void accept(Visitor visitor) {
visitor.visit(this);
}
public void watch() {
System.out.println("watch scenery photo");
}
}
class ObjectStructure {
private List<Computer> computers = new ArrayList<Computer>();
public void action(Visitor visitor) {
computers.forEach(c -> {
c.accept(visitor);
});
}
public void add(Computer computer) {
computers.add(computer);
}
public static void main(String[] args) {
ObjectStructure os = new ObjectStructure();
os.add(new Games());
os.add(new Photos());
// 创建一个访问者
Visitor visitor = new ZhangSan();
os.action(visitor);
}
}
Origin www.cnblogs.com/kintanx/p/11444399.html