1、设计模式的类型
- 创建型
- 结构型
- 行为型
1.1 创建型设计模式
- 1.1.1 简单工厂
- 1.1.2 工厂方法
- 1.1.3 抽象工厂
- 1.1.4 生成器
- 1.1.5 原型
- 1.1.6 单例
1.1.1 简单工厂
#include <iostream>
class IDoor {
public:
virtual float GetWidth() = 0;
virtual float GetHeight() = 0;
};
class WoodenDoor : public IDoor {
public:
WoodenDoor(float width, float height): m_width(width), m_height(height){}
float GetWidth() override { return m_width; }
float GetHeight() override { return m_height; }
protected:
float m_width;
float m_height;
};
class DoorFactory {
public:
static IDoor* MakeDoor(float width, float heigh)
{
return new WoodenDoor(width, heigh);
}
};
int main()
{
IDoor* door = DoorFactory::MakeDoor(100, 200);
std::cout << "Width: " << door->GetWidth() << std::endl;
std::cout << "Height: " << door->GetHeight() << std::endl;
}
1.1.2 工厂方法
#include <iostream>
class IInterviewer
{
public:
virtual void askQuestions() = 0;
};
class Developer : public IInterviewer
{
public:
void askQuestions() override {
std::cout << "Asking about design patterns!" << std::endl;
}
};
class CommunityExecutive : public IInterviewer
{
public:
void askQuestions() override {
std::cout << "Asking about community building!" << std::endl;
}
};
class HiringManager
{
public:
void takeInterview() {
IInterviewer* interviewer = this->makeInterviewer();
interviewer->askQuestions();
}
protected:
virtual IInterviewer* makeInterviewer() = 0;
};
template <typename Interviewer>
class OtherManager : public HiringManager {
protected:
IInterviewer* makeInterviewer() override {
return new Interviewer();
}
};
int main()
{
HiringManager* devManager = new OtherManager<Developer>();
devManager->takeInterview();
HiringManager* marketingManager = new OtherManager<CommunityExecutive>();
marketingManager->takeInterview();
}
1.1.3 抽象工厂
#include <iostream>
class IDoor {
public:
virtual void GetDescription() = 0;
};
class WoodenDoor : public IDoor {
public:
void GetDescription() override {
std::cout << "I am a wooden door" << std::endl;
}
};
class IronDoor : public IDoor {
public:
void GetDescription() override {
std::cout << "I am a iron door" << std::endl;
}
};
class IDoorFittingExpert {
public:
virtual void GetDescription() = 0;
};
class Carpenter : public IDoorFittingExpert {
void GetDescription() override {
std::cout << "I can only fit wooden doors" << std::endl;
}
};
class Welder : public IDoorFittingExpert {
void GetDescription() override {
std::cout << "I can only fit iron doors" << std::endl;
}
};
class IDoorFactory {
public:
virtual IDoor* MakeDoor() = 0;
virtual IDoorFittingExpert* MakeFittingExpert() = 0;
};
template <typename Door, typename DoorFittingExpert>
class DoorFactory : public IDoorFactory {
public:
IDoor* MakeDoor() override {
return new Door();
}
IDoorFittingExpert* MakeFittingExpert() override {
return new DoorFittingExpert();
}
};
int main()
{
IDoorFactory* woodenFactory = new DoorFactory<WoodenDoor, Carpenter>();
{
IDoor* door = woodenFactory->MakeDoor();
IDoorFittingExpert* expert = woodenFactory->MakeFittingExpert();
door->GetDescription();
expert->GetDescription();
}
IDoorFactory* ironFactory = new DoorFactory<IronDoor, Welder>();
{
IDoor* door = ironFactory->MakeDoor();
IDoorFittingExpert* expert = ironFactory->MakeFittingExpert();
door->GetDescription();
expert->GetDescription();
}
}
1.1.4 生成器
#include <iostream>
class Burger {
public:
class BurgerBuilder;
void showFlavors() {
std::cout << size_;
if (cheese_) std::cout << "-cheese";
if (peperoni_) std::cout << "-peperoni";
if (lettuce_) std::cout << "-lettuce";
if (tomato_) std::cout << "-tomato";
std::cout << std::endl;
}
private:
Burger(int size): size_(size) {}
int size_ = 7;
bool cheese_ = false;
bool peperoni_ = false;
bool lettuce_ = false;
bool tomato_ = false;
};
class Burger::BurgerBuilder {
public:
BurgerBuilder(int size) { burger_ = new Burger(size); }
BurgerBuilder& AddCheese() { burger_->cheese_ = true; return *this; }
BurgerBuilder& AddPepperoni() { burger_->peperoni_ = true; return *this; }
BurgerBuilder& AddLettuce() { burger_->lettuce_ = true; return *this; }
BurgerBuilder& AddTomato() { burger_->tomato_ = true; return *this; }
Burger* Build() { return burger_; }
private:
Burger* burger_;
};
int main()
{
Burger* burger = Burger::BurgerBuilder(14).AddPepperoni().AddLettuce().AddTomato().Build();
burger->showFlavors();
}
1.1.5 原型
#include <iostream>
#include <string>
class Sheep {
public:
Sheep(const std::string& name, const std::string& category = "Mountain Sheep") : name_(name), category_(category) {}
void SetName(const std::string& name) { name_ = name; }
void ShowInfo() { std::cout << name_ << " : " << category_ << std::endl; }
private:
std::string name_;
std::string category_;
};
int main()
{
Sheep jolly("Jolly");
jolly.ShowInfo();
Sheep dolly(jolly); // copy constructor
dolly.SetName("Dolly");
dolly.ShowInfo();
Sheep doolly = jolly; // copy assignment
doolly.SetName("Doolly");
doolly.ShowInfo();
}
1.1.6 单例
#include <iostream>
#include <string>
#include <cassert>
class President {
public:
static President& GetInstance() {
static President instance;
return instance;
}
President(const President&) = delete;
President& operator=(const President&) = delete;
private:
President() {}
};
int main()
{
const President& president1 = President::GetInstance();
const President& president2 = President::GetInstance();
assert(&president1 == &president2); // same address, point to same object.
}