[Design pattern sorting Note 5] creator pattern (builder)

Source: Internet
Author: User

[Guide]

[Design mode sorting Note 1] basic knowledge

[Design mode sorting Note 2] simple factory Mode)

[Design mode sorting Note 3] factory Mode)

[Design pattern sorting Note 4] abstract factory pattern (Abstract Factory)

[Design pattern sorting Note 5] creator pattern (builder)

[Design pattern arrangement Note 6] Summary of factory pattern and creator Pattern

[Design mode sorting Note 7] prototype)

[Design mode sorting Note 8] Singleton)

[Design mode sorting Note 9] appearance mode (facade)

... Later, including some examples

[/Guide]

 

 

In the simple factory mode, we only need to write a factory class to instantiate the class you need. In the factory model, there are several different products in our system. We will write several corresponding factory classes for production, which is equivalent to the current workshop, rather than instantiating different classes in the workshop, that is, some products are very similar. They all need to write two factory classes to produce and return the classes you need. In the abstract factory model, we will put a series of similar products in a factory class for instantiation, which is similar to the current workshop assembly line. One assembly line is responsible for producing similar products, different products need another pipeline for production. The factory model is used to create factories for subtle products, such as individuals.RealIn the wage calculation process, we still need to Calculate taxes, funds, and other operations. If we use the factory model for design, it will become complicated. How can we design a better way to deal with this problem? The author's model is used to introduce the computing process of a subsidiary in Shenzhen:

Since it is a subsidiary of Shenzhen, we should first create the class prefix of this subsidiary:

Code

Using System;

Namespace Consoleapp
{
Public   Class Shenzhencompany
{
Public   Virtual   Void Commandsalary ()
{
Console. writeline ( " This is the result of System Computing by Shenzhen subsidiary. " );
}
}
}

 

Since three processes are to be calculated, the following three calculation classes are defined: wages, taxes, and funds:

Salary:

Code

// Define interface methods
Namespace Consoleapp
{
Public   Interface Isalary
{
Void Commandsalary ();
}
}

// Definition class
Namespace Consoleapp
{
Public   Class Shenzhensalary: isalary
{
Public   Virtual   Void Commandsalary ()
{
Console. writeline ( " This is the wage calculation module of Shenzhen subsidiary. " );
}
}
}

 

The tax is defined as follows:

Code

// Define interface methods
Namespace Consoleapp
{
Public   Interface Itax
{
Void Commandtax ();
}
}

// Definition class
Namespace Consoleapp
{
Public   Class Shenzhentax: itax
{
Public   Void Commandtax ()
{
Console. writeline ( " This is the tax deduction calculation module of Shenzhen subsidiary. " );
}
}
}

 

The Fund is defined as follows:

Code

// The interface definition is as follows:
Namespace Consoleapp
{
Public   Interface Ifund
{
Void Commandfund ();
}
}
// Definition class
Namespace Consoleapp
{
Public   Class Shenzhenfund: ifund
{
Public   Void Commandfund ()
{
Console. writeline ( " This is the fund computing module of Shenzhen subsidiary. " );
}

}
}

 

The classes are basically defined here. Generally, we can instantiate them directly when needed. However, you know that designing a good call method will help you better maintain them in the future, I believe it will not be used to instantiate an object directly. First define an abstract factory class (ifacloud)

Namespace Consoleapp
{
Public   Interface Ifacloud
{
Isalary createsalary ();
Itax createtax ();
Ifund createfund (); }
}

 

Now you can create a class, which is derived from ifacloud. I define its name as shenzhenfactory. In this class, it is a class that returns three operation flows:

Code

Namespace Consoleapp
{
Public   Class Shenzhenfactory: ifacloud
{
Public Isalary createsalary ()
{
Return   New Shenzhensalary ();
}

Public itax createtax ()
{< br> return New shenzhentax ();
}

PublicIfund createfund ()
{
Return NewShenzhenfund ();
}
}
}

 

Now you know the role of this class. Every corresponding method returns the corresponding class, so we only need to create another class to assemble them. The client no longer needs various processes. All the processes are encapsulated in the corresponding classes, and their responsibilities are clear.

Code

Namespace Consoleapp
{
Public   Class Director
{
Private Ifactory factory;

PublicDirector (ifactory factory)
{
This. Factory=Factory;
}

Public Shenzhencompany assembleshenzhencompany ()
{
Isalary salary = Factory. createsalary ();
Salary. commandsalary (); // Wage Calculation

Itax tax = Factory. createtax ();
Tax. commandtax (); // Tax calculation

Ifund Fund = Factory. createfund ();
Fund. commandfund (); // Computing Fund

Return   New Shenzhencompany ();
}

}
}

 

The whole process is under our control. The following is the call,CodeAs follows:

Code

Namespace Consoleapp
{
Class Program
{
Public   Static   Void Main ( String [] ARGs)
{
Director mydire =   New Director ( New Shenzhenfactory ());
Shenzhencompany szcompany = Mydirector. assembleshenzhencompany ();
Console. Readline ();
}
}
}

 

The output is as follows:

This is the wage calculation module of Shenzhen subsidiary.

This is the tax deduction calculation module of Shenzhen subsidiary.

This is the fund computing module of Shenzhen subsidiary.

 

So far, a complete process has come out. Of course, you can write the code of the tax deduction or fund module in the factory mode in the same way as the wage calculation module. When practicing, now we know when to use the factory model and when to use the Creator model! The following chapter describes the features of simple factory mode, factory mode, abstract factory mode, and creator mode. Make sure you are impressed after reading it.

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.