Yesterday afternoon, after reading guoyan19811021Simple factory pattern (see http://www.csdn.net/Develop/read_article.asp? Id = 26635)Later, in order to deepen my impression, I changed the above VB. NET to Delphi, which may not be of great significance, but I still posted it and shared it with you.
First, create a base class cname, as shown below:
Type
Cname = Class
Private
{Public declarations}
Public
{Protected Declarations}
Function getfirstname: string;
Function getlastname: string;
Protected
{Private Declarations}
M_sfirstname: string;
M_slastname: string;
End;
Below are two inheritance classes
Type
Cfirst001 = Class (cname)
Public
{Public declarations}
Constructor create (a_sinput: string );
Protected
{Protected Declarations}
Private
{Private Declarations}
End;
Type
Cfirst002 = Class (cname)
Public
{Public declarations}
Constructor create (a_sinput: string );
Protected
{Protected Declarations}
Private
{Private Declarations}
End;
Simple Factory
Type
Cnamefactory = Class
Public
{Public declarations}
Function getname (a_sinput: string): cname;
Protected
{Protected Declarations}
Private
{Private Declarations}
End;
The following is the implementation of each function.
Function cname. getfirstname: string;
Begin
Result: = m_sfirstname;
End;
Function cname. getlastname: string;
Begin
Result: = m_slastname;
End;
Constructor cfirst001.create (a_sinput: string );
VaR I: integer;
Begin
Inherited create;
// Add code here
I: = pos ('', a_sinput );
If (I> 0) then begin // can find the ''(Space)
M_sfirstname: = copy (a_sinput, 1, I-1 );
M_sfirstname: = trim (m_sfirstname );
M_slastname: = copy (a_sinput, I + 1, length (a_sinput)-I );
M_slastname: = trim (m_slastname );
End
Else begin // can't find the '(Space)
M_sfirstname: = '';
M_slastname: = a_sinput;
End;
End;
Constructor cfirst002.create (a_sinput: string );
VaR
I, J: integer;
Begin
Inherited create;
// Add code here
I: = pos (',', a_sinput );
If (I> 0) then begin // can find the ''(Space)
M_sfirstname: = copy (a_sinput, 1, I-1 );
M_sfirstname: = trim (m_sfirstname );
M_slastname: = copy (a_sinput, I + 1, length (a_sinput)-I );
M_slastname: = trim (m_slastname );
End
Else begin // can't find the '(Space)
M_sfirstname: = '';
M_slastname: = a_sinput;
End;
End;
Function cnamefactory. getname (a_sinput: string): cname;
VaR I: integer;
Begin
I: = pos (',', a_sinput );
If (I> 0) then
Begin
Result: = cfirst002.create (a_sinput );
End
Else begin
Result: = cfirst001.create (a_sinput );
End;
End;
Of course, this alone cannot be run. You also need to add uses to the interface section.
Sysutils; because some functions of the string operation use it.
This completes.
Please contact me for any errors