A case study on the use of prototype patterns in C + + programming patterns _c language

Source: Internet
Author: User
Tags shallow copy

The implementation of the prototype Model code: the implementation of the prototype model is very simple, here for the convenience of beginners to learn and reference, will give a complete implementation code (all code in C + + implementation, and in VC 6.0 test run).

Code Snippets 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 Snippets 2:prototype.cpp

Prototype.cpp
#include "Prototype.h"
#include <iostream>
using namespace std;
Prototype::P rototype () {
}
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 to
 new Concreteprototype (*this);
}

Code Snippets 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: Prototype pattern of the structure and implementation is very simple, the key is (c + +) copy constructor implementation, which is also C + + implementation of the technical aspects of the matter. Because deep copies are not involved in the sample code (mainly referring to the case of pointers, composite objects), we implement the default copy constructor (in-place copy) provided by the compiler. The point is that this is all for simplicity and because the focus of this document is not on the implementation of the copy constructor, but on the idea of the prototype model itself.

Another instance

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

Namespace Prototype_designpattern {using System; Objects which are to work as prototypes must is based on classes which//are, from the abstract derived CLA
 SS Abstract class Abstractprototype {abstract public abstractprototype cloneyourself (); }//This is the sample object class Myprototype:abstractprototype {override public Abstractprototype Cloneyoursel
  F () {return ((Abstractprototype) MemberwiseClone ());
 }//Lots of 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 don't 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 fo
 R 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;

 }
 }
}

C # support for prototyping patterns

In C #, we can easily implement the prototype pattern through the Clone () method. For any class, you must implement the ICloneable interface in C # as long as you want to support cloning. The ICloneable interface has a clone method that allows you to make a copy of the custom cloning method in the class. There are two ways to implement a clone: a shallow copy (shallow copy) and a deep copy (deep copy).
shallow copy and deep copy

Here are two examples of shallow copies and deep copies, examples using the ICloneable interface. The array in C # is a reference 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-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 the SC1, its field V is not cloned, which causes the SC1 and SC2 Fields V to point to the same V, so SC1 's v[0] changed after the SC2 v[0 was modified.

Deep copy:

Using System;

Class deepcopy:icloneable
{public
 int[] v = {1,2,3};

 Default constructor public
 deepcopy ()
 {
 }

 //Private constructor for the Clone method call privacy
 deepcopy (int[] v)
 {
 THIS.V = (int[]) v.clone ();
 }

 public Object Clone ()
 {
 //constructs a new Deepcopy object, which constructs a parameter of
 the V return 
 new Deepcopy (THIS.V) used in the original object;
 } 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 on the prototype model

The prototype mode obtains the new object creation function by copying the prototype (prototype), where the prototype itself is the "Object Factory" (because it can produce objects), in fact the prototype and Builder patterns, the Abstractfactory A pattern is a class (object instance) that is specifically responsible for the creation of objects (factory objects), the difference being that the Builder pattern is created in a step-by-step (not directly returned object) of a complex object, and the abstractfactory pattern is focused on generating multiple objects of interdependent classes. And the prototype pattern is copying itself from itself to create the new class.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.