Yesterday afternoon, after watching the simple factory model of guysan19811021 (see http://www.net/develop/read_article.asp?id=26635), in order to deepen the impression, I put the above VB.NET is changed to Delphi, not necessarily, but it is still posted, sharing with you.
First build a base class CNAME, as follows
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 is 2 inheritance classes
Type cfirst001 = class (cname) public {public declarations} constructor create (a_sinput: string); protected {protected declarations} private {private declarations}
Type cfirst002 = class (cname) public {public declarations} constructor create (a_sinput: string); protected {protected declarations} private {private declarations}
Simple factory
Type cnamefactory = class public {public declarations} function getname (a_sinput: string): cname; protected {protected declarations} private {private declarations}
The following is a part of the implementation of each function
Function cname.getfirstname: string; begin result: = m_sfirstname;
Function cname.getlastname: string; begin result: = m_slastname;
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_SIINPUT; END;
Constructor cfirst002.create (A_SIINPUT: STRING); VAR I, J: Inteder; Begin inherited Creger; // 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;
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); Else Begin Result: = cfirst001.create (a_sinput); end;
Of course, only this is unable to run, but also need to add USESSYSUTILS in the interface; because the part of the String operation is used.
This is done.
If there is any mistake, please contact me.