添加原型模式的使用sample

master
a7458969 2020-05-19 00:59:32 +08:00
parent 204218c71b
commit 9e0e0ff7ca
1 changed files with 25 additions and 4 deletions

View File

@ -2,21 +2,42 @@
// Created by 29019 on 2020/5/18.
//
#include "factory.hpp"
#include <iostream>
using namespace std;
class Product :public CloneAble<Product>{
public:
Product(){
mAttribute1 = -1;
mAttribute2 = '1';
}
Product *Clone(){
Product *ret = new Product;
ret->mAttribute1 = this->mAttribute1;
ret->mAttribute2 = this->mAttribute2;
return ret;
}
void SetAttr1( char x){
this->mAttribute1 = x;
}
void SetAttr2(char x){
this->mAttribute2 = x;
}
void Show(){
std::cout<<mAttribute1<<" "<<mAttribute2<<" "<<std::endl;
}
private:
int mAttribute1;
char mAttribute2;
};
int main(){
Product z;
Prototype<Product> x(&z);
x.GetProduct();
Product *z = new Product;
z->SetAttr1(-11);
z->SetAttr2('c');
z->Show();
Prototype<Product> x(z);
delete(z);
// 即使delete掉了原型也能接着使用
auto tbl3 = x.GetProduct();
tbl3->Show();
}