Design Patterns Study Notes (four) - type of model (Behavioral Pattern)

Behavioral patterns (Behavioral Pattern)

  1. 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
  2. Command Mode (Command)
    • Command Mode is an object behavioral pattern, which is an alias for the action (Action) mode or transaction (Transaction) mode
    • It encapsulates a request for an object, so that we can use different clients request for parameterization;
    • A request queue or log requests, and support undoable operations.
    • The nature of the command mode command is encapsulated, the responsibility to responsibility for issuing commands and instructions separated
    • Five roles:
      • Command: Command abstract class
      • ConcreteCommand: concrete command classes: receiver Receiver holds a reference to bind to a specific operation
      • Invoker: caller
      • Receiver: command receiver
      • Client: client class
      //抽象命令
      public interface Command {
       void execute();
      }
      //具体命令:开灯
      public class LightOnCommand implements Command {
       private Light light;
       public LightOnCommand(Light light) {
           this.light = light;
       }
       public void execute() {
           light.lightOn();
       }
      }
      //具体命令:关灯
      public class LightOffCommand implements Command {
       private Light light;
       public LightOffCommand(Light light) {
           this.light = light;
       }
       public void execute() {
           light.lightOff();
       }
      }
      //调用者Invoker
      public class XiaoAi {
       private Command command;
       public void setCommand(Command command) {
           this.command = command;
       }
       public void doCommand() {
           command.execute();
       }
      }
      //命令接收者Receiver
      public class Light {
       public void lightOn() {
           System.out.println("灯打开了!!");
       }
       public void lightOff() {
           System.out.println("灯关上了!!");
       }
      }
      //客户类
      public class Client {
       public static void main(String[] args) {
           XiaoAi xiaoAi = new XiaoAi();
           Light light = new Light();
           System.out.println("帮我把灯开一下!");
           LightOnCommand lightOnCommand = new LightOnCommand(light);
           xiaoAi.setCommand(lightOnCommand);
           xiaoAi.doCommand();
      
           System.out.println("帮我关一下灯!");
           LightOffCommand lightOffCommand = new LightOffCommand(light);
           xiaoAi.setCommand(lightOffCommand);
           xiaoAi.doCommand();
       }
      }
  3. 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
  4. 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
  5. 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
  6. 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.
  7. 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
  8. Mode state (State)
    • Alias ​​state objects (Objects for States), the state of the object model is a behavioral pattern
    • Allows an object to change its behavior when its internal state is changed, the object will appear to change its class.
    • The key state model is the introduction of a special abstract class to represent an object's state, we call this class abstract class state, and each state specific object classes inherit this class and implements different states in different specific classes behavioral state, including transitions between various states
    • Three roles:
      • Context: Environmental
      • State: state abstract class
      • ConcreteState: Specific Status Class
      public class Context {
       private State state;
       public void setState(State state) {
           this.state = state;
       }
       public void request(String sampleParameter) {
           state.handle(sampleParameter);
       }
      }
      public interface State {
       public void handle(String sampleParameter);
      }
      public class ConcreteStateA implements State {
       public void handle(String sampleParameter) {
           System.out.println("ConcreteStateA handle :" + sampleParameter);
       }
      }
      public class ConcreteStateB implements State {
       public void handle(String sampleParameter) {
           System.out.println("ConcreteStateB handle :" + sampleParameter);
       }
      }
      public class Client {
       public static void main(String[] args){
           State state = new ConcreteStateB();
           Context context = new Context();
           context.setState(state);
           context.request("test");
       }
      }
  9. 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
  10. 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
  11. 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);
    
        }
    }

Guess you like

Origin www.cnblogs.com/kintanx/p/11444399.html