创建型模式-原型模式(prototype)

原型模式

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

实例

main.cc:

#include 
#include "software_computer.h"

/*
design_pattern:"prototype"
For example, the company to employees with computers.
Assistant computer configuration: I5 processor, 19 inch display, ordinary keyboard.
Software Engineer computer configuration: keyboard for cherry, the other with the assistantFollow up .
all the same software engineers
*/
int main(){
    SoftwareComputer *software_computer = new SoftwareComputer();
    software_computer->ShowConfig();
    AssistantComputer *software_computer_2 = software_computer->clone();
    software_computer_2->ShowConfig();
    AssistantComputer *software_computer_3 = software_computer->clone();
    software_computer_3->ShowConfig();

    //clear
    delete software_computer;
    delete software_computer_2;
    delete software_computer_3;
    system("Pause");
    return 0;
}

AssistantComputer:

//assistant_computer.h
#ifndef HELENDP_SOURCE_ASSISTANT_COMPUTER_H_
#define HELENDP_SOURCE_ASSISTANT_COMPUTER_H_
#include 
using namespace std;

class AssistantComputer{
public:
    AssistantComputer();
    virtual ~AssistantComputer();

    AssistantComputer* clone();
    virtual void ShowConfig();

    string  processor_;
    string  monitor_;
    string  keyboard_;
};
#endif


//assistant_computer.cc
#include "assistant_computer.h"
#include 
using namespace std;

AssistantComputer::AssistantComputer(){
    processor_ = "i5 processor";
    monitor_ = "19 inch monitor";
    keyboard_ = "Double Birds";
}

AssistantComputer::~AssistantComputer(){

}

AssistantComputer* AssistantComputer::clone(){
    AssistantComputer *assistant_computer = new AssistantComputer();
    assistant_computer->processor_ = processor_;
    assistant_computer->monitor_ = monitor_;
    assistant_computer->keyboard_ = keyboard_;
    return assistant_computer;
}

void AssistantComputer::ShowConfig(){
    cout << "Assistant ShowConfig:" << endl;
    cout << processor_ << endl;
    cout << monitor_ << endl;
    cout << keyboard_ << endl;
}

SoftwareComputer:

//software_computer.h
#ifndef HELENDP_SOURCE_SOFTWARE_COMPUTER_H_
#define HELENDP_SOURCE_SOFTWARE_COMPUTER_H_
#include 
#include "assistant_computer.h"
using namespace std;

class SoftwareComputer : public AssistantComputer{
public:
    SoftwareComputer();
    ~SoftwareComputer();
    SoftwareComputer* clone();
    void ShowConfig();
};
#endif


//software_computer.cc
#include "software_computer.h"
#include "assistant_computer.h"
#include 
using namespace std;

SoftwareComputer::SoftwareComputer():AssistantComputer(){
    keyboard_ = "Cherry";
}

SoftwareComputer::~SoftwareComputer(){

}

SoftwareComputer* SoftwareComputer::clone(){
    SoftwareComputer *software_computer = new SoftwareComputer();
    ((AssistantComputer *)software_computer)->clone();
    return software_computer;
}

void SoftwareComputer::ShowConfig(){
    cout << "software ShowConfig:" << endl;
    cout << processor_ << endl;
    cout << monitor_ << endl;
    cout << keyboard_ << endl;
}

代码和UML图(EA)工程文件,最后会整理打包上传.

UML类图

创建型模式-原型模式(prototype)_第1张图片

结构

  • Prototype(AssistantComputer):原型
  • ConcretePrototype(SoftwareComputer):具体原型,实现具体clone方法.

优点

  • 使用原型模式创建对象比直接new一个对象在性能上要好(上述代码没体现此优点,一般clone方法系统实现,可以达到这样的效果).
  • 使用原型模式的另一个好处是简化对象的创建.

缺点

  • 使用原型模式复制对象不会调用类的构造方法。
  • 深拷贝与浅拷贝问题。

你可能感兴趣的:(设计模式)