Object-Oriented Programming Design Model-simple factory model (the simplest and clearest example in History)

Source: Internet
Author: User

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 isCodeExample:

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)
{< br> int I = Name. indexof (',');
if (I> 0)
{< br> return New lastname (name );
}< br> else
{< br> return New fristname (name);
}< BR >}

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;

 

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.