[Design pattern collation Note one] basics

Source: Internet
Author: User

[Design pattern collation Note one] basics

Reading guidance

[Design pattern collation Note one] basics

[design mode collation Note II] simple Factory mode (easy Factory)

[design mode finishing Note III] Factory mode (Factory)

[design mode collation note Four] abstract Factory mode (Factory)

[Design mode grooming Note Five] Creator mode (Builder)

[Design pattern finishing note Six] Factory mode and creator mode summary

[Design mode grooming note Seven] prototype mode (PROTOTYPE)

[design mode collation note Eight] singleton mode (Singleton)

[Design mode grooming note Nine] appearance mode (facade)

.... Follow-up, including some examples

[/guide]

The scope of the design pattern is very wide, I recently also slightly to tidy up and study a bit, feel every time to sort out what they learned, will find that the knowledge will be more than the previous learning more comprehensive, or scattered in the head. The following example is a large enterprise's calculation of the wage pattern, because there are many subsidiaries around, of course, the method of calculating wages is different.

First define an interface to determine the method of calculating the salary, as follows:

using System;

Namespace ConsoleApp
{
public interface isalary
{
void commandsalary ();
}
}

The following different subsidiaries inherit the Commandsalary method of this interface respectively, the following is the calculation method of two places in Shenzhen and Beijing. The design of two classes is as follows:

Code UsingSystem;

Namespace consoleapp
{
     Public class  shenzhensalary : isalary
    {
         public  void commandsalary ()
         {
             Console.WriteLine ( " This is the Shenzhen subsidiary's payroll calculation Module ");
        }
    }
}

Beijing subsidiary

Code UsingSystem;

Namespace consoleapp
{
     Public class  beijinsalary : isalary
    {
         public  void commandsalary ()
         {
             Console.WriteLine ( " This is the Beijing subsidiary's payroll calculation Module ");
        }
    }
}

To the inside of the basic class has been designed to complete, many people will be in the code to use the new one out, such code may cause difficult to maintain, give people a good mess of feeling. Or, as in the following code, the code is as follows:

Code UsingSystem;

NamespaceConsoleApp
{
ClassProgram
{
PublicStaticvoidMain (String[] args)
{
Isalary Salary=Createsalary ("Beijin");//To call the subsidiary's payroll calculation process, the name of the subsidiary is passed in
Salary.commandsalary ();
Console.ReadLine ();
}

///<summary>
///Return the corresponding instance by the company name passed in
///</summary>
///<param name= "CompanyName" >Name of subsidiary</param>
///<returns></returns>
PrivateStaticIsalary Createsalary (StringCompanyName)
{
Isalary Salary=Null;
If(CompanyName=="ShenZhen")//There are more ways to judge this, and return the corresponding instance by a different name.
{
Salary=NewShenzhensalary ();
}
ElseIf(CompanyName= = "beijin")
{
Salary = new beijinsalary ();
}
return Salary;
}
}
}

This can be called to calculate the wages of the subsidiaries, for small companies, such a design is quite perfect, but for a large company, the subsidiary may have dozens of or even hundreds of, so in the createsalary need more if to judge, this is obviously maintenance is very bad, How do you design it? This is the design pattern to be discussed, and the following article will be the usual factory pattern to write this module.

Note: This is a novice commonly used to write, this has not talked about the factory model, here just said that the class is not good, not in line with the design pattern of the system, first to give a counter example, will introduce the design pattern. Some friends did not see clearly, mark.

(Note: This is the personal learning process of experience, there may be a great mistake, please correct us)

[Design pattern collation Note one] basics

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.