After work, I found an easy-to-understand example of a simple factory model while reading the materials. I tried it myself and felt that I was not familiar with this design model,
You will immediately know what is going on.
The simple factory mode returns instances of one of several possible classes based on the data provided to it. Generally, the class it returns has a common class and a common method,
However, the tasks executed by each method are different and optimized based on different data.
The simple factory mode is a creation mode, also called the static factory method mode, but not one of the 23 gof design modes.
The following code is used as an example:
Public class getname
{
Private string _ fname, _ lname;
Public String fname {Get; set ;}
Public String lname {Get; set ;}
}
Defines a class and returns the surname and name of the person with spaces in the middle of the name.
Public class fristname: getname
{
Public fristname (string name)
{
Int I = Name. Trim (). indexof ('');
If (I> 0)
{
Fname = Name. substring (0, I). Trim ();
Lname = Name. substring (I + 1). Trim ();
}
Else
{
Fname = Name;
Lname = "";
}
}
}
Defines a class and returns the last name and name of the person with a comma in the middle of the name.
Public class lastname: getname
{
Public lastname (string name)
{
Int I = Name. Trim (). indexof (',');
If (I> 0)
{
Lname = Name. substring (0, I). Trim ();
Fname = Name. substring (I + 1). Trim ();
}
Else
{
Lname = Name;
Fname = "";
}
}
}
The following is a simple factory mode class:
Public class namefac
{
Public namefac ()
{}
Public static getname (string name)
{
Int I = Name. indexof (',');
If (I> 0)
{
Return new lastname (name );
}
Else
{
Return new fristname (name );
}
}
}
All business processes are coupled through a namefac factory class. When using the interface layer, you only need to substitute the factory class to return the result.
Getname GN = namefac. getname ("Kelly Chen ");
Richtextbox1.text = GN. fname + "," + GN. lname;