原型模式(Prototype Pattern)

原型模式(Prototype Pattern)

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

结构



与者
Prorotype
--声明一个克隆自身的接口。
ConcretePrototype
--实现一个克隆自身的操作。
Client
--让一个原型克隆自身从而创建一个新的对象。

代码

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

namespace MyPrototype
{
    abstract class Prototype
    {
        private string id;

        public string Id
        {
            get { return id; }
        }

        public Prototype(string id)
        {
            this.id = id;
        }

        public abstract Prototype Clone();
    }

    class ConcrtePrototype1 : Prototype
    {
        public ConcrtePrototype1(string id)
            : base(id)
        {

        }

        public override Prototype Clone()
        {
            return (Prototype)this.MemberwiseClone(); // 浅复制
        }
    }

    class ConcrtePrototype2 : Prototype
    {
        public ConcrtePrototype2(string id)
            : base(id)
        {

        }

        public override Prototype Clone()
        {
            return (Prototype)this.MemberwiseClone(); // 浅复制
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyPrototype
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcrtePrototype1 p1 = new ConcrtePrototype1("A");
            ConcrtePrototype2 p2 = new ConcrtePrototype2("B");

            ConcrtePrototype1 c1 = (ConcrtePrototype1)p1.Clone();
            ConcrtePrototype2 c2 = (ConcrtePrototype2)p2.Clone();

            Console.WriteLine("Clone c1.Id: {0}", c1.Id);
            Console.WriteLine("Clone c2.Id: {0}", c2.Id);
            Console.ReadKey();
        }
    }
}
实现

1)使用一个原型管理器(Prototype manager)。

2)实现克隆操作(注意浅拷贝与深拷贝)。

3)初始化克隆对象。

效果

1)运行时刻增加和删除产品;
2)改变值以指定新对象;
3)改变结构以指定新对象;
4)减少子类的结构;
5)用类动态配置应用。

原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道深入创建细节。

.NET在System命令空间中提供了ICloneable接口,其含有唯一的方法Close(),实现该接口就完成了原型模式。MemberwiseClose()方式,如果字段是值类型的,则对该字段执行逐位复制,如果字段是引用类型,则复制引用但不复制引用的对象;因此,原始对象及其副本引用同一对象(浅复制)。

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

namespace Resume
{
    class WorkExperience : ICloneable
    {
        private string workDate;
        public string WorkDate
        {
            get { return workDate; }
            set { workDate = value; }
        }

        private string company;
        public string Company
        {
            get { return company; }
            set { company = value; }
        }

        public Object Clone()
        {
            return (Object)this.MemberwiseClone();
        }
    }

    class MyResume : ICloneable
    {
        private string name;
        private string sex;
        private string age;
        private WorkExperience work;

        public MyResume(string name)
        {
            this.name = name;
            work = new WorkExperience();
        }

        private MyResume(WorkExperience work)
        {
            this.work = (WorkExperience)work;
        }

        public void SetPersonalInfo(string sex, string age)
        {
            this.sex = sex;
            this.age = age;
        }

        public void SetWordExperience(string wordDate, string company)
        {
            work.WorkDate = wordDate;
            work.Company = company;
        }

        public void Display()
        {
            Console.WriteLine("{0} {1} {2}", name, sex, age);
            Console.WriteLine("Work experience:{0} {1}", work.WorkDate, work.Company);
        }

        public Object Clone() // 深复制
        {
            MyResume obj = new MyResume(this.work);
            obj.name = this.name;
            obj.sex = this.sex;
            obj.age = this.age;

            return obj;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Resume
{
    class Program
    {
        static void Main(string[] args)
        {
            MyResume a = new MyResume("A");
            a.SetPersonalInfo("man", "23");
            a.SetWordExperience("1998-2000", "XX");

            MyResume b = (MyResume)a.Clone();
            b.SetWordExperience("2000-2008 ", "YY");

            MyResume c = (MyResume)a.Clone();
            c.SetWordExperience("2008-2015", "ZZ");

            a.Display();
            b.Display();
            c.Display();

            Console.ReadKey();
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值