Java Interface Factory Schema understanding

Source: Internet
Author: User

As a practical Java development experience less than a year I, the first time to write a blog, afraid to make their own fallacy to the public, fraught, but on second thought, if you can have peers to judge, will their insights described, then this article even throw quoted Jade.

Recently, in the last section of the Java programming thought-related interface, the author proposes a factory design pattern to address--- generating objects that implement an interface . To tell you the truth, I have almost no idea of the design pattern, and it's too easy to think about it, and it's too simple to write an example in my mind:

1 //Gas2 Interfaceair{3   voidShow ();4 }5 6 //Wu Empty7 classWuKongImplementsair{8 @Override9    Public voidShow () {TenSystem.out.println ("Wukong: Turtle Qigong"); One   } A } -  -  Public classTest { the    Public Static voidMain (string[] args) { -Air wk =NewWuKong (); - wk.show (); -   } +}

Reminiscent of the factory design patterns mentioned in the book, I would like to think: what is the design of this model? Under what circumstances can it play a role in the pattern? How to write this pattern, but not the pure backrest?

1. The explanation in the book is not very clear, backstage on the internet to find an easy-to-understand explanation: The factory design pattern is to separate the creation and use of objects. The above example creates a Wukong object, randomly calling show (), and this pattern is in the creation and use of the Wukong object in these two links to make a fuss. The idea figured out, super simple. The original process is: Generate Object A-->A.M (), and evolve to generate a factory class B,b in the method N (), you can generate object A, then A.M. (). Look at the code:

Interfaceair{voidshow ();} //Wu EmptyclassWuKongImplementsair{@Override Public voidShow () {System.out.println ("Wukong: The Turtle-Pie qigong"); } }//Goku factory class, the purpose is simple, in order to create Goku objectclasswukongfactory{ PublicAir Getair () {return NewWuKong (); } } Public classTest { Public Static voidMain (string[] args) {//Air wk = new WuKong ();//wk.show ();       /*** With the introduction of the factory class, call the Show method also made some adjustments, first create the Wukong factory class object factory, * The emphasis is on, new WuKong () operation is encapsulated in the Getair (). * Imagine a scenario like this: the main () method we do not change it, we can add ourselves to modify other classes, * in this case: we can modify the Getair () method to return any object containing the show () method (that is, to implement the air interface), * so that the next The code for the polygon outputs different results.        Obviously, the two lines of code that are annotated do not play this pattern. */Wukongfactory Factory=Newwukongfactory ();          Factory.getair (). Show (); } }

Here, let's verify that, in the case of the main () method, we add an eight-ring class that implements the air interface to see the effect:

 //  Goku factory class, purpose is simple, just to create Goku object  class   wukongfactory{  Public   Air Getair () { //  Note: This is no longer new WuKong ()  return  new   Bajie (); }}  //  Eight commandments   Class  Bajie implements   air{@Override public  void      show () {System.out.println ( "Eight commandments: Pig's fart" 

At this point Factory.getair () created the eight-ring object, resulting in different results. A little bit of optimization: for wukongfactory This Getair () method, is actually the core method of this pattern, it is to create an instance of the object that you want to decouple, as in the example in the book, it can be drawn out, encapsulated into an interface, when you want to create an Goku or an eight-ring object, To implement this interface, override the method to create the object you want.

Interface factory{    Air getair ();} class Implements factory{    @Override    public  Air getair () {        returnnew WuKong ();    }} class Implements factory{    public  Air getair () {        returnnew  Bajie ();    }}

  

Respect so far, the factory method design mode of understanding, I can only talk so much, because the development experience is too thin, and I am also in the process of learning Java, so I can not cite the actual project in the use of this model of the live case (useful to this model of friends hope to share). However, after the understanding of this model, it is relatively simple, and the bend around is just that. At the same time, I continue to read this book, I will also write more blog, sorting out the experience, and share with you.

  

Java Interface Factory Schema understanding

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.