(创建型模式)Prototype 原型模式

 

概述

在软件系统中,有时候面临的产品类是动态变化的,而且这个产品类具有一定的等级结构。这时如果用工厂模式,则与产品等级结构平行的工厂方法类也要随着这种变化而变化,显然不大适合。那么如何封装这种动态的变化?从而是依赖于这些易变对象的客户程序不随着产品类变化?

 

意图

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

 

结构


Prototype模式结构图

生活中的例子

Prototype模式使用原型实例指定创建对象的种类。新产品的原型通常是先于全部产品建立的。这样的原型是被动的,并不参与复制它自己。一个细胞的有丝分裂,产生两个同样的细胞,是一个扮演主动角色复制自己原型的例子,这演示了原型模式。一个细胞分裂,产生两个同样基因型的细胞。换句话说,细胞克隆了自己。


使用细胞分裂例子的Prototype模式对象图

原型模式解说

我们考虑这样一个场景,假定我们要开发一个调色板,用户单击调色板上任一个方块,将会返回一个对应的颜色的实例,下面我们看看如何通过原型模式来达到系统动态加载具体产品的目的。

很自然,我们利用OO的思想,把每一种颜色作为一个对象,并为他们抽象出一个公用的父类,如下图:



实现代码:

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:Color类
    
/// 编写:Kalen_Chen
    
/// 日期:2007年5月10日
    
/// </summary>

    public abstract class Color
    
{
        
public abstract void Display();
    }

}


using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:GreenColor类
    
/// 编写:Kalen_Chen
    
/// 日期:2007年5月10日
    
/// </summary>

    public class GreenColor:Color
    
{
        
public override void Display()
        
{
            Console.Write(
"Green's RGB Values are:0,255,0");
        }

    }

}


using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:RedColor类
    
/// 编写:Kalen_Chen
    
/// 日期:2007年5月10日
    
/// </summary>

    public class RedColor:Color
    
{
        
public override void Display()
        
{
            Console.WriteLine(
"Red's RGB Values are:255,0,0");
        }

    }

}


客户程序需要某一种颜色的时候,只需要创建对应的具体类的实例就可以了。但是这样我们并没有达到封装变化点的目的,也许你会说,可以使用工厂方法模式,为每一个具体子类定义一个与其等级平行的工厂类,那么好,看一下实现:



实现代码:

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:ColorFactory类
    
/// 编写:Kalen_Chen
    
/// 日期:2007年5月10日
    
/// </summary>

    public abstract class ColorFactory
    
{
        
public abstract Color Create();
    }

}


using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:RedFactory类
    
/// 编写:Kalen_Chen
    
/// 日期:2007年5月10日
    
/// </summary>

    public class RedFactory:ColorFactory
    
{
        
public override Color Create()
        
{
            
return new RedColor();
        }

    }

}


using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:GreenFactory类
    
/// 编写:Kalen_Chen
    
/// 日期:2007年5月10日
    
/// </summary>

    public class GreenFactory:ColorFactory
    
{
        
public override Color Create()
        
{
            
return new GreenColor();
        }

    }

}


实现了这一步之后,可以看到,客户程序只要调用工厂方法就可以了。似乎我们用工厂方法模式来解决是没有问题的。但是,我们考虑的仅仅是封装了new变化,而没有考虑颜色的数量是不断变化的,甚至可能是在程序运行的过程中动态增加和减少的,那么用这种方法实现,随着颜色数量的不断增加,子类的数量会迅速膨大,导致子类过多,显然用工厂方法模式有些不大适合。

进一步思考,这些Color子类仅仅在初始化的颜色对象类别上有所不同。添加一个ColorTool这样的类,来参数化它的实例,而这些实例是由Color支持和创建的。我们让ColorTool通过克隆或者拷贝一个Color子类的实例来创建新的Color,这个实例就是一个原型。如下图所示:



实现代码:

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:ColorPrototype类
    
/// 编写:Kalen_Chen
    
/// 日期:
2007年5月10日
    /// </summary>
    public abstract class ColorPrototype
    
{
        
public abstract ColorPrototype Clone();
    }

}


using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:ColorPrototype,实现浅拷贝
    
/// 编写:Kalen_Chen
    
/// 日期:
2007年5月10日
    /// </summary>
    public class ConcteteColorPrototype:ColorPrototype
    
{
        
private int _red, _green, _blue;

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


        
/// <summary>
        
/// 功能:实现浅拷贝
        
/// </summary>
        
/// <returns>返回一个ColorPrototype对象</returns>

        public override ColorPrototype Clone()
        
{
            
return (ColorPrototype)this.MemberwiseClone();
        }


        
public void Display(string _colorname)
        
{
            Console.WriteLine(
"{0}'s RGB Values are: {1},{2},{3}", _colorname, _red, _green, _blue);
        }


    }

}


using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Collections;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:ColorManager类
    
/// 编写:Kalen_Chen
    
/// 日期:
2007年5月10日
    /// </summary>
    public class ColorManager
    
{
        Hashtable colors 
= new Hashtable();

        
public ColorPrototype this[string name]
        
{
            
get
            
{
                
return (ColorPrototype)colors[name];
            }


            
set
            
{
                colors.Add(name, value);
            }


        }

    }

}


现在我们分析一下,这样带来了什么好处?首先从子类的数目上大大减少了,不需要再为每一种具体的颜色产品而定一个类和与它等级平行的工厂方法,而ColorTool则扮演了原型管理器的角色。再看一下为客户程序的实现:

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  PrototypePattern
{
       
/// <summary>
    
/// 功能:App类
    
/// 编写:Kalen_Chen
    
/// 日期:2007年5月10日
    
/// </summary>

    public class App
    
{
        
public static void Main(string[] args)
        
{
            ColorManager colormanager 
= new ColorManager();

            
//初始化颜色
            colormanager["red"= new ConcteteColorPrototype(25500);
            colormanager[
"green"= new ConcteteColorPrototype(02550);
            colormanager[
"blue"= new ConcteteColorPrototype(00255);
            colormanager[
"angry"= new ConcteteColorPrototype(255540);
            colormanager[
"peace"= new ConcteteColorPrototype(128211128);
            colormanager[
"flame"= new ConcteteColorPrototype(2113420);

            
//使用颜色
            string colorName = "red";
            ConcteteColorPrototype c1 
= (ConcteteColorPrototype)colormanager[colorName].Clone();
            c1.Display(colorName);

            colorName 
= "peace";
            ConcteteColorPrototype c2 
= (ConcteteColorPrototype)colormanager[colorName].Clone();
            c2.Display(colorName);

            colorName 
= "flame";
            ConcteteColorPrototype c3 
= (ConcteteColorPrototype)colormanager[colorName].Clone();
            c3.Display(colorName);

            Console.ReadLine();
        }

    }

}


可以看到,客户程序通过注册原型实例就可以将一个具体产品类并入到系统中,在运行时刻,可以动态的建立和删除原型。最后还要注意一点,在上面的例子中,用的是浅表复制。如果想做深复制,需要通过序列化的方式来实现。经过了上面的分析之后,我们再来思考下面的问题:

<!--[if !supportLists]-->1.         <!--[endif]-->为什么需要Prototype模式?

引入原型模式的本质在于利用已有的一个原型对象,快速的生成和原型对象一样的实例。你有一个A的实例aA a = new A();现在你想生成和car1一样的一个实例b,按照原型模式,应该是这样:A b = a.Clone();而不是重新再new一个A对象。通过上面这句话就可以得到一个和a一样的实例,确切的说,应该是它们的数据成员是一样的。Prototype模式同样是返回了一个A对象而没有使用new操作。

<!--[if !supportLists]-->2.         <!--[endif]-->引入Prototype模式带来了什么好处?

可以看到,引入Prototype模式后我们不再需要一个与具体产品等级结构平行的工厂方法类,减少了类的构造,同时客户程序可以在运行时刻建立和删除原型。

<!--[if !supportLists]-->3.         <!--[endif]-->Prototype模式满足了哪些面向对象的设计原则?

依赖倒置原则:上面的例子,原型管理器(ColorManager)仅仅依赖于抽象部分(ColorPrototype),而具体实现细节(ConcteteColorPrototype)则依赖于抽象部分(ColorPrototype),所以Prototype很好的满足了依赖倒置原则。



通过序列化实现深拷贝

要实现深拷贝,可以通过序列化的方式。抽象类及具体类都必须标注为可序列化的[Serializable],上面的例子加上深拷贝之后的完整程序如下:

using  System;
using  System.Collections;
using  System.IO;
using  System.Runtime.Serialization;
using  System.Runtime.Serialization.Formatters.Binary;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:ColorPrototype类
    
/// 编写:Kalen_Chen
    
/// 日期:
2007年5月10日
    /// </summary>
    [Serializable]
    
public abstract class ColorPrototype
    
{
        
public abstract ColorPrototype Clone(bool Deep);
    }

}


using  System;
using  System.Collections;
using  System.IO;
using  System.Runtime.Serialization;
using  System.Runtime.Serialization.Formatters.Binary;

namespace  PrototypePattern
{
       
/// <summary>
    
/// 功能:ConcteteColorPrototype类
    
/// 编写:Kalen_Chen
    
/// 日期:2007年5月10日
    
/// </summary>

    [Serializable]
    
public class ConcteteColorPrototype : ColorPrototype
    
{
        
private int _red, _green, _blue;

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


        
public override ColorPrototype Clone(bool Deep)
        
{
            
if (Deep)
            
{
                
return CreateDeepCopy();
            }

            
else
            
{
                
return (ColorPrototype)this.MemberwiseClone();
            }

        }


        
public ColorPrototype CreateDeepCopy()
        
{
            ColorPrototype colorPrototype;
            MemoryStream memoryStream 
= new MemoryStream();
            BinaryFormatter formatter 
= new BinaryFormatter();
            formatter.Serialize(memoryStream, 
this);
            memoryStream.Position 
= 0;
            colorPrototype 
= (ColorPrototype)formatter.Deserialize(memoryStream);
            
return colorPrototype;
        }



        
public ConcteteColorPrototype Create(int red, int green, int blue)
        
{
            
return new ConcteteColorPrototype(red, green, blue);
        }


        
public void Display(string _colorname)
        
{
            Console.WriteLine(
"{0}'s RGB Values are: {1},{2},{3}", _colorname, _red, _green, _blue);
        }


    }

}


using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Collections;

namespace  PrototypePattern
{
    
/// <summary>
    
/// 功能:ColorManager类
    
/// 编写:Kalen_Chen
    
/// 日期:
2007年5月10日
    /// </summary>
    public class ColorManager
    
{
        Hashtable colors 
= new Hashtable();

        
public ColorPrototype this[string name]
        
{
            
get
            
{
                
return (ColorPrototype)colors[name];
            }


            
set
            
{
                colors.Add(name, value);
            }


        }

    }

}


using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  PrototypePattern
{
       
/// <summary>
    
/// 功能:App类
    
/// 编写:Kalen_Chen
    
/// 日期:2007年5月10日
    
/// </summary>

    public class App
    
{
        
public static void Main(string[] args)
        
{
            ColorManager colormanager 
= new ColorManager();

            
//初始化颜色
            colormanager["red"= new ConcteteColorPrototype(25500);
            colormanager[
"green"= new ConcteteColorPrototype(02550);
            colormanager[
"blue"= new ConcteteColorPrototype(00255);
            colormanager[
"angry"= new ConcteteColorPrototype(255540);
            colormanager[
"peace"= new ConcteteColorPrototype(128211128);
            colormanager[
"flame"= new ConcteteColorPrototype(2113420);

            
//使用颜色
            string colorName = "red";
            ConcteteColorPrototype c1 
= (ConcteteColorPrototype)colormanager[colorName].Clone(false);
            c1.Display(colorName);

            colorName 
= "peace";
            ConcteteColorPrototype c2 
= (ConcteteColorPrototype)colormanager[colorName].Clone(true);
            c2.Display(colorName);

            colorName 
= "flame";
            ConcteteColorPrototype c3 
= (ConcteteColorPrototype)colormanager[colorName].Clone(true);
            c3.Display(colorName);

            Console.ReadLine();
        }

    }

}


实现要点

<!--[if !supportLists]-->1.       <!--[endif]-->使用原型管理器,体现在一个系统中原型数目不固定时,可以动态的创建和销毁,如上面举的调色板的例子。

<!--[if !supportLists]-->2.       <!--[endif]-->实现克隆操作,在.NET中可以使用Object类的MemberwiseClone()方法来实现对象的浅表拷贝或通过序列化的方式来实现深拷贝。

<!--[if !supportLists]-->3.       <!--[endif]-->Prototype模式同样用于隔离类对象的使用者和具体类型(易变类)之间的耦合关系,它同样要求这些“易变类”拥有稳定的接口。

 

效果

<!--[if !supportLists]-->1.       <!--[endif]-->它对客户隐藏了具体的产品类,因此减少了客户知道的名字的数目。

<!--[if !supportLists]-->2.       <!--[endif]-->Prototype模式允许客户只通过注册原型实例就可以将一个具体产品类并入到系统中,客户可以在运行时建立和删除原型。

<!--[if !supportLists]-->3.       <!--[endif]-->减少了子类构造,Prototype模式是克隆一个原型而不是请求工厂方法创建一个,所以它不需要一个与具体产品类平行的Creater类层次。

<!--[if !supportLists]-->4.       <!--[endif]-->Prototype模式具有给一个应用软件动态加载新功能的能力。由于Prototype的独立性较高,可以很容易动态加载新功能而不影响老系统。

<!--[if !supportLists]-->5.       <!--[endif]-->产品类不需要非得有任何事先确定的等级结构,因为Prototype模式适用于任何的等级结构。

<!--[if !supportLists]-->6.       <!--[endif]-->Prototype模式的最主要缺点就是每一个类必须配备一个克隆方法。而且这个克隆方法需要对类的功能进行通盘考虑,这对全新的类来说不是很难,但对已有的类进行改造时,不一定是件容易的事。

 

适用性

在下列情况下,应当使用Prototype模式:

<!--[if !supportLists]-->1.       <!--[endif]-->当一个系统应该独立于它的产品创建,构成和表示时;

<!--[if !supportLists]-->2.       <!--[endif]-->当要实例化的类是在运行时刻指定时,例如,通过动态装载;

<!--[if !supportLists]-->3.       <!--[endif]-->为了避免创建一个与产品类层次平行的工厂类层次时;

<!--[if !supportLists]-->4.       <!--[endif]-->当一个类的实例只能有几个不同状态组合中的一种时。建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些。

 

总结

Prototype模式同工厂模式,同样对客户隐藏了对象的创建工作,但是,与通过对一个类进行实例化来构造新对象不同的是,原型模式是通过拷贝一个现有对象生成新对象的,达到了“隔离对象的使用者和具体类型(易变类)之间的耦合关系”的目的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值