Factory model trilogy 2_factory Model

Source: Internet
Author: User
Tags define abstract

Since the "factory model trilogy _ simple factory model", it seems that this trilogy has not been continued for a long time, so take some time to complete the last two articles. First, let's review the main content of the simple factory model: the simple factory model is essentially a factory class that uses static factory methods to create objects based on user needs, the advantage is that the pressure on the client is relatively small, and the client does not need to create an instance, but only needs to call methods in the factory class to obtain the corresponding instance, and its disadvantages are also very obvious, all of its creation work is completed in a factory class, with high coupling, and the factory class needs to be changed when the system needs to add or modify the product, the factory model is introduced because it does not conform to the principle of single responsibility or the principle of opening-closing.

 

Factory Model

The factory model further abstracts the simple factory model and is derived from the simple answer factory model. According to the basic principles of the design model, it is also the essence of the design model "encapsulation change point ", that is to say, the change is encapsulated, so the factory model encapsulates the factory class in a simple factory to meet the single responsibility and the open-close principle.

[Solution]: the simple factory mode violates the single responsibility and the open-close principle.

[Core idea]: build a base class of an abstract factory and create a factory for each product. The factory inherits the abstract factory and requires that each product has a specific factory to be created.

[Role]

Abstract Factory role: this is the core of the factory model. It has nothing to do with the application. It is the parent class that a factory must inherit or the interface that must be implemented.

Specific factory role: contains the logic business code, called by the application to create a specific product object

Abstract Product role: the parent class or required interface that a product must inherit

Specific product role: the created object is the instance of this role

 

The following is an example of a simple factory, which will be transformed.

[Role analysis]

Abstract Factory: clothing manufacturers include some common characteristics of clothes
Specific Factory: ** type clothing manufacturers each clothing manufacturer is responsible for only one dress (High Cohesion ), of course, that is to say, when there is no customer need to create a corresponding factory and a dress, the factory is responsible for providing the dress, but it has no impact on the previous factory and clothes (this is the design what the model expects-single responsibility principle) abstract product: Clothes include some common features of clothes
Specific products: ** type clothes inherit abstract products, and of course they can expand their own features

After the role analysis, you can perform the transformation and paste the code of each part after the transformation:

Abstract Factory class:

Abstract method 1 // Abstract Factory: declare the factory method to return a product
2 public abstract class clothfactory
3 {
4 public abstract cloth createcloth ();
5}

This class defines an abstract createcloth () method for creating clothes instances.

 

Specific factory type:

Specific Factory 1 // specific factory 1: Commercial assembly factory is only responsible for producing commercial assembly
2 public class businessfactory: clothfactory
3 {
4 Public override cloth createcloth ()
5 {
6 return New businesscloth ();
7}
8}
9
10 // factory 2: The sportswear factory is only responsible for producing sportswear
11 public class sportfactory: clothfactory
12 {
13 public override cloth createcloth ()
14 {
15 return New sportcloth ();
16}
17}
18
19 // factory 3: the school uniform factory is only responsible for producing school uniforms
20 public class lifefactory: clothfactory
21 {
22 public override cloth createcloth ()
23 {
24 return New lifecloth ();
25}
26}

This class is the core. The class defines three factories, namely businessfactory, sportfactory, and lifefactory, to create business clothes, sportswear, and school uniforms. The three factories inherit the abstract factory clothfactory, the createcloth () method in the abstract factory is rewritten to create the clothes produced by the factory respectively, and implemented by using the Lee's replacement principle (subclass instead of the parent class, this is when the product needs to be changed (ADD, modify, delete). For example, you only need to add a factory dedicated to the product and the product, the add operation has no direct impact on all previous factories and products. Similarly, you only need to modify the factory class to create the product. It is good to follow the open-close principle. At the same time, a factory class creates a product and follows the principle of single responsibility.

 

Abstract product category:

Abstract product 1 // define abstract product: Clothes
2 public abstract class cloth
3 {
4 public abstract void getcloth ();
5}

This method does not need to be changed. A getcloth () method is still defined to simulate the use of products.

 

Specific product categories:

Specific product 1 // define specific product: 1: Commercial Installation
2 class businesscloth: Cloth
3 {
4 Public override void getcloth ()
5 {
6 console. writeline ("business installation ......");
7}
8}
9
10 // define Product 2: sportswear
11 class sportcloth: Cloth
12 {
13 public override void getcloth ()
14 {
15 console. writeline ("sportswear .......");
16}
17}
18
19 // define Product 3: Casual Wear
20 class lifecloth: Cloth
21 {
22 public override void getcloth ()
23 {
24 console. writeline ("casual wear ........");
25}
26}

This class does not need to be changed either. There is no direct impact on the encapsulation and change of the factory class, and three types of clothes are still defined, and rewrite the abstract methods in abstract products, because each product has its own characteristics and has its own unique functions.

 

Client:

Client 1 // Client
2 class Program
3 {
4 static void main (string [] ARGs)
5 {
6 // requires commercial Installation
7 cloth C = new businessfactory (). createcloth ();
8 C. getcloth ();
9
10 // requires sportswear
11 cloth C2 = new sportfactory (). createcloth ();
12 c2.getcloth ();
13
14 console. readkey ();
15}
16}

Running effect:

 

 

Summary of advantages and disadvantages:

Advantage: it solves the problem of violating single responsibility and opening-closing principle in a simple factory, increases program scalability, and shields the product class.

Disadvantages: 1. logic judgment is placed on the client.

2. Every time you add a product, you need to add a factory for it for production.

3. Each factory can only produce one product with a single variety.

 

 

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.