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

简介:

一、原型模式简介(Brief Introduction

原型模式 (Prototype Pattern) :用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype
浅复制与深复制区别:
浅复制,被复制的所有变量都还有与原来对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。
深复制,把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。
Net 命名空间 System 提供了一个 IConeable 接口,此接口只有一个方法 Clone() ,只需要实现这个接口就可以实现原型模式( Prototype Pattern )了。

二、解决的问题(What To Solve

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

三、原型模式分析(Analysis

1、原型模式结构

 
Prototype :原型类  Clone() 方法 : 克隆自身的接口。
ConcretePrototypeA ConcretePrototypeA :原型类的具体实现。克隆一个自身的操作。

2、代码

1 、原型抽象类 Prototype
///   <summary>
///   抽象原型模式类
///   </summary>
public  abstract class Prototype
{
    private string  _id;
    public  Prototype(string  id)
     {
        this .Id = id;
     }
    public string  Id
     {
        get  { return  _id; }
        set  { _id = value ; }
     }
 
    public abstract Prototype  Clone();
}
 
2 、具体实现类 ConcretePrototypeA ConcretePrototypeB
public  class ConcretePrototypeA  : Prototype
{
    public  ConcretePrototypeA(string  id)
         base (id)
     { }
    /// <summary>
    /// Returns a shallow copy  浅拷贝
    /// </summary>
    /// <returns>a shallow copy</returns>
    public override Prototype  Clone()
     {
        return  (Prototype )this .MemberwiseClone();
     }
}
 
public  class ConcretePrototypeB :Prototype
{
    public  ConcretePrototypeB(string  id)
         base (id)
     { }
    /// <summary>
    /// a shallow copy
    /// </summary>
    /// <returns> 浅拷贝 </returns>
    public override Prototype  Clone()
     {
        return  (Prototype )this .MemberwiseClone();
     }
}
 
2 、客户端代码
static  void Main (string [] args)
{
    Prototype  pA = new ConcretePrototypeA ("A" );
    Prototype  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();
}

3、实例运行结果










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

目录
相关文章
|
2月前
|
设计模式 缓存 安全
【设计模式】单例模式:确保类只有一个实例
【设计模式】单例模式:确保类只有一个实例
25 0
|
4月前
|
设计模式 算法 Java
行为型设计模式-策略模式(Strategy Pattern)
行为型设计模式-策略模式(Strategy Pattern)
|
4月前
|
设计模式 算法
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
42 1
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
|
4月前
|
设计模式 Java 应用服务中间件
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
37 1
|
4月前
|
设计模式 缓存 安全
设计模式 - 创建型模式_ 单例模式 Singleton Pattern
设计模式 - 创建型模式_ 单例模式 Singleton Pattern
40 0
|
29天前
|
设计模式 存储 Java
Java设计模式:解释一下单例模式(Singleton Pattern)。
`Singleton Pattern`是Java中的创建型设计模式,确保类只有一个实例并提供全局访问点。它通过私有化构造函数,用静态方法返回唯一的实例。类内静态变量存储此实例,对外仅通过静态方法访问。
16 1
|
4月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
47 0
|
19天前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
22 0
|
2月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
32 0
|
2月前
mvc.net分页查询案例——mvc-paper.css
mvc.net分页查询案例——mvc-paper.css
5 0