Design pattern (v): Abstract Factory Method (combination of multiple factory methods)

Source: Internet
Author: User

I. Overview

  Abstract Factory mode provides an interface for creating a family of related or dependent objects without explicitly specifying a specific class. can be understood as a combination of multiple factory methods.

Second, solve the problem

In factory method mode, our specific creator can only create one object of the same type each time it is used, and if we need multiple objects of different types now, the factory method will not meet the requirements. At this point we can combine multiple factory methods into a single class, which is the abstract factory model, which is designed to create multiple products, or to create a product family.

Third, structure class diagram

Iv. Role of Members

  Abstract Factory (abstractfactory): A client direct reference, consisting of a factory method that is not implemented, the subclass must implement its factory method to create a product family.

  Specific Factory (concretefactory): Implement the Abstract factory interface, responsible for implementing the Factory method, a specific factory can create a set of products.

  Abstract Product (abstractproduct): The parent of a product family that can derive many sub-products.

  specific product: derived from an abstract product, created directly by the factory method.

V. Examples of applications

In the factory method model, we used the Open Sesame Shop example, now we let us continue to open the pancake shop, in order to ensure the quality of sesame cake, we want to unify the production process, but also to do a uniform supply of raw materials to prevent jerry-building problems. We are using the abstract factory method to unify the supply of raw materials, together to see how to do it.

Start by creating an abstract factory, which is the raw material factory

Package Abstrfactorymethod.pattern;public interface Ingredientfactory {public dough Createdough ();p ublic Sauce Createsauce ();p ublic vegetable createvegetable ();p ublic meat createmeat ();p ublic Seafood Createseafood ();

The raw materials factory involves a lot of raw material products, we create these products now, start creating abstract products first

  

Package abstrfactorymethod.pattern;//flour Public interface Dough {public String getName ();}

  

Package abstrfactorymethod.pattern;//Ingredient Public interface Sauce {public String getName ();}

  

Package abstrfactorymethod.pattern;//Vegetables Public interface Vegetable {public String getName ();}

  

Package abstrfactorymethod.pattern;//meat Public interface Meat {public String getName ();

  

Package abstrfactorymethod.pattern;//Seafood Public interface Seafood {public String getName ();

Then create the specific product you want

Package Abstrfactorymethod.pattern;public class Dbdough implements Dough{public Dbdough () {System.out.println ("create" + GetName ());} Public String GetName () {return "Northeast Flour";}}

  

Package Abstrfactorymethod.pattern;public class Tomatosauce implements Sauce{public Tomatosauce () {System.out.println ("Create" + getName ());} Public String GetName () {return "tomato sauce";}}

  

Package Abstrfactorymethod.pattern;public class Onion implements Vegetable{public Onion () {System.out.println ("create" + GetName ());} Public String GetName () {return "Onion";}}

When the material is ready, we can create a specific factory.

Package Abstrfactorymethod.pattern;public class Shaobingingredientfactory implements Ingredientfactory{public Dough Createdough () {return new Dbdough ();//Tohoku Flour}public Meat createmeat () {return null;//temporarily without meat, the interface alternate}public Sauce createsauce () {return new tomatosauce ();//tomato sauce}public Seafood createseafood () {return null;//temporarily without seafood, the interface alternate}public vegetable Createvegetable () {return new Onion ();//Onion}}

With the raw materials factory, you can start to make sesame cake, first create abstract sesame cake

Package Abstrfactorymethod.pattern;public Abstract class Shaobing {//Pancake name public String name;//Pancake with ingredients public Sauce Sauce ;//Dough public dough dough;//vegetables public vegetable vegetable;//The method is implemented in the subclass, creating the required raw materials public abstract void prepare ();//baking pancake public void Bake () {System.out.println ("Bake for minutes at 350C");} Tangent group public void Cut () {System.out.println ("cutting the dough into fit slices");} Package public void Box () {System.out.println ("place shaobing into official box");}}

Create a specific pancake

Package abstrfactorymethod.pattern;//Onion Pancake public  class Onionshaobing extends shaobing{//raw materials Abstract Factory ingredientfactory ingredientfactory;public onionshaobing () {ingredientfactory = new shaobingingredientfactory ();} @Overridepublic void Prepare () {System.out.println ("onion pancake begins to prepare raw materials");d Ough = Ingredientfactory.createdough (); sauce = Ingredientfactory.createsauce (); vegetable = Ingredientfactory.createvegetable ();}}

Sesame cake Everything is ready, you can open a shop

Package abstrfactorymethod.pattern;//Guangzhou Pancake Shop public class Gzshaobingstore extends shaobingstore{@Overridepublic Shaobing createshaobing (String type) {shaobing shaobing = null;if ("Onion". Equals (Type)) {shaobing = new onionshaobing ();} return shaobing;}}

Test the operation of the pancake shop

Package Abstrfactorymethod.pattern;public class Testshaobingstore {public static void main (string[] args) {// Open a pancake shop in guangzhou shaobingstore gzstore = new Gzshaobingstore ();//Sell an onion pancake gzstore.ordershaobing ("onion");}

Operation Result:

Vi. Advantages and Disadvantages

  1. Advantages

(1), allows customers to use an abstract interface to create a set of related products, without needing to know (or care about) the output of the specific product is what, so that customers can be decoupled from the specific product.

(2), a specific factory can create a number of products, compared with the factory method model, can produce fewer specific plant classes.

(3), easy to Exchange product line, as long as the replacement of specific factories, you can change the product line.

 2. Disadvantages

(1), abstract Factory is used in a combination of methods to assemble the factory method into a class, when a new product family members will have to modify the abstract factory class and its following specific factory class, so its extensibility is poor.

(2), each new product sub-class to create a class, when the product subclass too many will produce a lot of classes, resulting in increased system complexity.

Seven, the use of the occasion

1. Use when you need to create a product family, or if you want to set up a product that you create.

2, when the series is not fixed, in the future may increase the entire series of products used.

Design pattern (v): Abstract Factory Method (combination of multiple factory methods)

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.