One of the Design Patterns in. Net: factory mode --> factory Mode

Source: Internet
Author: User

Design Patterns Overview

The design pattern is essentially a rule. In terms of form, it can be divided into creation type, structure type, and behavior type.

The application of design patterns is to implement several principles in software design. One of the important principles is to reduce the degree of coupling between modules. To ensure this purpose, when designing a class, we need to address the interface rather than implement it. (Programming to an interface, not an implementation) only focus on class interfaces. During programming, You can implement a simple interface for other modules to call. When you use a class, you only work on the interface. You do not care about the specific implementation or the specific type. This is also in line with the rules of human understanding of the world. Generally, people always first understand the general situation of one thing. For example, we first understand the general functions of a TV, then you can understand how each function is implemented.

Implementation is not provided at the beginning, just to maximize implementation in the future.

The design pattern is not restricted by the language and is easier to implement using. Net or Java.

Factory)

The factory mode is a creation mode (creational ). Singleton also applies to the creation mode, which will be available later.

Key points of the factory model:

1: There is a factory for creating objects;

2: The caller obtains some objects from the factory;

3: The factory determines how to create objects;

4: the customer does not know how the object is generated.

For example, the following Class View:

The namer object is the base class of firstfirst and lastfirst. When you call the namer class, you can use the getname method of namefactory to obtain the specific object instead of new namer class or its subclass. In this way, users do not have to worry about which namer they are using and which method they are calling. The user only works for the namer, rather than the specific type. In actual engineering, the constructor of the sub-classes of the namer class can be opened only to namefactory, further limiting programmers.

C # The Code is as follows:

1: namer implementation

Using system;

 

Namespace namefactory

{

/// <Summary>

/// Summary description for namer.

/// </Summary>

// Base class for getting split names

Public class namer {

// Parts stored here

Protected string frname, lname;

// Return first name

Public String getfrname ()
{

Return frname;

}

// Return last name

Public String getlname ()
{

Return lname;

}

}

}

2: Implementation of the firstfirst class

Using system;

Namespace namefactory

{

/// <Summary>

/// Summary description for firstfirst.

/// </Summary>

Public class firstfirst: namer

{

Public firstfirst (string name)

{

Int I = Name. indexof ("");

If (I> 0 ){

Frname = Name. substring (0, I). Trim ();

Lname = Name. substring (I + 1). Trim ();

}

Else {

Lname = Name;

Frname = "";

}

}

}

}

3: Implementation of the lastfirst class

Using system;

Namespace namefactory

{

/// <Summary>

/// Summary description for lastfirst.

/// </Summary>

Public class lastfirst: namer

{

Public lastfirst (string name ){

Int I = Name. indexof (",");

If (I> 0 ){

Lname = Name. substring (0, I );

Frname = Name. substring (I + 1). Trim ();

}

Else {

Lname = Name;

Frname = "";

}

}

}

}

4: namefactory, factory implementation

Using system;

Namespace namefactory

{

/// <Summary>

/// Summary description for namefactory

/// </Summary>
Public class namefactory {

Public namefactory (){}

Public namer getname (string name ){

Int I = Name. indexof (",");

If (I> 0)

Return new lastfirst (name );

Else

Return new firstfirst (name)

}

}

}

5: caller, a form,

The response Event code of the button is as follows:

Private void btcompute_click (Object sender, system. eventargs E)
{

Namer Nm = namefact. getname (txname. Text );

Txfirst. Text = Nm. getfrname ();

Txlast. Text = Nm. getlname ();

}

The program is not complex and it doesn't matter if it runs or not. The key is to understand the role of the factory: It hides the creation details of the namer class, and the caller never knows which class he created, you do not need to worry about which subclass of the method is called. If the requirements of future programs are changed, for example, if a person in a country has not only the first name and last name, but also the mid name, it can be easily expanded to add a sub-class of namer, modify the factory. The caller does not even know that a new namer type is added.

Practical Application

A simple example: multiple databases, Oracle, SQL Server, and Sybase, are required in the project to create their own connection and query operations for these databases, these classes have a common base class baseconn. You can create a connfactory class to generate a specific class based on different situations. Callers do not have to worry about who they are calling. This greatly simplifies the Business Code.

There are many practical examples.

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.