设计模式——10.原型模式

原型模式(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;
        }
    }

客户端调用

扫描二维码关注公众号,回复: 4437520 查看本文章

    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

猜你喜欢

转载自www.cnblogs.com/sylvan/p/10092467.html