Simple factory pattern Delphi code

Source: Internet
Author: User

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

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.