(i) Factory mode-code implementation

Source: Internet
Author: User

The factory model is divided into two types: 1, simple Factory mode 2, Factory mode 3, abstract Factory mode

Entity class inheritance diagram:

The entity class code that needs to be constructed in the example is omitted

1, Simple Factory mode:

Concept: Static, class that encapsulates the generated object

Usage Scenario: Remove the new operation from the code and unify the entry of the generated object

Benefit: When you replace an object, you only need to modify the code in the factory class to separate the instantiation code of the class from the logical code.

Factory class

 Package note.com.factory; Import note.com.bean.*;  Public class simplefactory {    publicstatic  A getA1 () {        returnNew  A1 ();        }          Public Static A getA2 () {        returnnew  A2 ();        }}

Test class:

 Package note.com.factory; Import Note.com.bean.Bean;  Public class simplefactorytest {    publicstaticvoid  main (string[] args) {        = simplefactory.geta1 ();         = simplefactory.geta2 ();    }}

2, Factory mode

Concept: Defines an interface for creating objects, but subclasses decide which class to instantiate, and the factory method causes the class to defer instantiation to the subclass (and the subclass produces the same type of object)

Usage Scenario: When the object that needs new has subclasses, the factory class is abstracted, providing a corresponding factory implementation class that is built for different target objects

Benefits: With a simple factory class, a variety of target objects should be carried out.

Features: Usually there is only one entry, and each factory produces only one type of object. In this case, only the subclass of a is returned, and for this feature, the abstract class is better able to control the unified portal as a factory base class (where unified initialization logic can be added).

Factory abstract class

 Package note.com.factory; Import Note.com.bean.Bean;  Public Abstract class afactory {    public  A geta () {        return  Getreala ();    }         public  abstract  A Getreala ();    }

Factory sub-Class 1

 Package note.com.factory; Import note.com.bean.A1; Import Note.com.bean.Bean;  Public  class extends afactory{    @Override    public  A Getreala () {        return  New  A1 ();    }}

Factory sub-Class 2

 Package note.com.factory; Import note.com.bean.A2; Import Note.com.bean.Bean;  Public  class extends afactory{    @Override    public  A Getreala () {        return  New  A2 ();    }}

Test Class A factory produces objects of different implementations of the same type

 Packagenote.com.factory;ImportNote.com.bean.a; Public classAfactorytest { Public StaticA Geta (Afactory factory) {returnFactory.geta (); } Public Static voidMain (string[] args) {afactory factory1=NewAFactory1 (); Afactory Factory2=NewAFactory1 (); A A=NULL; A=Afactorytest.geta (Factory1); A=Afactorytest.geta (Factory2);}}

3, Abstract Factory mode

Concept: Provides an interface for creating a family of related or dependent objects without explicitly specifying a specific class.

Features: Compared to the factory model, there can be more than one entry, and there are various types of returns. The following example returns two types, B, which are not unique to the abstract class, so use an interface to implement

Factory interface

 Package note.com.factory; Import Note.com.bean.a; Import note.com.bean.b;  Public Interface ifactory {    public  A geta ();          Public B Getb ();}

Factory Implementation Class 1

 Packagenote.com.factory;ImportNote.com.bean.a;Importnote.com.bean.A1;Importnote.com.bean.b;Importnote.com.bean.B1; Public classInterfaceFactory1Implementsifactory{ PublicA Geta () {return NewA1 (); }     PublicB Getb () {return NewB1 (); }    }

Factory Implementation Class 2

 Packagenote.com.factory;ImportNote.com.bean.a;Importnote.com.bean.A2;Importnote.com.bean.b;Importnote.com.bean.B2; Public classInterfaceFactory2Implementsifactory{ PublicA Geta () {return NewA2 (); }     PublicB Getb () {return NewB2 (); }}

Test Class A factory produces different types of objects and different factories can produce objects of different implementations of the same type

 Packagenote.com.factory;ImportNote.com.bean.a;Importnote.com.bean.b; Public classIfactorytest { Public StaticA Geta (Ifactory factory) {returnFactory.geta (); }     Public StaticB Getb (Ifactory factory) {returnFactory.getb (); }     Public Static voidMain (string[] args) {ifactory factory1=NewInterfaceFactory1 (); Ifactory Factory2=NewInterfaceFactory2 (); A A=NULL; b b=NULL; A=Ifactorytest.geta (Factory1); b=Ifactorytest.getb (Factory1); A=Ifactorytest.geta (Factory2); b=Ifactorytest.getb (Factory2); }}

(i) Factory mode-code implementation

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.