C#设计模式系列:原型模式(Prototype)
1、原型模式簡介
1.1>、定義
原型模式(Prototype)用原型實例指定創建對象的種類,并且通過復制這些原型創建新的對象。
1.2>、使用頻率
中
1.3>、原型模式應用
首先從實際生活來了解原型模式的由來,假設你有一份非常好的講義,你的朋友也想要一份,那么怎么辦?重新手抄一份?顯然不是,當然是用復印機復印一份來得方便、直接,并且準確性也高,這種用原型來復制而不是重新創建的思維方式就是原型模式的核心思想。
Prototype Pattern也是一種創建型模式,它關注的是大量相同或相似對象的創建問題。應用原型模式就是建立一個原型,然后通過對原型來進行復制的方法,來產生一個和原型相同或相似的新對象,或者說用原型實例指定創建對象的種類,并且通過復制這些原型創建新的對象。
2、原型模式結構
2.1>、結構圖
2.2>、參與者
原型模式參與者:
? Prototype:原型類,聲明一個Clone自身的接口;
? ConcretePrototype:具體原型類,實現一個Clone自身的操作。
在原型模式中,Prototype通常提供一個包含Clone方法的接口,具體的原型ConcretePrototype使用Clone方法完成對象的創建。
3、原型模式結構實現
Prototype.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace DesignPatterns.PrototypePattern.Structural {/// <summary>/// The 'Prototype' abstract class/// </summary>public abstract class Prototype{private string _id;/// <summary>/// Constructor/// </summary>public Prototype(string id){this._id = id;}/// <summary>/// Gets id/// </summary> public string Id{get { return _id; }}public abstract Prototype Clone();} }ConcretePrototype1.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace DesignPatterns.PrototypePattern.Structural {public class ConcretePrototype1 : Prototype{/// <summary>/// Constructor/// </summary>public ConcretePrototype1(string id): base(id){}/// <summary>/// Returns a shallow copy/// </summary>/// <returns></returns>public override Prototype Clone(){return (Prototype)this.MemberwiseClone();}} }ConcretePrototype2.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace DesignPatterns.PrototypePattern.Structural {public class ConcretePrototype2 : Prototype{/// <summary>/// Constructor/// </summary>public ConcretePrototype2(string id): base(id){}/// <summary>/// Returns a shallow copy/// </summary>/// <returns></returns>public override Prototype Clone(){return (Prototype)this.MemberwiseClone();}} }Client.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;using DesignPatterns.PrototypePattern.Structural;namespace DesignPatterns.PrototypePattern {class Client{static void Main(string[] args){// Create two instances and clone eachConcretePrototype1 p1 = new ConcretePrototype1("I");ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();Console.WriteLine("Cloned: {0}", c1.Id);ConcretePrototype2 p2 = new ConcretePrototype2("II");ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();Console.WriteLine("Cloned: {0}", c2.Id);}} }運行輸出:
Cloned: I Cloned: II 請按任意鍵繼續. . .4、原型模式實踐應用
?
ColorPrototype.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace DesignPatterns.PrototypePattern.Practical {/// <summary>/// The 'Prototype' abstract class/// </summary>public abstract class ColorPrototype{public abstract ColorPrototype Clone();} }Color.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace DesignPatterns.PrototypePattern.Practical {/// <summary>/// The 'ConcretePrototype' class/// </summary>public class Color : ColorPrototype{private int _red;private int _green;private int _blue;/// <summary>/// Constructor/// </summary>public Color(int red, int green, int blue){this._red = red;this._green = green;this._blue = blue;}/// <summary>/// Create a shallow copy/// </summary>public override ColorPrototype Clone(){Console.WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}", _red, _green, _blue);return this.MemberwiseClone() as ColorPrototype;}} }ColorManager.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace DesignPatterns.PrototypePattern.Practical {/// <summary>/// Prototype manager/// </summary>public class ColorManager{private Dictionary<string, ColorPrototype> _colors = new Dictionary<string, ColorPrototype>();/// <summary>/// Indexer/// </summary>public ColorPrototype this[string key]{get { return _colors[key]; }set { _colors.Add(key, value); }}} }Client.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;using DesignPatterns.PrototypePattern.Practical;namespace DesignPatterns.PrototypePattern {class Client{static void Main(string[] args){ColorManager colormanager = new ColorManager();// Initialize with standard colorscolormanager["red"] = new Color(255, 0, 0);colormanager["green"] = new Color(0, 255, 0);colormanager["blue"] = new Color(0, 0, 255);// User adds personalized colorscolormanager["angry"] = new Color(255, 54, 0);colormanager["peace"] = new Color(128, 211, 128);colormanager["flame"] = new Color(211, 34, 20);// User clones selected colorsColor color1 = colormanager["red"].Clone() as Color;Color color2 = colormanager["peace"].Clone() as Color;Color color3 = colormanager["flame"].Clone() as Color;}} }運行輸出:
Cloning color RGB: 255, 0, 0 Cloning color RGB: 128,211,128 Cloning color RGB: 211, 34, 20 請按任意鍵繼續. . .5、原型模式應用分析
原型模式可以適用于以下情形:
? 當一個系統應該獨立于它的產品創建、構成和表示時;
? 當要實例化的類是在運行時刻指定時,例如通過動態裝載來創建一個類;
? 為了避免創建一個與產品類層次平行的工廠類層次時;
? 當一個類的實例只能有幾個不同狀態組合中的一種時。建立相應數目的原型并Clone它們可能比每次用合適的狀態手工實例化該類更方便一些。
原型模式具有以下特點:
? 對客戶隱藏了具體的產品類,因此減少了客戶知道的名字的數目;
? 允許客戶只通過注冊原型實例就可以將一個具體產品類并入到系統中,客戶可以在運行時刻建立和刪除原型;
? 減少了子類的構造。原型模式是Clone一個原型而不是請求工廠方法創建一個,所以它不需要一個與具體產品類平行的Creator類層次;
? 原型模式具有給一個應用軟件動態加載新功能的能力。由于Prototype的獨立性較高,可以很容易動態加載新功能而不影響舊系統;
? 產品類不需要非得有任何事先確定的等級結構,因為原型模式適用于任何的等級結構;
? 原型模式的最重要缺點就是每一個類必須配備一個Clone方法,而且這個Clone方法需要對類的功能進行通盤考慮。這對全新的類來說不是很難,但對已有的類進行改造時,不一定是容易的事。
轉載于:https://www.cnblogs.com/libingql/p/3633377.html
總結
以上是生活随笔為你收集整理的C#设计模式系列:原型模式(Prototype)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android控件系列之RadioBut
- 下一篇: OSPFv2的综合实验试题分析第1例(C