Net设计模式实例之原型模式( Prototype Pattern)(2)

简介:

四.原型模式实例分析(Example

1、场景

颜色索引器存储多种颜色值,从颜色索引器中克隆客户需要几种颜色。结构 如下图所示
 
ColorManager :颜色索引器
ColorPrototype :原型模式抽象类
Color :原型模式抽象类的具体实现, Clone 方法的实现,克隆自身的操作

2、代码

1 原型模式抽象类ColorPrototype 及其具体实现类Color
///   <summary>
///   原型模式抽象类
///   </summary>
public  abstract class ColorPrototype
{
    public abstract ColorPrototype  Clone();
}
///   <summary>
///   具体实现类
///   </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;
     }
    /// <summary>
    ///  实现浅复制
    /// </summary>
    /// <returns></returns>
    public override ColorPrototype  Clone()
     {
        Console .WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}" , _red, _green, _blue);
        return this .MemberwiseClone() as ColorPrototype ;
     }
}
 
3 、客户端代码
static  void Main (string [] args)
{
    ColorManager  colormanager = new ColorManager ();
 
    // 初始化标准的red green blue 颜色。
     colormanager["red" ] = new Color (255, 0, 0);
     colormanager["green" ] = new Color (0, 255, 0);
     colormanager["blue" ] = new Color (0, 0, 255);
 
    //  添加个性的颜色
     colormanager["angry" ] = new Color (255, 54, 0);
     colormanager["peace" ] = new Color (128, 211, 128);
     colormanager["flame" ] = new Color (211, 34, 20);
 
    //  克隆颜色
    Color  color1 = colormanager["red" ].Clone() as Color ;
    Color  color2 = colormanager["peace" ].Clone() as Color ;
    Color  color3 = colormanager["flame" ].Clone() as Color ;
 
    Console .ReadKey();
}

3、实例运行结果

五、总结(Summary

本文对原型模式( Prototype Pattern )的概念、设计结构图、代码、使用场景、深复制与浅复制的区别,以及如何 Net 平台下实现克隆进行了描述。以一个实例进行了说明。原型模式是比较常用和简单的设计模式。









本文转自 灵动生活 51CTO博客,原文链接:http://blog.51cto.com/smartlife/263881,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
设计模式 缓存 安全
【设计模式】单例模式:确保类只有一个实例
【设计模式】单例模式:确保类只有一个实例
25 0
|
4月前
|
设计模式 算法 Java
行为型设计模式-策略模式(Strategy Pattern)
行为型设计模式-策略模式(Strategy Pattern)
|
5月前
|
设计模式
二十三种设计模式全面解析-解密组合模式(Composite Pattern):构建统一而强大的对象结构
二十三种设计模式全面解析-解密组合模式(Composite Pattern):构建统一而强大的对象结构
|
5月前
|
设计模式 缓存
二十三种设计模式全面解析-代理模式(Proxy Pattern)详解:探索隐藏于背后的力量
二十三种设计模式全面解析-代理模式(Proxy Pattern)详解:探索隐藏于背后的力量
|
5月前
|
设计模式 存储 安全
二十三种设计模式全面解析-享元模式(Flyweight Pattern)详解:构建高效共享的对象结构
二十三种设计模式全面解析-享元模式(Flyweight Pattern)详解:构建高效共享的对象结构
|
4月前
|
设计模式 算法
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
41 1
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
|
4月前
|
设计模式 Java 应用服务中间件
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
37 1
|
4月前
|
设计模式 缓存 安全
设计模式 - 创建型模式_ 单例模式 Singleton Pattern
设计模式 - 创建型模式_ 单例模式 Singleton Pattern
40 0
|
26天前
|
设计模式 存储 Java
Java设计模式:解释一下单例模式(Singleton Pattern)。
`Singleton Pattern`是Java中的创建型设计模式,确保类只有一个实例并提供全局访问点。它通过私有化构造函数,用静态方法返回唯一的实例。类内静态变量存储此实例,对外仅通过静态方法访问。
16 1
|
5月前
|
设计模式 存储 Java
认真学习设计模式之命令模式(Command Pattern)
认真学习设计模式之命令模式(Command Pattern)
83 0