This design
pattern is come under Creational Pattern in .Net. It is pattern is one of the simplest
design patterns. Specify the kinds of objects to create using a prototypical
instance, and create new objects by copying this prototype. It will avoid harm
of ‘New’ keyword.
public ICards Clone()
OutPut:
The Components
of Prototype Design Pattern
Prototype Interface:
Represents way for Clone the objects details.
ConcreatePrototype
: Represents the implementation of Prototype interface and allow to clone the
object itself.
Real Time Usage
Assume that I am
one of the member of a Club . Club maintain all the details of members. While
joining I have created membership card. After some days my friend also plan to
join this club. My friend is living the same flat except name & number are
same. In this scenario Prototype Design
pattern will come in to picture. Admin can clone my details and just change
name & Phone & and save it new contact. One more scenario like club
storing the Business details. If I have start new business then admin can clone
my existing Business details and create new Business details.
Example
/// <summary>
/// Prototype
interface implementation
/// </summary>
public interface
ICards
{
ICards
Clone();
void
ShowDetail();
}
/// <summary>
///
ConcretePrototype1 Implementation
/// </summary>
public class
MemberCard : ICards
{
public string Name { get; set; }
public Int64 PhoneNumber { get;
set; }
public string Address { get;
set; }
{
return
(ICards)MemberwiseClone();
}
public void ShowDetails()
{
Console.WriteLine("...............................");
Console.WriteLine("Name :"
+ Name);
Console.WriteLine("PhoneNumber
:" + PhoneNumber);
Console.WriteLine("Address
:" + Address);
Console.WriteLine("...............................");
}
}
/// <summary>
///
ConcretePrototype2 Implementation
/// </summary>
public class
BusinessCard : ICards
{
public string Name { get; set; }
public Int64 PhoneNumber { get;
set; }
public string Address { get;
set; }
public ICards Clone()
{
return
(ICards)MemberwiseClone();
}
public void ShowDetails()
{
Console.WriteLine("...............................");
Console.WriteLine("Name :"
+ Name);
Console.WriteLine("PhoneNumber
:" + PhoneNumber);
Console.WriteLine("Address
:" + Address);
Console.WriteLine("...............................");
}
}
/// <summary>
///
ConcretePrototype3 Implementation
/// </summary>
public class
FamilyCard : ICards
{
public string Name { get; set; }
public Int64 PhoneNumber { get;
set; }
public string Address { get;
set; }
public string SpouseName { get;
set; }
public ICards Clone()
{
return
(ICards)MemberwiseClone();
}
public void ShowDetails()
{
Console.WriteLine("...............................");
Console.WriteLine("Name :"
+ Name);
Console.WriteLine("PhoneNumber
:" + PhoneNumber);
Console.WriteLine("Address
:" + Address);
Console.WriteLine("Spouse Name
:" + SpouseName);
Console.WriteLine("...............................");
}
}
Create Call for
this
MemberCard membercard = new MemberCard();
membercard.Name = "Santhosh";
membercard.PhoneNumber =
9962513322;
membercard.Address = "B 101,Marg Pushpadruma";
MemberCard
newMembercard = (MemberCard)membercard.Clone();
newMembercard.Name = "mrs Santhosh";
newMembercard.PhoneNumber =
9988776655;
membercard.ShowDetails();
newMembercard.ShowDetails();
BusinessCard
businessCard = new BusinessCard();
businessCard.Name = "Santhosh";
businessCard.PhoneNumber =
89374972;
businessCard.Address = "MindTree Chennai";
BusinessCard
newbusinessCard = (BusinessCard)businessCard.Clone();
newbusinessCard.Address = "BOA";
businessCard.ShowDetails();
newbusinessCard.ShowDetails();
No comments:
Post a Comment