131
人物
原型模式
【原型模式】:
用原型實例指定創建對象的種類,並且通過拷貝這些原型創建新的對象。
適用性
當要實例化的類是在運行時刻指定時,例如,通過動態裝載;
或者為了避免創建一個與產品類層次平行的工廠類層次時;
或者當一個類的實例隻能有幾個不同狀態組合中的一種時。
建立相應數目的原型並克隆它們可能比每次用合適的狀態手工實例化該類更方便一些。
【實現代碼】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
//原型模式:其實就是從一個對象再創建另一個可製定的對象,而且不需知道任何創建的細節。
//原型類;
abstract class Prototype
{
private string id;
public Prototype(string id)
{
this.id = id;
}
public string Id
{
get { return id; }
}
public abstract Prototype Copy();//抽象類關鍵就是有這樣一個Clone方法;
}
//具體原型類;
class ConcretePtototypel : Prototype
{
public ConcretePtototypel(string id)
: base(id)
{ }
public override Prototype Copy()
{
return (Prototype)this.MemberwiseClone();
/*MemberwiseClone 方法創建一個淺表副本,方法是創建一個新對象,
然後將當前對象的非靜態字段複製到該新對象。
如果字段是值類型的,則對該字段執行逐位複製。
如果字段是引用類型,則複製引用但不複製引用的對象;
因此,原始對象及其複本引用同一對象。*/
}
}
//客戶代碼
class Program
{
static void Main(string[] args)
{
ConcretePtototypel p1 = new ConcretePtototypel("I");
//克隆類ConcretePtototypel的對象p1就能得到新實例c1;
ConcretePtototypel c1 = (ConcretePtototypel)p1.Copy();
Console.WriteLine("Cloned:{0}",c1.Id);
Console.Read();
}
}
}
【實例代碼2.】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
//由於.NET在System命名空間中提供了IConeable接口,
//其中唯一的一個方法就是Clone。這樣就隻需實現這個接口就可以完成原型模式了。
//簡曆類;
class Resume:ICloneable
{
private string name;
private string sex;
private string age;
private string timeArea;
private string company;
public Resume(string name)
{
this.name = name;
}
//設置個人信息;
public void SetPersonalInfo(string sex, string age)
{
this.age = age;
this.sex = sex;
}
//設置工作經曆;
public void SetWorkPerience(string timeArea, string company)
{
this.company = company;
this.timeArea = timeArea;
}
//顯示;
public void Display()
{
Console.WriteLine("{0}{1}{2}",name ,sex,age);
Console.WriteLine("工作經曆:{0} {1}",timeArea,company);
}
public Object Clone()
{
return (Object)this.MemberwiseClone();
}
//客戶代碼
class Program
{
static void Main(string[] args)
{
Resume a = new Resume("小山");
a.SetPersonalInfo("男","22");
a.SetWorkPerience("2006-2007","大一年級");
Resume b = (Resume)a.Clone();
b.SetPersonalInfo("張小山","22");
b.SetWorkPerience("2007-2008","大二年級");
Resume c = (Resume)a.Clone();
c.SetPersonalInfo("張 山","22");
c.SetWorkPerience("2008-2009","大三年級");
a.Display();
b.Display();
c.Display();
Console.Read();
}
}
}
}
【淺表複製】
MemberwiseClone 方法創建一個淺表副本,方法是創建一個新對象,然後將當前對象的非靜態字段複製到該新對象。如果字段是值類型的,則對該字段執行逐位複製。如果字段是引用類型,則複製引用但不複製引用的對象;因此,原始對象及其複本引用同一對象。使用實現 ICloneable 接口的類執行對象的淺表或深層複製。
【實現代碼】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Resume:ICloneable
{
private string name;
private string sex;
private string age;
private WorkExperience work;//引用“工作經曆”對象;
public Resume(string name)
{
this.name = name;
//在“簡曆”類實例化的同時,實例化”工作經曆“;
work=new WorkExperience();
}
//設置個人信息;
public void SetPersonalInfo(string sex, string age)
{
this.age = age;
this.sex = sex;
}
//設置工作經曆;
public void SetWorkPerience(string workDate, string company)
{
work.WorkDate=workDate;
work.Company=company;
}
//顯示;
public void Display()
{
Console.WriteLine("{0}{1}{2}",name ,sex,age);
Console.WriteLine("工作經曆:{0} {1}",
work.WorkDate,work.Company);
}
public Object Clone()
{
return (Object)this.MemberwiseClone();
}
//工作經曆類;
class WorkExperience
{
private string workDate;
public string WorkDate
{
get{return workDate;}
set{workDate=value;}
}
private string company;
public string Company
{
get{return company;}
set{company=value;}
}
}
//客戶代碼
class Program
{
static void Main(string[] args)
{
Resume a = new Resume("小山");
a.SetPersonalInfo("男","22");
a.SetWorkPerience("2006-2007","大一年級");
Resume b = (Resume)a.Clone();
b.SetPersonalInfo("張小山","22");
b.SetWorkPerience("2007-2008","大二年級");
Resume c = (Resume)a.Clone();
c.SetPersonalInfo("張山","22");
c.SetWorkPerience("2008-2009","大三年級");
a.Display();
b.Display();
c.Display();
/這裏b和c都克隆於a,但當它們都設置了工作經曆時,我們希望的結果是現實三個不同的結果;但是,結果它們卻是一樣的,都顯示最後的設置。因為隻是複製了引用,沒有真正複製對象!
Console.Read();
}
}
}
}
【深表複製】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
//工作經曆類;
class WorkExperience:ICloneable // ①讓“工作經曆”類實現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 Resume:ICloneable
{
private string name;
private string sex;
private string age;
private WorkExperience work;//引用“工作經曆”對象;
public Resume(string name)
{
this.name = name;
work=new WorkExperience();
}
private Resume(WorkExperience work)
{
// ③提供Clone方法調用私有的構造函數,以便克隆“工作經曆”的數據;
this.work = (WorkExperience)work.Clone();
}
//設置個人信息;
public void SetPersonalInfo(string sex, string age)
{
this.age = age;
this.sex = sex;
}
//設置工作經曆;
public void SetWorkPerience(string workDate, string company)
{
work.WorkDate=workDate;
work.Company=company;
}
//顯示;
public void Display()
{
Console.WriteLine("{0}{1}{2}",name ,sex,age);
Console.WriteLine("工作經曆:{0}
{1}",work.WorkDate,work.Company);
}
public Object Clone()
{
// ④調用私有的構造方法,完成克隆"工作經曆"類的數據;
Resume obj = new Resume(this.work);
obj.name = this.name;
obj.sex = this.sex;
obj.age = this.age;
return obj;
}
//客戶代碼
class Program
{
static void Main(string[] args)
{
Resume a = new Resume("小山");
a.SetPersonalInfo("男","22");
a.SetWorkPerience("2006-2007","大一年級");
Resume b = (Resume)a.Clone();
b.SetPersonalInfo("張小山","22");
b.SetWorkPerience("2007-2008","大二年級");
Resume c = (Resume)a.Clone();
c.SetPersonalInfo("張山","22");
c.SetWorkPerience("2008-2009","大三年級");
a.Display();
b.Display();
c.Display();
Console.Read();
}
}
}
}
最後更新:2017-04-02 03:42:38
上一篇:
C#3.0的一些新特性
下一篇:
淺談如何提高企業核心凝聚力
oracle 判斷列是否在數據庫中存在
思維導圖Minimanager9 “參數錯誤”問題
微軟的世界你不懂,Office 即將來到 iOS 平台
Delete與truncate的區別
阿裏發布多款新產品,欲以何為?
android loading界麵 及 處理
WebLogic中修改端口號和省略端口號的做法
[Android] Eclipse Warning: No grammar constraints (DTD or XML schema) detected for the document
JAVA線程中的生產者和消費者問題
《STM32庫開發實戰指南:基於STM32F4》----2.2 硬件連接