C + +, Java implementations of the abstract factory model

Source: Internet
Author: User

1. Abstract Factory mode UML

1 Total UML for Abstract Factory mode

2. C + + implementation

The C + + implementation class Diagram Is:

2 Total C + + implementation class diagram for abstract Factory mode

Where the implementation code for Abstractfactory Is:

1 //abstract Factory class base class. 2 classabstractfactory3 {4  public:5 abstractfactory ();6~abstractfactory ();7 8     //Create an abstract factory method for a specific product. 9     //creates a Class A object. Ten     VirtualProductA * Createproducta () =0; one  a     //Create a Class B product object.  -     VirtualPRODUCTB * CREATEPRODUCTB () =0; -};

The implementation code for the Concretefactorya is:

1 classConcreteFactory1:2      publicabstractfactory3 {4  public:5 ConcreteFactory1 ();6~ConcreteFactory1 ();7 8     //creates an abstract factory method for ConcreteProductA1. 9ProductA *createproducta ();Ten  one     //creates an abstract factory method for ConcreteProductA1.  aPRODUCTB *CREATEPRODUCTB (); -  -};//End Class Concretefactorya the  -  -ProductA *concretefactory1::createproducta () - { +PRODUCTA * PA =NewConcreteProductA1 (); -     returnpa; +}//End Method Createproducta a  atPRODUCTB *CONCRETEFACTORY1::CREATEPRODUCTB () - { -PRODUCTB * PB =NewConcreteProductB1 (); -     returnpb; -}//End Method CREATEPRODUCTB

The ConcreteFactory2 code is similar to ConcreteFactory1 and is no longer listed.

3. Implementation of Java

The Java Implementation Class View Is:

Figure 3. Java implementation Class View of abstract Factory mode

Where the code for Abstractfactory is:

1 //Abstract Factory class. 2  public InterfaceAbstractfactory {3 4     //Create an abstract factory method for Class A Products. 5      public Abstractproducta createproducta ();6     7     //Create an abstract factory method for Class B Products. 8      public AbstractPRODUCTB CREATEPRODUCTB ();9     Ten}//End Interface Abstractfactory

The code for ConcreteFactory1 is:

1  public classConcreteFactory1ImplementsAbstractfactory {2 3     //creates an ConcreteProductA1 Object. 4 @Override5      publicproducta createproducta () {6         //TODO auto-generated Method Stub7         return NewConcreteProductA1 ();8}//End Method Createproducta9 Ten     //creates an ConcreteProductB1 Object.  one @Override a      publicPRODUCTB CREATEPRODUCTB () { -         //TODO auto-generated Method Stub -         return NewConcreteProductB1 (); the}//End Method CREATEPRODUCTB -  -}//End Class ConcreteFactory1

The code for ConcreteFactory2 is similar to ConcreteFactory1 and is no longer listed Here.

4. Click to download the source code

C + +, Java implementations of the abstract factory model

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.