Design pattern Learning--abstract factory

Source: Internet
Author: User
Tags abstract

Brief introduction:

When each abstract product has more than one specific subclass, how does a factory role know which subclass to instantiate? For example, each abstract production [1] product role has two specific products. The abstract factory model provides two specific factory roles that correspond to these two specific product roles, and each specific factory role is responsible for instantiating only one product role. Each specific factory class is responsible only for creating an instance of a specific subclass of the abstract product.

Each pattern is a solution to a problem, the factory method pattern is for a product hierarchy, and the abstract factory model is for multiple product hierarchy structures. (Excerpt from Baidu Encyclopedia)

Speech is too abstract, the best way to express the programmer is code, of course, we first use UML class diagram to intuitively understand.

UML Class Diagram:

From the above class diagram, you can see that there are two product families, respectively, abstract products 1 and abstract products 2 of these two product families, each specific factory can produce a number of product family products, such as specific products 1 not only the production of specific products 11, but also the production of specific products 21.

Product Family: refers to the family of products that are related to function in different product grade structure. For example, in the above figure, the specific product 11 and the specific product 12 to form a product family.

Code profiling:

Product Family 1 Interface abstractproduct1{} class Product11 implements abstractproduct1{public Product11 () {
         System.out.println ("produced product 11"); } class Product12 implements abstractproduct1{public Product12 () {System.out.println ("produced product
         12 ");
                   Product Family 2 Interface abstractproduct2{} class Product21 implements abstractproduct2{public Product21 () {
         System.out.println ("produced product 21"); } class Product22 implements abstractproduct2{public Product22 () {System.out.println ("produced product
         22 "); } interface abstractfactory{AbstractProduct1 createP1 ()//Product product Family 1 AbstractProduct2 createP2 ();//Live Product Family 2 of products}//specific factory 1, by the specific factory 1 themselves decide which product of each product class Factory1 implements abstractfactory{public AbstractProduct1 CRE
         AteP1 () {return new Product11 (); Public AbstractProduct2 createP2 () {return nEW Product21 (); }//Specific factory 2, by the specific factory 2 themselves decide which product class Factory2 implements abstractfactory{public AbstractProduct1 createP1 () of each product family
         {return new Product12 ();
         Public AbstractProduct2 createP2 () {return new Product22 (); }
}

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.