[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]
I usually see someone in the garden questioning how the simple factory model was designed? The following describes its meaning.
A simple factory mode is a creation mode. After a simple understanding of the Creation Mode, an object is created and the corresponding instance is returned. Therefore, it focuses on who created it, how it was created, and when it was created, giving you great flexibility. The specific meaning is described as follows: the simple factory mode is also called the static factory method mode, which defines a specific factory class to be responsibleCreate some class instances. That is to say, this class integrates some functions similar to or similar class instantiation, such as the subsidiary wage calculation class, whether it is the subsidiary (because the subsidiary computing class inherits the isalary interface, so there are similar or similar features). To be instantiated, they are all completed through a class, which is a factory class. Let's take a look at how the simple factory model creates a wage calculation module.
Next let's take a lookCodeFirst, create an interface and define its method:
Using System;
NamespaceConsoleapp
{
Public InterfaceIsalary
{
VoidCommandsalary ();
}
}
Two subsidiary computing classes:
Code
Using System;
namespace consoleapp
{< br> Public class shenzhensalary: isalary
{< br> Public void commandsalary ()
{< br> console. writeline ( " This is the salary calculation module of Shenzhen subsidiary " )
}< BR >}
Classes implemented by Beijing subsidiaries:
Code
Using System;
namespace consoleapp
{< br> Public class beijinsalary: isalary
{< br> Public void commandsalary ()
{< br> console. writeline ( " This is the salary calculation module of Beijing subsidiary " );
}< BR >}< br>
When it is created like this, you need to consider calling it. When you need a Shenzhen subsidiary, You Need To directly instantiate the Shenzhen subsidiary or directly instance the Beijing subsidiary? Here is the discussion. Let's take a look at the factory code of the simple factory model:
Code
Using System;
Namespace Consoleapp
{
Public Class Salaryfactory
{
/// <Summary>
/// Returns the corresponding instance by the company name passed in.
/// </Summary>
/// <Param name = "companyName"> Subsidiary name </Param>
/// <Returns> </returns>
Public Static Isalary createsalary ( String CompanyName)
{
Isalary salary = Null ;
If (CompanyName = " Shenzhen " ) // Here, we can use more methods to determine whether to return the corresponding instance with different names.
{
Salary = New Shenzhensalary ();
}
Else If (CompanyName = " Beijin " )
{
Salary = New Beijinsalary ();
}
ReturnSalary;
}
}
}
The above judgment is just a description of the factory model. This is not good enough. We will introduce it later. When you want to use the computing modules of Shenzhen or Beijing subsidiaries, you can directly use this factory to return the corresponding instance. The Code is as follows:
Code
Using System;
Namespace Consoleapp
{
Class Program
{
Public Static Void Main ( String [] ARGs)
{
// ProgramThe following code calls the Beijing subsidiary:
Isalary salarybj = Salaryfactory. createsalary ( " Beijin " ); // When you need to call the salary calculation process of a subsidiary, transfer the name of the subsidiary
Salarybj. commandsalary ();
/* there are many other codes */
// The following
isalary salarysz = salaryfactory. createsalary ( " Shenzhen " ); // when you need to call the salary calculation process of a subsidiary, transfer the name of the subsidiary to
salarysz. commandsalary ();
Console. Readline ();
}
}
}
In this way, the design of the simple factory model is actually a class (the factory class here) dedicated to returning some similar or similar (all inherit a class or interface class) class.
Advantage: you do not need to be responsible for class instantiation at the place where the call is made. The factory class is used to return the corresponding instance to achieve the instantiation result. Meets the single responsibility requirements of the object-oriented.
Disadvantage: the simple factory mode is created using static methods, so the factory class cannot be inherited. If the factory class needs to be implemented frequently, this class will be very large and is not conducive to maintenance, if necessary, you also need to split the factory class into different small factory classes. For example, create a factory class for each subsidiary, which will be introduced in the next article.