码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-12-09 20:01:43      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:let   oid   hub   深复制   self   输出   deepcopy   UNC   c++   

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

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

标签:let   oid   hub   深复制   self   输出   deepcopy   UNC   c++   

原文地址:https://www.cnblogs.com/sylvan/p/10092467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!