Analysis of Abstract Factory, factory method, simple (static) Factory

Source: Internet
Author: User
---- Simple factory (also called static factory model): An abstract product abstracts multiple product classes and a specific factory class.
Code:
// Abstract Product role public interface car {public void drive ();} // specific product role public class Benz implements car {public void drive () {system. out. println ("Driving Benz");} public class BMW implements car {public void drive () {system. out. println ("Driving BMW") ;}// factory class role public class driver {// factory method. note that the return type is abstract Product role public static car drivercar (string s) throws exception {// judgment logic. Return the specific product role to the client if (S. equalsignorecase ("be NZ ") return New Benz (); else if (S. equalsignorecase ("BMW") return new BMW ();...... else throw new exception ();... // Welcome to the startup ...... public class magnate {public static void main (string [] ARGs) {try {// tell the driver that I am sitting on a Mercedes-Benz car = driver today. drivercar ("Benz"); // run the following command: drive car. drive ();...


---- Factory method: One abstract product class derives multiple specific product classes, one abstract factory class derives multiple specific factory classes, and each specific factory class can only create an instance of a specific product class.

Define an interface for creating an object (that is, an abstract factory class), and let its subclass (a specific factory class) decide which class to instantiate (a specific product class ). "One-to-one" relationship.


(The solid line in the figure indicates that the inheritance is incorrect. It should be a dotted line to indicate implementation)

Factory method:

Public interface product {} public interface creator {public product factory ();} public class concreteproduct1 implements product {public concreteproduct1 () {system. out. println ("concreteproduct1 created") ;}} public class concreteproduct2 implements product {public concreteproduct2 () {system. out. println ("concreteproduct2 created") ;}} public class concretecreator1 implements creator {public product factory () {return New concreteproduct1 ();}} public class concretecreator2 implements creator {public product factory () {return New concreteproduct2 () ;}} public class client {Private Static creator creator1, creator2; Private Static product prod1, prod2; public static void main (string [] ARGs) {creator1 = new concretecreator1 (); prod1 = creator1.factory (); system. out. println ("----------------------------"); creator2 = new concretecreator2 (); prod2 = creator2.factory ();}}


---- Abstract Factory

Abstract Factory: Multiple abstract product classes are derived from multiple specific product classes. One abstract factory class is derived from multiple specific factory classes. Each factory class can create instances of multiple specific product classes.

That is, an interface is provided to create a series of related or mutually dependent objects without specifying their specific classes. "One-to-many" relationship.


Advantages:

1. The Abstract Factory mode isolates the production of specific classes so that customers do not need to know what is created.

2. When multiple objects in a product family are designed to work together, it ensures that the client always uses only objects in the same product family.

3. It is convenient to add new specific factories and product families. You do not need to modify existing systems and comply with the "Open and Close principles ".

 

Disadvantages:

Adding a new product level structure is complex. You need to modify the abstract factory and all the specific factory classes, and the support for the "Open and Close principle" is skewed.


Applicable environment:

1. A system should not depend on the details of how product instances are created, combined, and expressed. This is important for all types of factory models.

2. There are more than one product family in the system, and only one of them is used at a time.

3. products belonging to the same product family will be used together. This constraint must be reflected in the system design.

4. The system provides a product library. All products use the same interface, so that the client does not rely on the specific implementation.

Abstract Factory:

// Define certain standards for different products, and implement them using interfaces // The method () method can be seen as extracting the commonalities of different products, for example, all mobile phones have similar functions: interface iproducta {public void method ();} interface iproductb {public void method ();} // a series of specific products that have achieved product standards. // The A_1 has been designed for production by vendor 1, therefore, the following output code is "vendor X" class producta1 implements iproducta {public void method () {system. out. println ("vendor 1 produces producta1... ") ;}} class producta2 implements iproducta {public void method () {system. out. println ("vendor 2 produces producta2... ") ;}} class productb1 implements iproductb {public void method () {system. out. println ("vendor 1 produces productb1... ") ;}} class productb2 implements iproductb {public void method () {system. out. println ("vendor 2 produces productb2... ") ;}}// product production plants of each brand, that is, different manufacturers are responsible for the production of their own brand products abstract class factory1 {abstract iproducta getproducta1 (); abstract iproductb getproductb1 ();} abstract class factory2 {abstract iproducta getproducta2 (); abstract iproductb getproductb2 ();} // The specific factory is used to produce the related product class concretefactory1 extends factory1 {public iproducta getproducta1 () {return New producta1 ();} public iproductb getproductb1 () {return New productb1 () ;}} class concretefactoryb extends factory2 {public iproducta getproducta2 () {return New producta2 ();} public iproductb getproductb2 () {return New productb2 () ;}// test class public class client {public static void main (string [] ARGs) {// vendor 1 is responsible for producing products A1 and B1 factory1 factory1 = new concretefactory1 (); iproducta producta1 = factory1.getproducta1 (); iproductb productb1 = factory1.getproductb1 (); productb1.method (); // vendor 2 is responsible for producing products A2 and B2 factory2 factory2 = new concretefactoryb (); iproducta producta2 = require (); iproductb productb2 = factory2.getproductb2 (); productb2.method ();}}


Abstract Factory methods have almost no scalability in the vertical view. If we want to add a product C, that is to say, the product family will change from two to three, and the program will change a lot, add a method to the abstract factory. This seriously violates the open and closed principle. Abstract classes do not like to be modified.

However, in the horizontal view, the scalability is well supported. If we add a product level 3, we only need to write a create3 file, this is in line with the principle of opening and closing, and there should be as many implementation factory classes as the number of product levels. Each time a product grade is added, an implementation factory class is added accordingly. The scalability here is naturally reflected.



Application scenarios

Factory method:

The factory method mode is applicable in the following cases:

(1) When a class does not know the class of the object it must create.

(2) When a class wants its subclass to specify the object it creates.

(3) When the class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies.


Abstract Factory:

(1) A system should not depend on the details of how product instances are created, combined, and expressed. This is important for all forms of factory models.

(2) This system has more than one product family, and the system only consumes one of them.

(3) products belonging to the same product family are used together. This constraint must be reflected in the system design.

(4) The system provides a product library. All products use the same interface, so that the client does not rely on implementation.


Reprinted please indicate the source: http://blog.csdn.net/df1012890048/article/details/38672341

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.