• 设计模式:原型模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)


    上一篇《访问者模式》                                                                   下一篇《享元模式》

    简介:

    原型模式,它是一种创建型设计模式,它允许通过复制原型对象来创建新的对象,而无需知道创建的细节。其工作原理是将一个原型对象传递给要创建的对象,然后通过请求原型对象复制自己来实施创建。

    在原型模式中,克隆方法所创建的对象是全新对象,它们在内存中拥有全新的地址,通常对克隆所产生的对象进行修改不会对原型对象造成任何影响,每个克隆对象都是相互独立的。通过不同的方式对克隆对象进行修改后,可以得到一系列相似但不完全相同的对象。

    需要注意的是,对原型对象的浅拷贝,对于数据类型是基本数据类型的成员变量,会直接进行值传递,也就是将该属性值复制一份给新的对象;对于数据类型是引用数据类型的成员变量,比如说成员变量是某个数组、某个类的对象等,那么浅拷贝会进行引用传递,也就是只是将该成员变量的引用值(内存地址)复制一份给新的对象。在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成员变量值。

    原型模式的使用场景:
    1、类初始化需要消化非常多的资源,包括数据、硬件资源等。通过原型拷贝可以避免这些消耗。
    2、通过使用new关键字创建一个对象需要非常繁琐的数据准备或访问权限。
    3、一个对象需要提供给其他对象访问,而且各个调用者可能需要修改其值。在这种情况下,可以考虑使用原型模式拷贝多个对象供调用者使用,即保护性拷贝。

    在实际项目中,原型模式很少单独出现,一般是和工厂方法模式一起出现,通过 clone 的方法创建一个对象,然后由工厂方法提供给调用者。

    原型模式的创建步骤:
    1、定义抽象原型类:抽象原型类是定义具有克隆自己的方法的接口,是所有具体原型类的公共父类。
    2、定义具体原型类:具体原型类实现抽象原型类中的克隆方法,返回自己的一个克隆对象。
    3、定义客户类:客户类让一个原型克隆自身,从而创建一个新的对象。在客户类中只需要直接实例化或通过工厂方法等创建一个对象,再通过调用该对象的克隆方法复制得到多个相同的对象。

    原型模式通过复制原型对象来创建新对象,减少了创建新对象时所消耗的时间和资源。同时,由于复制的是原型对象,因此不会影响原对象的状态。

    原型模式的优点,主要包括:
    1、简化创建过程:原型模式可以简化对象的创建过程,通过复制一个已有实例可以提高新实例的创建效率。
    2、扩展性较好:由于在原型模式中提供了抽象原型类,在客户端可以针对抽象原型类进行编程,而将具体原型类写在配置文件中,增加或减少产品类对原有系统都没有任何影响。
    3、提供简化的创建结构:原型模式中产品的复制是通过封装在原型类中的克隆方法实现的,无须专门的工厂类来创建产品。
    4、支持深拷贝:原型模式可以使用深克隆方式保存对象的状态,使用原型模式将对象复制一份,并将其状态保存起来,简化了创建对象的过程,以便在需要的时候使用(如恢复到某一历史状态),可辅助实现撤销操作。

    总的来说,原型模式可以大大提高创建对象的效率,同时还能保证系统的扩展性和灵活性。

    原型模式的缺点,主要包括:
    1、在实现深拷贝时可能需要比较复杂的代码,需要为每一个类配备一个克隆方法,而且该克隆方法需要对类的功能进行通盘考虑,这对全新的类来说不是很难,但对已有的类进行改造时,不一定是件容易的事,必须修改其源代码,违背了“开闭原则”。

    示例:

    一、C#原型模式

    以下是一个示例,展示了如何在C#中实现原型模式:

    1. using System;  
    2.   
    3. namespace PrototypePatternExample  
    4. {  
    5.     // 抽象原型类  
    6.     public abstract class Prototype  
    7.     {  
    8.         public abstract Prototype Clone();  
    9.     }  
    10.   
    11.     // 具体原型类  
    12.     public class ConcretePrototype : Prototype  
    13.     {  
    14.         private string _property;  
    15.   
    16.         public ConcretePrototype(string property)  
    17.         {  
    18.             _property = property;  
    19.         }  
    20.   
    21.         public override Prototype Clone()  
    22.         {  
    23.             // 使用深拷贝复制对象  
    24.             ConcretePrototype clone = (ConcretePrototype)MemberwiseClone(this);  
    25.             return clone;  
    26.         }  
    27.   
    28.         public void Display()  
    29.         {  
    30.             Console.WriteLine("Property: " + _property);  
    31.         }  
    32.     }  
    33.   
    34.     class Program  
    35.     {  
    36.         static void Main(string[] args)  
    37.         {  
    38.             // 创建原型对象  
    39.             ConcretePrototype prototype = new ConcretePrototype("Hello World");  
    40.   
    41.             // 克隆原型对象  
    42.             ConcretePrototype clonePrototype = (ConcretePrototype)prototype.Clone();  
    43.   
    44.             // 显示原型对象和克隆对象的属性值是否相同  
    45.             prototype.Display();  
    46.             clonePrototype.Display();  
    47.             Console.ReadLine();  
    48.         }  
    49.     }  
    50. }

    二、java原型模式

    原型模式通常通过以下方式实现:

    1. import java.util.ArrayList;  
    2. import java.util.List;    
    3.   
    4. abstract class Shape {  
    5.     private String name;  
    6.       
    7.     public Shape(String name) {  
    8.         this.name = name;  
    9.     }  
    10.       
    11.     public String getName() {  
    12.         return name;  
    13.     }  
    14.       
    15.     public abstract void draw();  
    16.       
    17.     // 实现克隆方法  
    18.     public Shape clone() {  
    19.         try {  
    20.             return (Shape) this.getClass().newInstance();  
    21.         } catch (Exception e) {  
    22.             e.printStackTrace();  
    23.             return null;  
    24.         }  
    25.     }  
    26. }  
    27.   
    28. class Circle extends Shape {  
    29.     private int radius;  
    30.       
    31.     public Circle(String name, int radius) {  
    32.         super(name);  
    33.         this.radius = radius;  
    34.     }  
    35.       
    36.     @Override  
    37.     public void draw() {  
    38.         System.out.println("Drawing Circle");  
    39.     }  
    40. }  
    41.   
    42. class Rectangle extends Shape {  
    43.     private int width;  
    44.     private int height;  
    45.       
    46.     public Rectangle(String name, int width, int height) {  
    47.         super(name);  
    48.         this.width = width;  
    49.         this.height = height;  
    50.     }  
    51.       
    52.     @Override  
    53.     public void draw() {  
    54.         System.out.println("Drawing Rectangle");  
    55.     }  
    56. }
    57. public class PrototypePatternDemo {  
    58.   
    59.     public static void main(String[] args) {  
    60.         List shapeList = new ArrayList<>();  
    61.         shapeList.add(new Circle("Circle 1", 5));  
    62.         shapeList.add(new Rectangle("Rectangle 1", 5, 10));  
    63.         shapeList.add(new Circle("Circle 2", 10));  
    64.         shapeList.add(new Rectangle("Rectangle 2", 10, 20));  
    65.           
    66.         // 使用原型模式创建对象,节省创建对象的时间  
    67.         for (Shape shape : shapeList) {  
    68.             Shape cloneShape = shape.clone();  
    69.             System.out.println("Original Shape: " + shape.getName());  
    70.             System.out.println("Clone Shape: " + cloneShape.getName());  
    71.         }  
    72.     }  
    73. }  

    三、javascript原型模式

    在JavaScript中,实现原型模式的关键是使用构造函数和原型对象。
    下面是一个简单的JavaScript原型模式示例:

    1. // 定义原型对象  
    2. var CarProto = {  
    3.   color: "blue",  
    4.   speed: 0,  
    5.   start: function() {  
    6.     console.log("Car started");  
    7.   },  
    8.   stop: function() {  
    9.     console.log("Car stopped");  
    10.   }  
    11. };  
    12.   
    13. // 定义构造函数  
    14. function Car(color) {  
    15.   this.color = color;  
    16.   this.speed = 0;  
    17.   // 将构造函数prototype属性指向原型对象  
    18.   this.prototype = CarProto;  
    19. }  
    20.   
    21. // 定义子类  
    22. function SportCar() {  
    23.   // 调用父类构造函数  
    24.   Car.call(this, "red");  
    25.   // 重写父类方法  
    26.   this.start = function() {  
    27.     console.log("SportCar started");  
    28.   };  
    29. }  
    30.   
    31. // 设置原型对象,让SportCar继承CarProto  
    32. SportCar.prototype = CarProto;

    在这个示例中,我们定义了一个CarProto原型对象,它包含了汽车的属性和方法。然后我们定义了一个Car构造函数,它接受颜色参数,并设置速度属性为0,并将它的prototype属性指向CarProto。这样,当我们创建一个新的汽车对象时,它就会继承CarProto的属性和方法。

    然后我们定义了一个SportCar子类,它调用父类构造函数,并重写了父类的start方法。最后我们将SportCar.prototype设置为CarProto,这样SportCar就可以继承CarProto的属性和方法了。现在我们可以使用new关键字来创建SportCar对象了。

    四、C++原型模式

    以下是在C++中实现原型模式:

    1. #include  
    2. #include  
    3. #include  
    4.   
    5. using namespace std;  
    6.   
    7. // 定义原型接口  
    8. class Prototype {  
    9. public:  
    10.     virtual Prototype* clone() = 0;  
    11.     virtual void display() = 0;  
    12. };  
    13.   
    14. // 具体原型类  
    15. class ConcretePrototype : public Prototype {  
    16. private:  
    17.     string name;  
    18. public:  
    19.     ConcretePrototype(string n) : name(n) {}  
    20.     void setName(string n) { name = n; }  
    21.     string getName() { return name; }  
    22.     // 实现克隆方法  
    23.     Prototype* clone() { return new ConcretePrototype(*this); }  
    24.     // 实现显示方法  
    25.     void display() { cout << "ConcretePrototype " << name << endl; }  
    26. };  
    27.   
    28. // 工厂类  
    29. class PrototypeFactory {  
    30. private:  
    31.     map prototypes; // 存储原型对象的映射表  
    32. public:  
    33.     PrototypeFactory() {}  
    34.     Prototype* create(string type) { // 创建原型对象  
    35.         if (prototypes.find(type) == prototypes.end()) { // 如果该类型的原型对象不存在,则创建并存储在映射表中  
    36.             prototypes[type] = new ConcretePrototype(type);  
    37.         }  
    38.         return prototypes[type]->clone(); // 返回克隆后的对象  
    39.     }  
    40. };  
    41.   
    42. int main() {  
    43.     PrototypeFactory factory;  
    44.     Prototype* p1 = factory.create("prototype1"); // 创建原型对象1的克隆对象1  
    45.     p1->display(); // 显示原型对象1的名称,输出 "ConcretePrototype prototype1"  
    46.     Prototype* p2 = factory.create("prototype1"); // 创建原型对象1的克隆对象2  
    47.     p2->setName("prototype2"); // 设置克隆对象2的名称,不影响原型对象1的名称  
    48.     p2->display(); // 显示原型对象1的名称,输出 "ConcretePrototype prototype1",因为克隆对象2的名称没有修改成功,仍然是原型对象1的名称  
    49.     delete p1; // 释放内存空间,因为p1和p2都是通过克隆得到的,所以应该释放内存空间,避免内存泄漏问题。  
    50.     delete p2; // 释放内存空间,因为p1和p2都是通过克隆得到的,所以应该释放内存空间,避免内存泄漏问题。  
    51.     return 0;  
    52. }

    五、python原型模式

    以下是在python中实现原型模式:

    1. import copy  
    2.   
    3. # 定义原型类  
    4. class Prototype:  
    5.     def __init__(self, name):  
    6.         self.name = name  
    7.       
    8.     def clone(self):  
    9.         return copy.deepcopy(self)  
    10.   
    11. # 定义具体原型类  
    12. class ConcretePrototype(Prototype):  
    13.     def __init__(self, name):  
    14.         super().__init__(name)  
    15.         self.data = []  
    16.       
    17.     def add_data(self, data):  
    18.         self.data.append(data)  
    19.       
    20.     def clone(self):  
    21.         return ConcretePrototype(self.name)  
    22.   
    23. # 测试代码  
    24. if __name__ == '__main__':  
    25.     # 创建原型对象  
    26.     prototype1 = ConcretePrototype("prototype1")  
    27.     prototype1.add_data(1)  
    28.     prototype1.add_data(2)  
    29.     print("Prototype 1 data:", prototype1.data)  
    30.       
    31.     # 克隆原型对象  
    32.     clone1 = prototype1.clone()  
    33.     clone1.add_data(3)  
    34.     print("Clone 1 data:", clone1.data) # [1, 2, 3]  
    35.     print("Prototype 1 data:", prototype1.data) # [1, 2]  
    36.       
    37.     # 克隆克隆对象,避免修改原对象的影响  
    38.     clone2 = clone1.clone()  
    39.     clone2.add_data(4)  
    40.     print("Clone 2 data:", clone2.data) # [1, 2, 4]  
    41.     print("Prototype 1 data:", prototype1.data) # [1, 2]

    六、go原型模式

    以下是一个示例,展示了如何在go中实现原型模式:

    1. package main  
    2.   
    3. import (  
    4.     "fmt"  
    5. )  
    6.   
    7. // 原型接口  
    8. type Prototype interface {  
    9.     Clone() Prototype  
    10. }  
    11.   
    12. // 具体原型类  
    13. type ConcretePrototype struct {  
    14.     Name string  
    15. }  
    16.   
    17. // 克隆方法实现原型接口  
    18. func (p *ConcretePrototype) Clone() Prototype {  
    19.     return &ConcretePrototype{Name: p.Name}  
    20. }  
    21.   
    22. func main() {  
    23.     // 创建原型对象  
    24.     prototype1 := &ConcretePrototype{Name: "Prototype1"}  
    25.   
    26.     // 克隆原型对象  
    27.     clone1 := prototype1.Clone()  
    28.     fmt.Println("Clone 1 Name:", clone1.(*ConcretePrototype).Name) // 输出:Clone 1 Name: Prototype1  
    29.   
    30.     // 修改原型对象  
    31.     prototype1.Name = "Prototype2"  
    32.     fmt.Println("Prototype 1 Name:", prototype1.(*ConcretePrototype).Name) // 输出:Prototype 1 Name: Prototype2  
    33.   
    34.     // 克隆克隆对象,避免修改原对象的影响  
    35.     clone2 := clone1.Clone()  
    36.     fmt.Println("Clone 2 Name:", clone2.(*ConcretePrototype).Name) // 输出:Clone 2 Name: Prototype1  
    37. }

    七、PHP原型模式

    以下是一个示例,展示了如何在PHP中实现原型模式:

    1.  
    2.   
    3. class Prototype implements Cloneable {  
    4.     private $name;  
    5.       
    6.     public function __construct($name) {  
    7.         $this->name = $name;  
    8.     }  
    9.       
    10.     public function getName() {  
    11.         return $this->name;  
    12.     }  
    13.       
    14.     public function setName($name) {  
    15.         $this->name = $name;  
    16.     }  
    17.       
    18.     public function clone() {  
    19.         return clone $this;  
    20.     }  
    21. }  
    22.   
    23. // 创建原型对象  
    24. $prototype = new Prototype("Original");  
    25. echo "Prototype Name: " . $prototype->getName() . "\n";  
    26.   
    27. // 克隆原型对象  
    28. $clone = $prototype->clone();  
    29. echo "Clone Name: " . $clone->getName() . "\n";  
    30.   
    31. // 修改原型对象的属性  
    32. $prototype->setName("Modified");  
    33. echo "Prototype Name after modification: " . $prototype->getName() . "\n";  
    34.   
    35. // 克隆克隆对象,避免修改原对象的影响  
    36. $clone2 = $clone->clone();  
    37. echo "Clone 2 Name: " . $clone2->getName() . "\n";


    《完结》

    上一篇《访问者模式》                                                                     下一篇《享元模式》

  • 相关阅读:
    mysql和clickhouse数据同步 MaterializeMySQL 引擎
    全网最透彻的Netty原理讲解 一
    工厂人员违规行为识别系统
    PS不能完成命令,因为没有足够内存(RAM)
    python opencv实现图片清晰度增强
    酷炫的文字悬停效果
    LabVIEW开发航天器模拟器的姿态控制和反作用轮动量管理
    Ubuntu安装Protobuf,指定版本
    哈达玛矩阵与克罗内克积
    vscode 自定义(修改已有)主题教程
  • 原文地址:https://blog.csdn.net/yetyrain/article/details/134075575