添加原型模式的使用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. // Created by 29019 on 2020/5/18.
// //
#include "factory.hpp" #include "factory.hpp"
#include <iostream>
using namespace std;
class Product :public CloneAble<Product>{ class Product :public CloneAble<Product>{
public: public:
Product(){
mAttribute1 = -1;
mAttribute2 = '1';
}
Product *Clone(){ Product *Clone(){
Product *ret = new Product; Product *ret = new Product;
ret->mAttribute1 = this->mAttribute1; ret->mAttribute1 = this->mAttribute1;
ret->mAttribute2 = this->mAttribute2; ret->mAttribute2 = this->mAttribute2;
return ret; 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: private:
int mAttribute1; int mAttribute1;
char mAttribute2; char mAttribute2;
}; };
int main(){ int main(){
Product z; Product *z = new Product;
Prototype<Product> x(&z); z->SetAttr1(-11);
x.GetProduct(); z->SetAttr2('c');
z->Show();
Prototype<Product> x(z);
delete(z);
// 即使delete掉了原型也能接着使用
auto tbl3 = x.GetProduct();
tbl3->Show();
} }