C++設計モード——作成型モード
5683 ワード
ソースクリック(https://github.com/bob-young/CPPDesignPattern ) .
Singleton
特徴:クラスにインスタンスが1つしかないことを確認し、独自にインスタンス化し、システム全体にインスタンスを提供します.
アプリケーション:スレッドプール、ファイルIOなどで、単一オブジェクトの頻繁な作成と破棄に対応し、リソース間の相互通信を容易にする.
Factory
特徴:工場を使用してサブクラスのインスタンス化を制御する
適用:異なる条件で異なるインスタンスを作成する場合を明確に計画します.
Abstract Factory
特徴:スーパーファクトリをめぐって他のファクトリを作成し、インタフェースを分離し、各ファクトリはインタフェースのクラスのインスタンス化を担当します.
適用:異なる機能では、オブジェクトを個別に作成および制御する必要があります.
例:apple社とSamsung生産製品シナリオ
Builder
特徴:複数の簡単なオブジェクトを使用して一歩一歩複雑なオブジェクトに構築され、開閉原則、リス置換、変化と不変を分離します.
≪適用|Apply|emdw≫:いくつかの基本部品は変更されず、その組み合わせが常に変化する場合.
例:ファーストフード店セット
Prototype
特徴:このモードは、現在のオブジェクトのクローンを作成するためのプロトタイプインタフェースを実現します.
適用:作成オブジェクトの種類をプロトタイプインスタンスで指定し、迅速なdeepcopyを実現します.
このモードは、オブジェクトを直接作成するコストが大きい場合に使用します.たとえば、オブジェクトは、高いコストのデータベース操作の後に作成される必要があります.オブジェクトをキャッシュし、次のリクエスト時にクローンを返し、必要に応じてデータベースを更新してデータベース呼び出しを減らすことができます.
Singleton
特徴:クラスにインスタンスが1つしかないことを確認し、独自にインスタンス化し、システム全体にインスタンスを提供します.
アプリケーション:スレッドプール、ファイルIOなどで、単一オブジェクトの頻繁な作成と破棄に対応し、リソース間の相互通信を容易にする.
#include
class Singleton {
private:
Singleton(){
std::cout<
Factory
特徴:工場を使用してサブクラスのインスタンス化を制御する
適用:異なる条件で異なるインスタンスを作成する場合を明確に計画します.
#include
class Product{
public:
virtual void getType(){std::cout<
Abstract Factory
特徴:スーパーファクトリをめぐって他のファクトリを作成し、インタフェースを分離し、各ファクトリはインタフェースのクラスのインスタンス化を担当します.
適用:異なる機能では、オブジェクトを個別に作成および制御する必要があります.
例:apple社とSamsung生産製品シナリオ
#include
//product
class laptop{
public:
virtual void info()=0;
laptop();
virtual ~laptop();
};
class smartphone{
public:
virtual void info()=0;
smartphone();
virtual ~smartphone();
};
class samsungLaptop:public laptop{
public:
void info(){
std::cout<
Builder
特徴:複数の簡単なオブジェクトを使用して一歩一歩複雑なオブジェクトに構築され、開閉原則、リス置換、変化と不変を分離します.
≪適用|Apply|emdw≫:いくつかの基本部品は変更されず、その組み合わせが常に変化する場合.
例:ファーストフード店セット
#include
#include
#include
class Pack{
public:
virtual std::string getContainer()=0;
};
class Wrapper : public Pack{
public:
std::string getContainer(){
return "wrapper";
}
};
class Bottle : public Pack{
public:
std::string getContainer(){
return "bottle";
}
};
class Item{
public:
virtual std::string getName()=0 ;
virtual float getPrice()=0 ;
virtual Pack* getPackage()=0;
};
class Burger:public Item{
public:
Pack* getPackage(){
return new Wrapper();
}
};
class Drink:public Item{
public:
Pack* getPackage(){
return new Bottle();
}
};
class VegBurger :public Burger{
public:
std::string getName(){
return "vegburger";
}
float getPrice(){
return 10.0;
}
};
class ChickenBurger:public Burger{
public:
std::string getName(){
return "chicken burger";
}
float getPrice(){
return 15.0;
}
};
class Coca:public Drink{
public:
std::string getName(){
return "cocacola";
}
float getPrice(){
return 3.0;
}
};
class Spirit:public Drink{
public:
std::string getName(){
return "spirit";
}
float getPrice(){
return 2.5;
}
};
class Meal{
std::vector- food;
public:
void addItem(Item* i){
food.push_back(i);
}
float getTotalPrice(){
float totalPrice=0.0;
for(int i=0;i
getPrice();
}
return totalPrice;
}
void showItems(){
for(int i=0;igetName();
std::cout<getPackage()->getContainer();
std::cout<getPrice()<<:endl for="" i="food.size()-1;i!=0;i--){" delete="" food="" class="" builder="" public:="" virtual="" meal="" getmeal="" vegbuilder:public="" m="new" item="" burger="new" vegburger="" m-="">addItem(burger);
Item* drink=new Coca();
m->addItem(drink);
return m;
}
};
class MeatBuilder:public Builder{
public:
Meal* getMeal(){
Meal* m=new Meal();
Item* burger=new ChickenBurger;
m->addItem(burger);
Item* drink=new Spirit();
m->addItem(drink);
return m;
}
};
Prototype
特徴:このモードは、現在のオブジェクトのクローンを作成するためのプロトタイプインタフェースを実現します.
適用:作成オブジェクトの種類をプロトタイプインスタンスで指定し、迅速なdeepcopyを実現します.
このモードは、オブジェクトを直接作成するコストが大きい場合に使用します.たとえば、オブジェクトは、高いコストのデータベース操作の後に作成される必要があります.オブジェクトをキャッシュし、次のリクエスト時にクローンを返し、必要に応じてデータベースを更新してデータベース呼び出しを減らすことができます.
#include
class Prototype {
public:
int *ip=(int*)malloc(sizeof(int));
int counter=100;
virtual Prototype* clone()=0;
};
class cloneablePrototype:public Prototype{
public:
cloneablePrototype();
Prototype* clone();
private:
cloneablePrototype(const cloneablePrototype&);//copy
cloneablePrototype& operator=(cloneablePrototype const& cloneablePrototype1 );//ban on =operator
};
cloneablePrototype::cloneablePrototype() {
}
Prototype *cloneablePrototype::clone() {
return new cloneablePrototype(*this);
}
cloneablePrototype:: cloneablePrototype(const cloneablePrototype& c){
//pass your stack value and heap value here
counter=c.counter;
*(ip)=*(c.ip);
}//copy