first, The Strategy design mode
Creating a method that behaves differently depending on the object being passed is called a policy design pattern , which contains a fixed portion of the algorithm to be executed, and the "policy" contains the changed parts . a policy is a parameter object that is passed in, which contains the code to Execute.
This design pattern encapsulates the algorithm separately, and then passes it as a parameter to the method so that the method produces different behavior, and the different algorithms can be substituted (as with the Method's different arguments). As we can see, there are three parts in the policy design pattern: policy, Policy reference, method of receiving policy references (this is the term that the author gives himself according to the understanding, and there is no query for these terms). The following is an example of a brief introduction to the policy design pattern:
packagecom.tongye.strategy;/*base class, which can be either a normal class or an interface*/Abstract classshape{ public Abstract voidOutputshape ();}/*Strategy*/classCircleextendsshape{ public voidoutputshape () {System.out.println ("this is a circle"); }}/*Strategy*/classSquareextendsshape{ public voidoutputshape () {System.out.println ("this is a square"); }}/*Strategy*/ classTriangleextendsshape{ public voidoutputshape () {System.out.println ("this is a triangle"); }} public classStrategy {/*the Strategy.strategic () method receives a reference to a shape type as a parameter * This reference can be any type of shape, such as circle, Square, Triangle * Depending on the parameters passed, the strategic method Have a different behavior slightly **/ public Static voidStrategic (Shape S) {//the method that receives the policy reference, s is the policy referenceS.outputshape ();//part of a method that is inherently unchanged } public Static voidmain (string[] Args) {strategic (NewCircle ());//the New Circle () object is used as a parameter to the strategic () method, where the Circle () object is a strategy, which is used for upward transformationStrategic (NewSquare ());//Policy Square () ObjectStrategic (NewTriangle ());//policy Triangle () Object }}/** Output: * This was a circle * this was a square * this is a triangle **/
Through a small piece of code above, we know that the strategic method can produce different behaviors depending on the parameters being passed, and these behaviors are encapsulated in different strategies, which is the policy design Pattern. By encapsulating different behaviors into different strategies, we can change the policy at any time to achieve different behavior, which makes the program more flexible and easier to maintain.
second, the adapter mode
As a simple example, when we charge the phone, because the phone charging port is 5V, and the socket provides 220V ac, so we usually need to use a charger to convert 220V AC to a mobile phone to charge the 5V direct current, This charger is an adapter.
similarly, When writing Java programs, we may encounter a situation where we need a class A to implement interface b, but Class A does not implement all of the methods in interface b, and Class A cannot be changed, so we can create a class C that inherits Class A and implements interface b, which is an adapter class C. the code in the adapter will accept the interface you have and produce the interface you need . There are two adapter Modes: class Adapter mode and Object Adapter Mode. Here are two examples for a quick demonstration:
1. Class Adapter Mode:
/*****************************/ packagecom.tongye.adapters; public InterfaceTargetinterface {voidmethod1 (); voidmethod2 ();}/*****************************/ packagecom.tongye.adapters;/*Source class, We need this class to implement interface targetinterface, but this class cannot be changed * There is no relationship between the source class and the interface **/classbeadapted{ public voidmethod1 () {System.out.println ("method1"); }}/*This is the adapter that accepts the interface that is already in beadapted and produces the interface that we need * Method1 () method inherits from the Beadapted class (that is, the existing interface) and does not need to be declared * Here the adapter acts as a relationship between the source class and the interface, similar to Implements **/classAdapterextendsbeadaptedImplementstargetinterface{ public voidMethod2 () {//the Method2 () method cannot be obtained through inheritance, so it needs to declare itself, which is the interface that the adapter generates for USSystem.out.println ("method2"); }} public classClassadapter { public Static voidmain (string[] Args) {Adapter adapt=NewAdapter (); Adapt.method1 (); ADAPT.METHOD2 (); }}
Adapter and beadapted are inheritance relationships, that is, class adapter Mode.
2. Object Adapter Mode:
/*****************************/ packagecom.tongye.adapters; public InterfaceTargetinterface {voidmethod1 (); voidmethod2 ();}/*****************************/ packagecom.tongye.adapters;/*Source class, We need this class to implement interface targetinterface, but this class cannot be changed * There is no relationship between the source class and the interface **/classbeadapted{ public voidmethod1 () {System.out.println ("method1"); }}/*This is the adapter that accepts the existing interface and produces the interface we need*/classAdapterImplementstargetinterface{Private StaticBeadapted adapted;//declares a Beadapted object reference publicAdapter (beadapted Adapted) {//constructs a method that receives a beadapted reference as a parameter this. adapted =adapted; } public voidMethod1 () {//method1 () in the source class beadapted, which is directly delegated this. Adapted.method1 (); } public voidMethod2 () {//the Method2 () method cannot be obtained by inheritance and requires its own declarationSystem.out.println ("method2"); }}
Adapter and beadapted are delegate relationships, that is, the object adapter Pattern.
By using adapter mode, you can greatly increase the flexibility of your program and the repeatability of your Code.
Note: the author in the first time in the book to see the Strategy design mode and adapter mode, and did not understand, feel foggy, so went online to find someone Else's experience, one blogger (java_my_life) wrote the blog is very simple and easy to understand, speaking is also very comprehensive, I just read his blog to feel the epiphany, here paste the Blogger's two blog address:
Http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html
Http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html
Java Learning Notes--strategy design mode and adapter mode