Examples illustrate the use of prototype patterns in design patterns in C++ programming

  • 2020-05-09 18:58:44
  • OfStack

Prototype pattern implementation complete code example (code) : the prototype pattern implementation is very simple, here for beginners to learn and reference, will give the complete implementation code (all code using C++ implementation, and VC 6.0 under the test run).

Code snippet 1: Prototype.h


//Prototype.h
#ifndef _PROTOTYPE_H_
#define _PROTOTYPE_H_
class Prototype{
 public:
 virtual ~Prototype();
 virtual Prototype* Clone() const = 0;
 protected:
 Prototype();
 private:
};
class ConcretePrototype:public Prototype{
 public:
 ConcretePrototype();
 ConcretePrototype(const ConcretePrototype& cp);
 ~ConcretePrototype();
 Prototype* Clone() const;
 protected:
 private:
};
#endif //~_PROTOTYPE_H_

Code snippet 2: Prototype.cpp


//Prototype.cpp
#include "Prototype.h"
#include <iostream>
using namespace std;
Prototype::Prototype(){
}
Prototype::~Prototype(){
}
Prototype* Prototype::Clone() const{
 return 0;
}
ConcretePrototype::ConcretePrototype(){
}
ConcretePrototype::~ConcretePrototype(){
}
ConcretePrototype::ConcretePrototype(const ConcretePrototype& cp){
 cout<<"ConcretePrototype copy ..."<<endl;
}
Prototype* ConcretePrototype::Clone() const{
 return new ConcretePrototype(*this);
}

Code snippet 3: main.cpp


//main.cpp
#include "Prototype.h"
#include <iostream>
using namespace std;
int main(int argc,char* argv[]){
 Prototype* p = new ConcretePrototype();
 Prototype* p1 = p->Clone();
 return 0;
}

Code description: the structure and implementation of the prototype pattern is very simple. The key is the implementation of the copy constructor (C++), which is also the technical aspect of C++ implementation. Since there is no deep copy involved in the sample code (mainly in the case of Pointers and composite objects), we implemented it using the default copy constructor (bitwise copy) provided by the compiler. Note that this cut is for implementation simplicity only, and because the focus of this document is not on the copy constructor implementation technique, but on the idea of the prototype pattern itself.

Another example

Let's look at another example of a specific project:


namespace Prototype_DesignPattern
{
 using System;

 // Objects which are to work as prototypes must be based on classes which 
 // are derived from the abstract prototype class
 abstract class AbstractPrototype 
 {
  abstract public AbstractPrototype CloneYourself();
 }

 // This is a sample object
 class MyPrototype : AbstractPrototype 
 {
  override public AbstractPrototype CloneYourself()
  {
   return ((AbstractPrototype)MemberwiseClone());
  }
  // lots of other functions go here!
 }

 // This is the client piece of code which instantiate objects
 // based on a prototype. 
 class Demo 
 {
  private AbstractPrototype internalPrototype;

  public void SetPrototype(AbstractPrototype thePrototype)
  {
   internalPrototype = thePrototype;   
  }

  public void SomeImportantOperation()
  {
   // During Some important operation, imagine we need
   // to instantiate an object - but we do not know which. We use
   // the predefined prototype object, and ask it to clone itself. 

   AbstractPrototype x;
   x = internalPrototype.CloneYourself();
   // now we have two instances of the class which as as a prototype
  }
 }

 /// <summary>
 /// Summary description for Client.
 /// </summary>
 public class Client
 {
  public static int Main(string[] args)
  {      
   Demo demo = new Demo();
   MyPrototype clientPrototype = new MyPrototype();
   demo.SetPrototype(clientPrototype);
   demo.SomeImportantOperation();

   return 0;
  }
 }
}

Support for prototype patterns in C#

In C#, we can easily implement the prototype pattern using the Clone() method. Any class that wants to support cloning must implement the ICloneable interface in C#. The ICloneable interface has the 1Clone method, which can be copied to a class to implement a custom clone method. There are two ways to achieve cloning: shallow copy (shallow copy) and deep copy (deep copy).
Shallow copy and deep copy

Here are two examples of shallow and deep copy, using the ICloneable interface. The array in C# is a referential variable, which we demonstrate by using an array:

Shallow copy:


using System;

class ShallowCopy : ICloneable
{
 public int[] v = {1,2,3};

 public Object Clone()
 {
 return this.MemberwiseClone();
 }

 public void Display()
 {
 foreach(int i in v)
  Console.Write( i + ", ");
 Console.WriteLine();
 }
}

class Client
{
 public static void Main()
 {
 ShallowCopy sc1 = new ShallowCopy();
 ShallowCopy sc2 = (ShallowCopy)sc1.Clone();
 sc1.v[0] = 9;

 sc1.Display();
 sc2.Display();
 }
}

The ShallowCopy object implements a shallow copy, so when cloning sc1, its field v is not cloned, which causes sc1 and sc2's field v both point to the same v. Therefore, when sc1's v[0] is modified, sc2's v[0] is also changed.

Deep copy:


using System;

class DeepCopy : ICloneable
{
 public int[] v = {1,2,3};

 //  Default constructor 
 public DeepCopy()
 {
 }

 //  for Clone Private constructor for method calls 
 private DeepCopy(int[] v)
 {
 this.v = (int[])v.Clone();
 }

 public Object Clone()
 {
 //  structure 1 A new one DeepCopy Object, and the construction parameter is 
 //  Used in the original object  v 
 return new DeepCopy(this.v);
 }

 public void Display()
 {
 foreach(int i in v)
  Console.Write( i + ", ");
 Console.WriteLine();
 }
}

class Client
{
 public static void Main()
 {
 DeepCopy dc1 = new DeepCopy();
 DeepCopy dc2 = (DeepCopy)dc1.Clone();
 dc1.v[0] = 9;

 dc1.Display();
 dc2.Display();
 }
}

Discussion of prototype patterns

Prototype model by copying the prototype (prototype) and the function of the new object creation, this prototype is "object factory" (because can produce objects), prototype model and actually Builder mode, AbstractFactory mode by a class object (object instance) to responsible for the creation of object (factory), the difference between them is: Builder model on complex object 1 step by step to create (not directly return objects), AbstractFactory mode on produce multiple interdependent class object, and the prototype model from their own copy themselves to create a new class.


Related articles: