zoukankan      html  css  js  c++  java
  • 设计模式——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

  • 相关阅读:
    hausaufgabe--python 37 -- self in Class
    hausaufgabe--python 36-- Basic knowledge of Class
    hausaufgabe--python 35
    hausaufgabe--python 34
    hausaufgabe--python 33
    Retrofit2的使用简单介绍
    android中的Application作用
    第三章 Android控件架构与自定义控件详解
    第一章 Android体系与系统架构
    写在前面的话
  • 原文地址:https://www.cnblogs.com/sylvan/p/9978974.html
Copyright © 2011-2022 走看看