原型模式(Prototype Pattern)

         通过一个给定的实例创建一个对象,与通过对一个类进行实例化构造新对象不同的是,通过拷贝一个现有的对象生成新的对象。利用原型模式可以不需要工厂方法类。特别是不同对象的类仅仅在属性具有区别,而行为并没有区别时候,通过拷贝一个现有的对象生成新的对象可以减少子类的数目。它与调用构造函数创建新对象的区别在于:拷贝对象一般包括了原有对象的某些状态信息。

        模型模式允许动态的增加或减少产品类,产品类不需要非得有任何事先确定的等级结构,原型模式适用于任何的等级结构。[1]

       原型模式(Prototype)和生成器模式(Builder)、抽象工厂模式(AbstractFactory)都是通过一个类的对象来专门负责对象的创建工作,它们之间的区别是:原型模式通过拷贝一个现有的对象生成新的对象;生成器模式在进行对象构造之前,要逐步收集构造相关的信息,一步步创建;抽象工厂模式是创建一组相关或者相互依赖的对象提供支持.

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

namespace  PrototypePattern
{
    
public class EntityModel
    
{
        
private string _name;

        
/// <summary>
        
/// 名称
        
/// </summary>

        public string Name
        
{
            
get 
            
{
                
return _name;
            }

            
set 
            
{
                _name 
= value;
            }

        }


        
private DateTime _currentTime;

        
/// <summary>
        
/// 当前时间
        
/// </summary>

        public DateTime CurrentTime
        
{
            
get
            
{
                
return _currentTime;
            }

            
set
            
{
                _currentTime 
= value;
            }

        }


        
/// <summary>
        
/// 默认构造函数
        
/// </summary>

        public EntityModel()
        
{
            
this._name = string.Empty;
            
this._currentTime = DateTime.Now;
        }


        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="name">名称</param>
        
/// <param name="currentTime">当前时间</param>

        public EntityModel(string name, DateTime currentTime)
        
{
            
this._name = name;
            
this._currentTime = currentTime;
        }

    }


    
public abstract class Prototype
    
{
        
public abstract Prototype Clone();
    }


    
/// <summary>
    
/// 浅拷贝原型类 
    
/// </summary>

    public class ConcteteShallowPrototype : Prototype
    
{
        
private EntityModel _currentEntityModel;

        
/// <summary>
        
/// 实体对象
        
/// </summary>

        public EntityModel CurrentEntityModel
        
{
            
get
            
{
                
return _currentEntityModel;
            }

            
set
            
{
                _currentEntityModel 
= value;
            }

        }


        
/// <summary>
        
/// 默认构造函数
        
/// </summary>

        public ConcteteShallowPrototype()
        

        }


        
/// <summary>
        
/// 浅拷贝
        
/// </summary>
        
/// <returns></returns>

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


    
/// <summary>
    
/// 深拷贝原型类 
    
/// </summary>

    public class ConcteteDeepPrototype : Prototype
    
{
        
private EntityModel _currentEntityModel;

        
/// <summary>
        
/// 实体对象
        
/// </summary>

        public EntityModel CurrentEntityModel
        
{
            
get
            
{
                
return _currentEntityModel;
            }

            
set
            
{
                _currentEntityModel 
= value;
            }

        }


        
/// <summary>
        
/// 默认构造函数
        
/// </summary>

        public ConcteteDeepPrototype()
        
{
        }


        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="currentEntityModel">实体</param>

        public ConcteteDeepPrototype(EntityModel currentEntityModel)
        
{
            
this._currentEntityModel = currentEntityModel;
        }


        
/// <summary>
        
/// 浅拷贝
        
/// </summary>
        
/// <returns></returns>

        public override Prototype Clone()
        
{
            
return new ConcteteDeepPrototype(new EntityModel(_currentEntityModel.Name, _currentEntityModel.CurrentTime));
        }

    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            ConcteteShallowPrototype shallowPrototype 
= new ConcteteShallowPrototype();
            shallowPrototype.CurrentEntityModel 
= new EntityModel("原型", DateTime.Now);

            ConcteteShallowPrototype shallowPrototypeCopy 
= (ConcteteShallowPrototype)shallowPrototype.Clone();

            Console.ReadLine();
        }

    }

}

 

[1]http://blog.csdn.net/zzdong168/archive/2007/05/21/1619891.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype) 结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern) 17. 解释器模式(Interpreter Pattern) 18. 中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern) 工程结构 ├─01.Singleton │ ├─html │ └─MySingleton │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─02.ChainOfResponsibility │ ├─html │ ├─My2ChainOfResponsibility │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─MyChainOfResponsibility │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ ├─Refactor │ │ └─TempPE │ └─Properties ├─03.FactoryMethodMode │ ├─FactoryMethodMode │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─html ├─04.AbstractFactory │ ├─04.1.SimpleFactory │ │ ├─html │ │ └─SimpleFactory │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ ├─AbstractFactory │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─html ├─05.BuilderPattern │ ├─html │ └─MyBuilderPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─06.PrototypePattern │ ├─html │ │ └─C#设计模式(6)——原型模式Prototype Patt O技术博客_files │ └─PrototypePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─07.AdapterPattern │ ├─html │ └─MyAdapterPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─08.BridgePattern │ ├─html │ └─MyBridgePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─09.DecoratorPattern │ ├─html │ └─MyDecoratorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─10.CompositePattern │ ├─html │ └─MyCompositePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─11.FacadePattern │ ├─html │ └─MyFacadePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─12.FlyweightPattern │ ├─html │ └─MyFlyweightPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─13.ProxyPattern │ ├─html │ └─MyProxyPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─14.TemplateMethod │ ├─html │ └─MyTemplateMethod │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─15.VisitorPattern │ ├─html │ └─MyVisitorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─16.StrategyPattern │ ├─html │ └─MyStrategyPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─17.StatePattern │ ├─html │ └─StatePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─18.MementoPattern │ ├─html │ └─MementoPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─19.MediatorPattern │ ├─html │ └─MediatorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─20.OberverPattern │ ├─CatOberverPattern │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ ├─html │ └─MyOberverPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─21.IteratorPattern │ ├─html │ └─IteratorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─22.InterpreterPattern │ ├─html │ └─MyInterpreterPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties └─23.CommandPattern ├─html └─MyCommandPattern ├─bin │ └─Debug ├─obj │ └─Debug │ └─TempPE └─Properties

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值