--- Boring split line ---
In the object-orientedProgramWe often see a simple factory pattern ). Maybe you don't realize that you have been using the simple factory model. It doesn't matter. In many cases, we only need to use it.
The simple factory mode returns instances of one of several possible classes based on the data provided to it. Generally, the classes returned by this method have a common parent class and a common method, but each method executes different tasks and is optimized based on different data. The simple factory model does not actually belong to 23 gof models, but it can be used as a guide to learning the factory method model.
1. Define a parent class first
// Parent class Public Class Namer { // Define members again Protected String Frname, lname; // Return fname Public String Getfrname (){ Return Frname ;} // Returns lname Public String Getlname (){ Return Lname ;}}
2. Define two child-Derived classes
The two Derived classes inherit namer. The constructor of both classes divides the name into two parts. It is also agreed that all content after the last space belongs to frname. In subclass 1, The Delimiter is a space, and in subclass 2, the separator is a comma.
// Subclass 1 Public Class Firstlast: namer { Public Firstlast (string name ){ Int I = Name. Trim (). indexof ( " " ); If (I> 0 ) {Frname = Name. substring ( 0 , I). Trim (); lname = Name. substring (I + 1 ). Trim ();} Else {Lname = Name; frname = "" ;}}} // Subclass 2 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 ). Trin ();} Else {Lname = Name; frname = "" ;}}}
In both cases, we store the split name in the protection variables lname and frname in the base class namer.
3. Construct a simple factory
The class is ready, and a simple factory needs to be constructed below. The factory is very simple. When a comma is detected in the name, a lastfirst object is returned; otherwise, the firstlast object is returned. Below isCodeExample:
// Simple Factory Public Class Namefactory { // Constructor Public Namefactory {} // Return different classes based on judgment Public Namer getname ( String Name ){ Int I = Name. indexof ( " , " ); If (I> 0 ){ Return New Lastfirst (name );} Else { Return New Firstlast (name );}}}
4. Factory
Assume that you need to enter a name in the input text. In response to the button click event, enter the last name and name in the corresponding text box respectively. The following is a sample code:
//FactoryPrivate VoidBtnok_click (ObjectSender, system. eventargs e) {namer nm=Namefact. geyname (txtname. Text); txtfirst. Text=Nm. getfrname (); txtlast. Text=Nm. getlname ();}
When using this function, we do not need to know which derived class is used. The factory will make a choice for us.
Structure Diagram
Summary
A simple factory can return instances of classes with the same method. They can be instances of different derived subclasses, or in practice, they can only share classes with the same interface. Regardless of the form, the methods in these instances must be the same and can be used alternately.
Learning, from a simple start.
All code
Simple Factory
// Parent class Public Class Namer { // Define members again Protected String Frname, lname; // Return fname Public String Getfrname (){ Return Frname ;} // Returns lname Public String Getlname (){ Return Lname ;}} // Subclass 1 Public Class Firstlast: namer { Public Firstlast (string name ){ Int I = Name. Trim (). indexof ( " " ); If (I> 0 ) {Frname = Name. substring ( 0 , I). Trim (); lname = Name. substring (I + 1 ). Trim ();} Else {Lname = Name; frname = "" ;}}} // Subclass 2 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 ). Trin ();} Else {Lname = Name; frname = "" ;}}} // Simple Factory Public Class Namefactory { // Constructor Public Namefactory {} // Return different classes based on judgment Public Namer getname ( String Name ){ Int I = Name. indexof ( " , " ); If (I> 0 ){ Return New Lastfirst (name );} Else { Return New Firstlast (name );}}} // Factory Private Void Btnok_click ( Object Sender, system. eventargs e) {namer nm = Namefact. geyname (txtname. Text); txtfirst. Text =Nm. getfrname (); txtlast. Text = Nm. getlname ();}
References: C # Design Patterns