Abstract Factory mode

Source: Internet
Author: User



Abstract Factory mode provides a solution for creating a set of objects. Compared to the factory method pattern, the specific factory in the abstract factory pattern is not just creating a product, it is responsible for creating a product family. Abstract Factory mode provides an interface to create a series of related or interdependent objects without specifying their specific classes. The abstract Factory mode, also known as the kit mode, is an object-creation pattern.

The abstract factory pattern contains four roles: The abstract factory is used to declare the method of generating abstract products; The factory implements an abstract factory declaration of the method of generating abstract products, a set of specific products, which constitute a product family, each product is located in a product hierarchy; Abstract products declare interfaces for each product, The abstract business method of the product is defined in the abstract product, and the specific product defines the specific product objects produced by the specific factory, and realizes the business method defined in the abstract product interface.

Abstract Factory mode is the most abstract and general form of all forms of Factory mode. The biggest difference between the abstract factory model and the factory method pattern is that the factory method model is for a product hierarchy, while the abstract factory model requires multiple product hierarchies. Abstract Factory mode isolates the generation of specific classes so that customers do not need to know what is being created, and each time it is possible to create multiple objects in a product family through a specific factory class, it is convenient to add or replace product families, and it is convenient to add new specific factories and product families, but it is complex to add new product hierarchies Need to modify the abstract factory and all the specific factory classes, the "open and close principle" support is tilted.

The application of the abstract Factory pattern includes: A system should not rely on the details of how a product class instance is created, composed, or expressed; there are more than one product family in the system, and only one product family is used at a time; products belonging to the same product family will be used together; The system provides a library of product classes, All products appear on the same interface, so that the client does not rely on a specific implementation.

We take the production of a set of mobile phone products, for example, mobile phones include the phone itself and external headphones. We need to produce different brands of mobile phones, then we can target different brands of mobile phone products to do a separate factory dedicated to the production of this brand products.


First, create a phone abstract class that defines the contents of a phone's generality:


<span style= "FONT-SIZE:18PX;" >package abstract Factory;/** * Demand: Abstract Phone class * @author win2016 * */public abstract class Phone {    protected String brand;//Mobile phone brand 
   protected String model; Phone model public    phone (String brand,string model) {    This.brand = brand;        This.model = model;    } public abstract void Call ();  Call public    abstract void SendMessage ();  Hair Information}</span>

One more headset. Abstract class:


<span style= "FONT-SIZE:18PX;" >package abstract Factory;/** * Requirements: Headphones abstract class * @author Dragon over the river * */public abstract class headset{    protected String brand;//headphone Brand 
   //Ellipsis method declaration}</span>


to a Samsung brand headphone class:


<span style= "FONT-SIZE:18PX;" >package abstract Factory;/** * Demand: Samsung headphone class * @author dragon across the river * */public class Samsungheadset extends headset{public    Samsungheadset ( {        System.out.println ("Samsung headset");}    } </span>

Samsung mobile phone Category:


<span style= "FONT-SIZE:18PX;" >package abstract Factory;/** * Demand: Samsung brand mobile Phone class * @author dragon across the river * */public class Samsungphone extends phone{public    samsungphone (stri Ng brand, String model) {        Super (brand, model);    }    @Override public    Void Call () {        System.out.println ("Use" +this.brand+ "" +this.model+ ");    }    @Override public    void SendMessage () {        System.out.println ("Using" +this.brand+ "" +this.model+ "Texting");}    } </span>

Apple Headset class:


<span style= "FONT-SIZE:18PX;" >package abstract Factory;/** * Demand: Apple headphone class * @author dragon across the river * */public class Appleheadset extends headset{public    Appleheadset () {
   system.out.println ("Apple Headset");}    } </span>


Apple Phone class:


<span style= "FONT-SIZE:18PX;" >package abstract Factory;/** * Demand: Apple phone class * @author dragon across the river * */public class Applephone extends phone{public    applephone (String Bra nd, String model) {        Super (brand, model);    }    @Override public    Void Call () {        System.out.println ("Use" +this.brand+ "" +this.model+ ");    }    @Override public    void SendMessage () {        System.out.println ("Using" +this.brand+ "" +this.model+ "Texting");}    } </span>


Product line:


<span style= "FONT-SIZE:18PX;" >package abstract Factory;/** *  product line */public abstract class Productfactory {    //Production phone public    abstract phone Makephone () ;    Production headphones public    abstract Headset makeheadset ();} </span>


Samsung Products Factory:


<span style= "FONT-SIZE:18PX;" >package abstract Factory;/** * Demand: Samsung products factory * @author Dragon across the river * */public class Samsungproductionline extends Productfactory {    @Overr IDE Public    Phone Makephone () {    return new Samsungphone ("Samsung", "Galaxy");    }    @Override public    Headset Makeheadset () {        return new Samsungheadset ();}    } </span>

Apple Foundry Factory:


<span style= "FONT-SIZE:18PX;" >package abstract Factory;/** * Demand: Apple factory * @author Dragon across the river * */public class Appleproductionline extends Productfactory {    @Override C1/>public Phone Makephone () {        return new Applephone ("Apple", "IPhone 5");    }    @Override public    Headset Makeheadset () {        return new Appleheadset ();}    } </span>


Test class:


<span style= "FONT-SIZE:18PX;"  >package abstract Factory;/** * Requirements: Test class * @author dragon across the river * */public class Test {public static void main (string[] args) {productfactory Productionline;  Abstract factory, mobile phone production line phone    phone;//abstract products, mobile phone        Headset Headset;//abstract products, headphones        productionline = new Appleproductionline () ;  Specific factory  Apple production line        phone = Productionline.makephone ();           The specific product produced is the Apple mobile phone        headset = Productionline.makeheadset ();   The specific products produced are Apple headphones    phone.call ();    Phone.sendmessage ();    }} </span>

In this way, we can easily produce Apple, Samsung brand mobile phone.

  

Advantages and disadvantages of the abstract factory:


Pros: Abstract Factory mode isolates the generation of specific classes so that customers do not need to know what is being created. Because of this isolation, it becomes relatively easy to replace a specific factory. All of the specific factories implement the common interfaces defined in the abstract factory, so you can change the behavior of the entire software system to some extent by simply changing the instance of the specific factory.

In addition, the application of abstract Factory mode can realize the design goal of high cohesion and low coupling, so the abstract factory model has been widely used. When multiple objects in a product family are designed to work together, it ensures that the client always uses only objects from the same product family. This is a very practical design pattern for some software systems that need to determine their behavior based on the current environment. It is convenient to add new factories and product families, and it is not necessary to modify the existing system to meet the "open and closed principle".


Disadvantage: When adding new product objects, it is difficult to expand the abstract factory to produce new kinds of products, because in the abstract factory role is defined in all possible to create a collection of products, to support the new kind of product means to extend the interface, which will involve the abstract factory role and all its subclasses modification, Obviously, it will bring about great inconvenience. The tilt of the open/closed principle (adding new plant and product families is easy, adding new product grade structure trouble).




Abstract Factory 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.