Java Factory mode (big talk design mode)

Source: Internet
Author: User

In the previous article about the simple factory model, see link: http://www.cnblogs.com/zhuxiansheng/p/7873161.html

The Simple factory model decouples the client and implementation dependencies, but if there is another extension implementation, we have to modify the selection branch in the factory to add case and then add a new implementation, which violates the open-close principle. How to solve this problem, the factory model gives a very good answer.

Liar design mode-Class diagram

The example code in the liar design pattern is as follows:

/**  *   */  Public Interface ifactory {    public  Leifeng Createleifeng ();}

/***/Publicclass  Leifeng    {publicvoid Sweep () {        System.out.println ("sweeping");    }      Public void Buyrice () {        System.out.println ("Buy rice");    }      Public void Wash () {        System.out.println ("wash the dishes");}    }

/***/Publicclassextends  leifeng{}

/***/Publicclassextends  leifeng{}

/***/Publicclassimplements  ifactory{    @Override      public Leifeng Createleifeng () {        returnnew  Undergraduate ( );    }}

/***/Publicclassimplements  ifactory{    @Override      public Leifeng Createleifeng () {        returnnew  Volunteer ();}    } 

/***/Publicclass  Test    {publicstatic  void  Main (string[] args) {        new  volunteerfactory ();         = Volunteerfactory.createleifeng ();        Leifeng.sweep ();        Leifeng.buyrice ();        Leifeng.wash ();    }}

If we add a different role, we just need to add the corresponding factory and role domain can be implemented without modifying the original code to add a new implementation, the factory conforms to the open-close principle, but for the client we still have to modify which specific factory to create the code.

Recall the simple Factory mode, if we expand the N-side of M, we only need to implement the computing factory interface, and then add the specific calculation class, the implementation of the extension, which is the author of the Simple Factory and factory model understanding, I hope to help learn the factory model of children's shoes!

Java Factory mode (big talk design mode)

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.