[设计模式整理笔记 七] 原型模式(ProtoType)
来源:互联网 发布:上海餐饮软件 编辑:程序博客网 时间:2024/06/06 12:51
简单点理解原型模式的含义, 就是把一个对象复制出另一个对象, 但二个对象是独立的, 但原对象里面值在新对象里面也要存在, 一个是原对象, 一个是新对象, 注意原型模式里面, 必需是二个对象同时出现。 例如我们平时在编写代码过程中,通常用到Clone()方法, 这就是一原型模式的最典型的例子, 到这里已经理解得到原型模式是什么样的了吧!下面看看一个例子:
先设计一个类,继承ICloneable, 因为要用到Clone()方法:
代码
using System;
namespace ConsoleApp
{
public class SubCompany : ICloneable
{
public SubCompany(string name, string id)
{
this.name = name;
this.id = id;
this.headname = "中国移动";
}
//定义集团名称
private string headname;
//定义子公司名称
private string name;
//定义子公司标志ID
private string id;
public string Name
{
get { return name; }
set { name = value; }
}
public string ID
{
get { return id; }
set { id = value; }
}
public string HeadName
{
get { return headname; }
set { headname = value; }
}
/// <summary>
/// 返回集团公司名称
/// </summary>
/// <returns></returns>
public string GetHeadName()
{
return this.headname;
}
/// <summary>
/// 返回公司名称
/// </summary>
/// <returns></returns>
public string GetName()
{
return this.name;
}
/// <summary>
/// 设置公司名称
/// </summary>
/// <param name="name"></param>
public void SetName(string name)
{
this.name = name;
}
/// <summary>
/// 设置公司ID
/// </summary>
/// <param name="name"></param>
public void SetID(string id)
{
this.id = id;
}
/// <summary>
/// 返回公司的ID
/// </summary>
/// <returns></returns>
public string GetID()
{
return this.id;
}
/// <summary>
/// 实现ICloneable的Clone()方法
/// </summary>
/// <returns></returns>
public object Clone()
{
return this.MemberwiseClone();
}
}
}
namespace ConsoleApp
{
public class SubCompany : ICloneable
{
public SubCompany(string name, string id)
{
this.name = name;
this.id = id;
this.headname = "中国移动";
}
//定义集团名称
private string headname;
//定义子公司名称
private string name;
//定义子公司标志ID
private string id;
public string Name
{
get { return name; }
set { name = value; }
}
public string ID
{
get { return id; }
set { id = value; }
}
public string HeadName
{
get { return headname; }
set { headname = value; }
}
/// <summary>
/// 返回集团公司名称
/// </summary>
/// <returns></returns>
public string GetHeadName()
{
return this.headname;
}
/// <summary>
/// 返回公司名称
/// </summary>
/// <returns></returns>
public string GetName()
{
return this.name;
}
/// <summary>
/// 设置公司名称
/// </summary>
/// <param name="name"></param>
public void SetName(string name)
{
this.name = name;
}
/// <summary>
/// 设置公司ID
/// </summary>
/// <param name="name"></param>
public void SetID(string id)
{
this.id = id;
}
/// <summary>
/// 返回公司的ID
/// </summary>
/// <returns></returns>
public string GetID()
{
return this.id;
}
/// <summary>
/// 实现ICloneable的Clone()方法
/// </summary>
/// <returns></returns>
public object Clone()
{
return this.MemberwiseClone();
}
}
}
调用这个类:
代码
using System;
namespace ConsoleApp
{
class Program
{
public static void Main(string[] args)
{
SubCompany SubCompanySZ = new SubCompany("ShenZhen", "0755"); //定义深圳子公司
Console.WriteLine(SubCompanySZ.GetHeadName());
Console.WriteLine(SubCompanySZ.GetName());
Console.WriteLine(SubCompanySZ.GetID());
SubCompany SubCompanyBJ = (SubCompany)SubCompanySZ.Clone(); //复制对象为北京子公司
SubCompanyBJ.SetName("BeiJin");
SubCompanyBJ.SetID("010");
//这里下面这句, 因为北京子公司也是集团公司的, 所以这个集团公司的名称不需要再重新定义
Console.WriteLine(SubCompanySZ.GetHeadName());
Console.WriteLine(SubCompanyBJ.GetName());
Console.WriteLine(SubCompanyBJ.GetID());
Console.ReadLine();
}
}
}
namespace ConsoleApp
{
class Program
{
public static void Main(string[] args)
{
SubCompany SubCompanySZ = new SubCompany("ShenZhen", "0755"); //定义深圳子公司
Console.WriteLine(SubCompanySZ.GetHeadName());
Console.WriteLine(SubCompanySZ.GetName());
Console.WriteLine(SubCompanySZ.GetID());
SubCompany SubCompanyBJ = (SubCompany)SubCompanySZ.Clone(); //复制对象为北京子公司
SubCompanyBJ.SetName("BeiJin");
SubCompanyBJ.SetID("010");
//这里下面这句, 因为北京子公司也是集团公司的, 所以这个集团公司的名称不需要再重新定义
Console.WriteLine(SubCompanySZ.GetHeadName());
Console.WriteLine(SubCompanyBJ.GetName());
Console.WriteLine(SubCompanyBJ.GetID());
Console.ReadLine();
}
}
}
运行就看到结果了。
- [设计模式整理笔记 七] 原型模式(ProtoType)
- 设计模式 笔记 原型模式 prototype
- 设计模式 原型Prototype
- prototype(原型设计模式)
- 设计模式--Prototype(原型)
- Prototype(原型)设计模式
- 设计模式-prototype原型
- 设计模式--原型模式(ProtoType)
- [设计模式]Prototype原型模式
- 设计模式--Prototype(原型模式)
- 设计模式 -- 原型模式Prototype
- 设计模式-原型模式(Prototype)
- [设计模式] 原型模式(Prototype)
- 设计模式-原型模式(Prototype)
- 【设计模式】-原型模式-Prototype
- 设计模式 原型模式 Prototype
- 设计模式:原型模式-prototype
- 设计模式---原型模式(Prototype)
- TC1.0使用方法
- [设计模式整理笔记 六] 工厂模式与创建者模式总结
- c# string两个函数的用法
- Java注释规范
- 用结构实现栈,函数压栈,弹栈,初始化,释放
- [设计模式整理笔记 七] 原型模式(ProtoType)
- HTTP消息头中的“Cache-control”
- 彩色材料或成手机行业新一轮爆发点
- “第一次亲密接触”——数据库的设计
- [设计模式整理笔记 八] 单例模式(Singleton)
- RMAN输出到log文件中
- 有时一个眼神就知道对方是不是喜欢你
- wakelock 详解
- 测试