Policy mode: defined
Algorithm , Respectively
Encapsulation So that they can
Mutual replacement This mode changes the algorithm
Independent For customers who use algorithms. Encapsulation: encapsulate behavior with Interfaces. We can extract frequently changed parts from the current class and encapsulate them separately using interfaces. Replace each other: We have encapsulated interfaces and implemented classes to change algorithms by specifying different interfaces. Mind Map
I will explain the process of this mind map: 1. Joe played a very successful game simulating ducks. A super-class duck is designed, and various ducks are allowed to inherit the class. 2. The customer later proposed that the duck be capable of flying. So Joe adds a fly () method to the superclass, so that the following sub-classes have flight behaviors. The problem is: 1> there was actually a rubber duck in the duck subclass, And the Rubber Duck would not fly. -- Joe uses the overload method to set the fly () method of the Rubber Duck to null. 2> overwrite fly (). We can see that there is no fly () in the Rubber Duck.
Code If we add other ducks that won't fly in the future, do I have to do this? -- So
Code already exists Now! 3. We know there is a problem with the method above 2, so Joe wants to make duck an interface, so that each subclass must implement the method in duck. This ensures that each duck can add behavior based on its own needs. The problem is: the product is often updated, and the specifications are constantly changing. As a result, whenever a new duck exists, Joe is forced to check whether the subclass covers the fly () method. -- When you modify an action, you must track down and modify it in each class that defines this behavior. 4. Based on the above questions, Joe thought of extracting the changed parts from the unchangeable positions. For example, we have made a separate interface flybehavior for Fly () behavior. If the duck wants the flight function, let the duck implement the flybehavior. 5. Further study: we want the duck to have different flight functions for different flight actions. The duck class can only implement one behavior for ducks. So Joe thought of combining the defense. When the duck needs other flight function requirements, we can use setbehavior () to define the flight mode. Code
<? Phpinterface flybehavior {public function fly ();} class flywithwings implements flybehavior {public function fly () {echo "fly with wings \ n ";}} class flywithno implements flybehavior {public function fly () {echo "fly with no wings \ n ";}}
Class duck {private $ _ flybehavior; public function extends mfly () {$ this-> _ flybehavior-> fly ();} public function setflybehavior (flybehavior $ behavior) {$ this-> _ flybehavior = $ behavior;} class rubberduck extends duck {}
// Test case $ duck = new rubberduck ();/* Fly the duck with wings */$ duck-> setflybehavior (New flywithwings ()); $ duck-> mongomfly ();/* Fly the duck without wings */$ duck-> setflybehavior (New flywithno (); $ duck-> mongomfly ();
In summary, our design principles in development are as follows: 1. identify the potential changes in the application and separate them from those that do not need to be changed. 2. for interface programming, not for implementation programming; 3. multi-Purpose Combination and less inheritance; References: head first design model