Java Common design pattern learning experience

Source: Internet
Author: User

Learn from: http://shenzhenchufa.blog.51cto.com/730213/161581

Code from: http://shenzhenchufa.blog.51cto.com/730213/161581

Thank the blogger: Shenzhenchufa.

While studying, I write some of my own experience. Intentional infringement of copyright, for their own learning.

-------------------------strategy (Policy mode)-------------------------
We have a design principle:
Find the similarities in the application, and not easily change things, extract them into the abstract class, let subclasses inherit them;
Find out where changes may be needed in your application, separate them, and don't mix with code that doesn't need to change. -->important.

Now, in order to separate the "changed and unchanged parts", we are going to set up two sets of classes (completely away from the Duck Class), one is "fly" related and the other is "quack" related, each group of classes will implement their respective actions. For example, we may have a class that implements "quack", another class that implements "squeaking", and a class that implements "quiet." First write two interfaces. Flybehavior (flight behavior) and Quackbehavior (called behavior).
 Public Interface flybehavior{     publicvoid  fly ();        Public Interface quackbehavior{     publicvoid  quack ();}

We are defining some specific implementations for Flybehavior.

 Public class Implements flybehavior{    publicvoid  fly () {     //  All winged ducks have been flown.     }}publicclassimplements  flybehavior{       Public void   Fly () {      // do Nothing, not fly     }}   

Several specific implementations for Quackbehavior.

 Public classQuackImplementsquackbehavior{ Public voidQuack () {//to achieve a quack duck  }}  Public classSqueakImplementsquackbehavior{ Public voidQuack () {//implement a squeaky duck  }}  Public classMutequackImplementsquackbehavior{ Public voidQuack () {//don't do anything, don't call  }}

Reviews One:
Such a design would allow the flight and the action of the quack to be reused by other objects, since these actions have nothing to do with the duck class. And we're adding some new

Behavior, does not affect the existing behavior class, and does not affect the "use" to the flight behavior of the Duck class. Finally, let's see how duck is designed.
 Public classduck{--------->in an abstract class, each interface is declared and the corresponding method is defined for each interface. Flybehavior Flybehavior;//InterfaceQuackbehavior Quackbehavior;//Interface        PublicDuck () {} Public Abstract voiddisplay ();  Public voidswim () {//The realization of swimming behavior        }        Public voidPerformfly () {flybehavior.fly (); -because it is an interface, the corresponding method is called according to the way the inheriting class implements it. }      Public voidPerformquack () {quackbehavior.quack ();(); } }

See how Mallardduck is implemented.
-----> through the construction method, generate ' fly ', ' call ' the concrete implementation of the class instance, thus specifying the ' fly ', ' called ' specific properties

 Public class extends duck{       public  mallardduck {               new  flywithwings ();         New Quack ();        // because Mallardduck inherits the duck, all have flybehavior and Quackbehavior instance variables}     Public void display () {     // implementation    }}

This is enough to be able to fly, but also can be called, at the same time to show their own color.

Such a design we can see is the Flybehavior, quackbehavior the instantiation of the sub-class is written. We can also make a dynamic decision.
We just need to add two methods to the duck.The difference between assigning a property to a setter in a constructor method and using a property:The property is assigned in the construction method: fixed, immutable;with the property setter, can be instantiated after the object, dynamic changes, more flexible.
 public  class   duck{Flybehavior Flybehavior;  //       interface  Quackbehavior Quackbehavior; //       interface  public  void   Setflybehavior (Flybehavior flybehavior) { th     is . Flybehavior = Flybehavior;  public  void   Setquackbehavior (Quackbehavior quackbehavior { this . Quackbehavio     R= Quackbehavior; } }

Strategy mode: I have several uncertain behavior, I do not know how the behavior should be specific performance, when I received a specific behavior, I based on the specific behavior of performance.

Java Common design pattern learning experience

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.