设计模式-原型模式

/*
 * Created by SharpDevelop.
 * User: anson.wu
 * Date: 2007-4-5
 * Time: 14:47
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
using  System;

namespace
 ClonePattern
{
    
class
 MainClass
    {
        
public static void Main(string
[] args)
        {
            ConscretePrototype1 p1
=new ConscretePrototype1("I"
);
            ConscretePrototype1 c1
=
(ConscretePrototype1)p1.Clone();
            Console.WriteLine(c1.id);
            
            ConscretePrototype2 p2
=new ConscretePrototype2("II"
);
            ConscretePrototype2 c2
=
(ConscretePrototype2)p2.Clone();
            Console.WriteLine(c2.id);
            
            Console.Read();
        }        
    }
    
public abstract class
 ProtoType
    {
        
private string
 _id;
        
public string
 id
        {
            
get{return
 _id;}
            
set{_id=
value;}
        }
        
public ProtoType(string
 id)
        {
            _id
=
id;
        }
        
public abstract
 ProtoType Clone();    
    }    
    
public class
 ConscretePrototype1:ProtoType
    {
        
public ConscretePrototype1(string id):base
(id)
        {
            
        }
        
public override
 ProtoType Clone()
        {
            
return (ProtoType)this
.MemberwiseClone();
            
        }        
    }
    
public class
 ConscretePrototype2:ProtoType
    {
        
public ConscretePrototype2(string id):base
(id)
        {
            
        }
        
public override
 ProtoType Clone()
        {
            
return (ProtoType)this
.MemberwiseClone();
            
        }        
    }
}

你可能感兴趣的:(设计模式)