Factory model trilogy _ simple factory Model

Source: Internet
Author: User

During this time, I want to have a good understanding of the factory model and read many blogs about the factory model in the garden. I have benefited a lot ...... at the same time, I would like to thank all bloggers for sharing their knowledge selflessly .... the accumulation of knowledge lies in accumulation. However, I personally think the best way to accumulate knowledge is to make a summary after learning a certain knowledge point, and then slowly reason what I learned, digest it in detail, and then convert it into your own things .......

Simple factory Mode

The simple factory mode is also called the static factory mode. As the name suggests, it is used to instantiate the static class of the target class, that is, the client does not need to be responsible for "Creating" objects, as long as an instance of the corresponding class is created through this static class and returned to the client, the client only needs to be responsible for "consumption...

[Solution]: How to instantiate a suitable object

[Core idea]: There is a special class responsible for the process of creating an instance and returning the object to the instance.

[Angle color]

Factory role: act as the core of the simple factory model for this role. The factory class creates a product object and returns this object under direct calls from the client.

Abstract Product role: the class that assumes this role is the parent class of the object created by the factory class, or they have a common interface.

Specific product role: Any object created by the factory class is an instance of this role.

 

The following is an example.

First of all, let's get to know the actual scenario: A person always has many different types of clothes to adapt to different occasions. How can I use a program to implement it ??

Analyze this scenario:

Factory type: Obtain the clothes --- createcloth () method based on the actual situation
Abstract product: clothes have some common features, or define common methods or attributes in this class. ----- Abstract: getcloth () abstract Method
Specific products: Clothing inheritance and interfaces or abstraction have their own characteristics

 

Next, paste the Code:

Abstract product class: defines a common method getcloth () to obtain the corresponding clothes.

1 // abstract product: Get clothes
2 public abstract class cloth
3 {
4 public abstract void getcloth ();
5}

 

Specific product categories: three specific products are defined: business, casual, and sportswear. They all inherit from the abstract product categories and override the abstract methods in the abstract product categories.

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

 

Factory class (CORE): instantiate the object of the corresponding class based on the parameters passed in by the client and return the object to the client (the parameter here is the lowercase letter of the Pinyin of the clothing type ), defines a createcloth () method ---- Of course, the type here must be known, you can also place the type in the configuration file for extension ,, easy to add and modify types ........

1 // factory class: Creates and returns Objects Based on Client Conditions
2 public class clothfactory
3 {
4
5 public static cloth createcloth (string clothtype)
6 {
7 cloth = NULL;
8 switch (clothtype)
9 {
10 case "SW ":
11 cloth = new business installation ();
12 break;
13 case "YD ":
14 cloth = new sportswear ();
15 break;
16 case "XX ":
17 cloth = new casual wear ();
18 break;
19 default:
20 cloth = NULL;
21 break;
22}
23 return cloth;
24}

 

Client: Call the static method of the factory class and pass in appropriate parameters to obtain the corresponding object.

1 // Client
2 static void main (string [] ARGs)
3 {
4 // requires commercial Installation
5 cloth = clothfactory. createcloth ("SW ");
6 cloth. getcloth ();
7
8 // requires sportswear
9 cloth clothsport = clothfactory. createcloth ("YD ");
10 clothsport. getcloth ();
11
12 console. readkey ();
13}

 

 

Summary advantages and disadvantages:

Advantage: users create the required object instances based on the factory class in use, without having to know how to create and organize such details, which is conducive to the optimization of the software architecture.

Disadvantages: 1. creation and operations on different instance objects are completed in one class, that is, they do not conform to the principle of single responsibility.

2. It is difficult to add or delete a function. You need to modify the factory class, which violates the open-closed principle (for modification and closure, for extension)

3. the method in the factory class is static (static factory method), and creation behavior cannot be changed through inheritance.

 

Hey, it's the factory model trilogy... of course, there will be subsequent factory models and abstract factory models .......... stay tuned .............

 

 

 

 

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.