Design mode in .NET 1: Factory mode

xiaoxiao2021-03-06  107

Design mode overview

Design patterns are essentially a rule, from the form, divided into creation, structural, behavior.

Design mode applications are to achieve several principles in software design, one of which is: reducing the degree of coupling between modules. In order to ensure this purpose, when designing a class, you want to interface, not implementation. (Programming to an interface, not an importation) is designed to care for the interface of the class. When programming, you can implement a simple interface, and the two ordered modules are called. When using a class, only the interface works, does not care about the specific type, and does not care about the specific type. This is also in line with the laws of human being aware of the world. Generally speaking, people always know about a matter of things. For example, we know the probably functionality of a TV set, and then understand how each function is implemented.

Do not provide implementation at the beginning, it is to achieve maximum implementation in order.

The design pattern is not limited by language, and it is easier to implement using .NET or Java.

Factory mode (Factory)

Factory model belongs to a creation mode. Also belong to the creation mode, there is a single mode (Singleton), and there will be a chance to say.

The main points of the factory model:

1: There is a factory that creates an object;

2: The caller has obtained certain objects from the factory;

3: Decide how to create an object by the factory;

4: Customers don't know how objects are generated. For example, the following class view:

The Namer object is the base class of FirstFirst and LastFirst. When the user calls the Namer class, it is not directly New from the NAMER class or his subclass, but uses the NameFactory GetName method to get the specific object. This does not have to care which Namer is being used in the user, which method is being called. Users are only working for NAMER without having to care about the specific type. In the actual engineering, the constructor of the NAMER class can be opened only to NameFactory, further restricting programmers.

The C # code is as follows:

1: Realization of Namer

using System; namespace NameFactory {///

/// Summary description for Namer /// // 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

using System; namespace NameFactory {///

/// Summary description for FirstFirst /// 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: Realization of Lastfirst

using System; namespace NameFactory {///

/// Summary description for LastFirst /// 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: Realization of NameFactory, factory

using System; namespace NameFactory {///

/// Summary description for NameFactory /// 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: The caller, a form, as shown:

The response 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 (); txlaast.text = nm.getlname ();} The program is not complicated It doesn't matter if it is easy to run. The key is to figure out the role of Factory: He hides the creation details of the Namer class. The caller always does not know which class he created, and does not need to care what the method he calls. Which one Child. If the demand for future procedures is changed, such as: some country not only has first name and last name, but also Mid Name, it can be easily expanded, add a NAMER's subclass, modify Factory. The caller does not even know that there is a type of NAMER.

Actual application

To give a simple example: A variety of databases, Oracle, SQL Server, Sybase are required in the project, which establishes a class of individual connections and query operations for these databases, which have a common base class BaseConn. A ConnFactory class can be created to produce a specific class according to different situations. The caller does not have to care about who he calls. Greatly simplifies business code.

There are still a lot of practical use cases.

转载请注明原文地址:https://www.9cbs.com/read-97245.html

New Post(0)