NET设计模式5--原型模式(Prototype Pattern)

ConcretePrototypeA一. 原型模式

  原型模式:用原型实例指定创建对象的种类,并通过拷贝这些原型创建的对象。

浅复制和深复制

  浅复制:被复制的所有变量都还有与原来对象相同的值,而所有的对其他对象引用都仍然指向原来的对象。

  深复制:把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。

  Net命名空间System提供一个IConeable接口,此接口只有一个方法Clone(),只需要实现这个接口就可以实现原型模式。

  二. 原型模式分析

  1.结构

  Prototype类:原型类Clone()就去:克隆自身的接口。

  ConcretePrototypeA、ConcretePrototypeB类:原型类的具体实现,克隆一个自身的操作。

  2.代码

  

 

1.原型抽象类PrototypeClass
代码
 /// <summary>
    
/// 抽象原型模式类
    
/// </summary>
    public abstract class PrototypeClass
    {
        
private string _id;

        
public string ID
        {
            
get
            {
                
return _id;
            }
            
set
            {
                _id 
= value;
            }
        }

        
public PrototypeClass(string id)
        {
            
this.ID = id;
        }

        
public abstract PrototypeClass Clone();

    }
 
代码
2 具体实现类ConcretePrototypeA和ConcretePrototypeB


    
/// <summary>
    
/// 原型类的具体实现ConcretePrototypeA
    
/// </summary>
    public class ConcretePrototypeA : PrototypeClass
    {
        
public ConcretePrototypeA(string id)
            : 
base(id)
        {
 
        }
        
/// <summary>
        
/// 返回一个浅拷贝
        
/// </summary>
        
/// <returns></returns>
        public override PrototypeClass Clone()
        {
            
return (PrototypeClass)this.MemberwiseClone();
        }
    }

    
/// <summary>
    
/// 原型类的具体实现ConcretePrototypeB
    
/// </summary>
    public class ConcretePrototypeB : PrototypeClass
    {
        
public ConcretePrototypeB(string id)
            : 
base(id)
        { }

        
/// <summary>
        
/// 返回一个浅拷贝
        
/// </summary>
        
/// <returns></returns>
        public override PrototypeClass Clone()
        {
            
return (PrototypeClass)this.MemberwiseClone();
        }
    }

 

 
3.客户端代码
代码
public static void Main(string[] args)
        {
            PrototypeClass pA 
= new ConcretePrototypeA("A");
            PrototypeClass cA 
= pA.Clone() as ConcretePrototypeA;
            Console.WriteLine(
"Cloned:" + cA.ID);

            ConcretePrototypeB pB 
= new ConcretePrototypeB("B");
            ConcretePrototypeB cB 
= (ConcretePrototypeB)pB.Clone();
            Console.WriteLine(
"Cloned:" + cB.ID);

            Console.ReadKey();
        }

 

4.实现

 

 三. 实例分析

   1.说明

    颜色索引器存储多种颜色值,从颜色索引器中克隆客户需要几种颜色。

  ColorPrototype:原型模式抽象类

  Color:原型模式抽象类的具体实现,Clone方法的实现,克隆自身的操作

    ColorManager:颜色索引器  

  2.代码实现

  

代码
(1) 原型模式抽象类ColorPrototype及其具体实现类Color

 
#region 原型模式抽象类ColorPrototype及其具体实现类Color
    
/// <summary>
    
/// 原型模式抽象类ColorPrototype
    
/// </summary>
    public abstract class ColorPrototype
    {
        
public abstract ColorPrototype Clone();
    }

    
/// <summary>
    
/// 具体实现类Color
    
/// </summary>
    public class Color : ColorPrototype
    {
        
private int _red;
        
private int _green;
        
private int _blue;

        
public Color(int red, int green, int blue)
        {
            
this._red = red;
            
this._green = green;
            
this._blue = blue;

        }

        
public override ColorPrototype Clone()
        {
            Console.WriteLine(
"Cloning color RGB:{0,3},{1,3},{2,3}",_red,_green,_blue);
            
return this.MemberwiseClone() as ColorPrototype;
        }

    }
    
#endregion
 
(2)颜色索引器类
代码
 /// <summary>
    
/// 颜色索引器类
    
/// </summary>
    public class ColorManager
    {
        
//Fields
        Hashtable ht = new Hashtable();

        
//Indexers
        public ColorPrototype this[string name]
        {
            
get
            {
                
return (ColorPrototype)ht[name];
            }
            
set
            {
                ht.Add(name, value);
            }
        }
    }

 

(3)客户端代码
代码
 public static void Main(string[] args)
        {
            
            ColorManager cm 
= new ColorManager();
            
//初始化标准的RGB颜色
            cm["red"= new Color(25500);
            cm[
"green"= new Color(02550);
            cm[
"blue"= new Color(00255);

            
//添加个性的颜色
            cm["angry"= new Color(255540);
            cm[
"peace"= new Color(128211128);
            cm[
"flame"= new Color(2113420);

            
//克隆颜色
            Color c1 = cm["red"].Clone() as Color;
            Color c2 
= cm["peace"].Clone() as Color;
            Color c3 
= cm["flame"].Clone() as Color;
            Console.ReadKey();           
        }

(4)运行结果

 

四. 总结

  当一个对象生成不是通过NEW而是通过复制旧对象的时候,可以考虑使用原型模式。

posted @ 2010-09-20 17:24  Jasmines  阅读(246)  评论(0编辑  收藏  举报