• 设计模式——8.原型模式


    原型模式(Prototype)

    Prototype模式简介:

    使用 原型实例 来指定 所要创建对象 的种类 ,然后通过拷贝 原型实例 来创建新的对象。

    Prototype模式结构:

    Prototype

    注意事项:

    使用原型模式时,要注意浅复制与深复制之间的区别(即C++中默认的复制构造函数或者C#中所有类都继承了的MemberwiseClone方法的使用)

    C++代码

    //file: Prototype.h
    #pragma once
    #include "Example.h"
    
    class Prototype
    {
    public:
    	Prototype();
    	virtual ~Prototype();
    	virtual Prototype* Clone() = 0;
    	Example* m_Example;
    };
    
    class ConcretePrototype : public Prototype
    {
    public:
    	ConcretePrototype(Example* pE);
    
    	//Use Default Copy Constructor To Get The Shallow Copy ,
    	//Or Define The Copy Constructor By Your Self To Get Deep Copy .
    	ConcretePrototype(const ConcretePrototype& ref);
    
    	virtual ~ConcretePrototype();
    
    	Prototype* Clone();
    };
    
    //file: Prototype.cpp
    #include "pch.h"
    #include "Prototype.h"
    
    ///Prototype
    Prototype::Prototype() {}
    
    Prototype::~Prototype() {}
    
    
    ///ConcretePrototype
    ConcretePrototype::ConcretePrototype(Example* pE) 
    {
    	ConcretePrototype::m_Example = pE;
    }
    
    //DeepCopy
    ConcretePrototype::ConcretePrototype(const ConcretePrototype& ref)
    {
    	ConcretePrototype::m_Example = new Example();
    	ConcretePrototype::m_Example->m_name = ref.m_Example->m_name;
    	ConcretePrototype::m_Example->m_level = ref.m_Example->m_level;
    }
    
    ConcretePrototype::~ConcretePrototype() {}
    
    Prototype* ConcretePrototype::Clone()
    {
    	return new ConcretePrototype(*this);
    }
    

    客户端代码:

    // PrototypePattern.cpp : This file contains the 'main' function. Program execution begins and ends there.
    #include "pch.h"
    #include "Prototype.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	Example* e = new Example();
    	Prototype* proto = new ConcretePrototype(e);
    	Prototype* copy= proto->Clone();
    
    	cout << (copy->m_Example == proto->m_Example) << endl;
    	
    	delete proto , copy;
    
    	return 0;
    }
    

    输出结果:

    深拷贝 0
    浅拷贝 1

    C#代码

    ////继承实现原型模式
    //public abstract class Prototype
    //{
    //    public abstract Prototype Clone();
    //}
    
    //public class ConcretePrototype : Prototype
    //{
    //    public override Prototype Clone()
    //    {
    //        return (ConcretePrototype)this.MemberwiseClone();
    //    }
    //}
    
    
    ////接口实现原型模式
    public class ConcretePrototype : ICloneable
    {
    	public Example Example { get; set; }
    
    	public ConcretePrototype(Example e)
    	{
    		this.Example = e;
    	}
    
    	////ShallowCopy
    	//public object Clone()
    	//{
    	//    return this.MemberwiseClone();
    	//}
    
    	//DeepCopy
    	public object Clone()
    	{
    		Example example = new Example();
    		example.Name = this.Example.Name;
    		example.Level = this.Example.Level;
    		ConcretePrototype prototype = new ConcretePrototype(example);
    		return prototype;
    	}
    }
    

    示例类Example:

    public class Example
    {
    	public string Name { get; set; }
    	public string Level { get; set; }
    
    	public Example(string name = "DefaultName", string lv = "123456")
    	{
    		Name = name;
    		Level = lv;
    	}
    }
    

    客户端调用

    class Program
    {
    	static void Main(string[] args)
    	{
    		Example e = new Example("sylvan","24");
    		ConcretePrototype proto = new ConcretePrototype(e);
    		ConcretePrototype copy = (ConcretePrototype)proto.Clone();
    		Console.WriteLine("Name : " +  copy.Example.Name + "  Level : " + copy.Example.Level );
    		Console.WriteLine(proto.Example.Equals(copy.Example));
    		Console.ReadKey();
    	}
    }
    

    浅拷贝输出为True

    深拷贝输出为False

    REF

    书籍:

    设计模式与游戏开发、大话设计模式

    GitHub:

    https://github.com/me115/design_patterns

  • 相关阅读:
    Vue学习笔记【28】——Vue路由(使用 children 属性实现路由嵌套)
    Vue学习笔记【27】——Vue路由(设置路由)
    Vue学习笔记【26】——Vue路由(什么是路由)
    Vue学习笔记【25】——Vue组件(组件间传值)
    Vue学习笔记【24】——Vue组件(组件切换)
    Vue学习笔记【23】——Vue组件(组件的定义)
    ga统计
    token验证机制
    网站发布(项目上线流程)
    使用CSS将图片转换成黑白(灰色、置灰) & 毛玻璃效果
  • 原文地址:https://www.cnblogs.com/sylvan/p/9978974.html
Copyright © 2020-2023  润新知