Design Mode Reading Notes (5)-Comparison of Three factory Modes

Source: Internet
Author: User

Design Mode Reading Notes (5)-Comparison of Three factory Modes

First look at the main code of the three factory models (the complete code is in the first three blogs ):

Simple factory mode:

public class SampleFactory{public static Animal createAnimal(String animalName){if("Tiger".equals(animalName))){return new Triger();}else if("Dolphin".equals(animalName)){return new Dolphin();}}}

Factory method mode:

public interface Factory{public Animal createAnimail();}public class Trigerfactory implements Factory{public Animal createAnimal(){return new Triger();}}public class Dolphinfactory implements Factory{public Animal createAnimal(){return new Dolphin();}}

Abstract Factory mode:

Suitable for Asian tigers and Asian dolphins; public class AsiaFactory implements Factory {public Triger createTriger () {return new AsiaTriger () ;}; public doldolphin createdoldolphin () {return new asiadoldolphin () ;}}suitable for African tigers and African dolphins; public class AfricaFactory implements Factory {public Triger createTriger () {return new AfricaTriger () ;}; public doldolphin createdoldolphin () {return new africadoldolphin ();};}

Specific differences:

Simple Factory:
A specific factory creates multiple products using condition statements. The product creation logic is concentrated in one factory class.
The client transmits different parameters to the factory to create different products.
When adding a new product, you need to modify the factory class and add the product class, which does not comply with the OCP principle.

Factory method:
A factory creates a product, and all specific factories inherit from an abstract factory.
The client first creates factories for different products, and then the factory creates specific products. The product creation logic is distributed in each specific factory class.
The client only depends on the abstract factory and the abstract product, and does not depend on any specific factory or product.
When adding new products, you need to add factory and product categories to comply with the OCP principles.

Abstract Factory:
A specific factory creates a product family. A product family is a combination of different product series. The product creation logic is divided into specific factory classes. All specific factories inherit from the same abstract factory.
The client creates factories of different product families, and the factory of the product family creates specific products that are invisible to the client.
When adding a new product family, you need to add specific factory classes to comply with the OCP principles.
When adding a new product, you need to modify the specific factory class and add the product class, which does not comply with the OCP principle.
If there is no need to cope with the changes in the "Multi-series object creation", there is no need to use the abstract factory mode. In this case, a simple static factory is enough.

The three models mentioned above have separated the client from the coupling with specific products. The client does not pay attention to the production methods of specific products.

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.