目录
- 【设计模式专题之工厂方法模式】2.积木工厂
- [【设计模式专题之抽象工厂模式】3. 家具工厂](#【设计模式专题之抽象工厂模式】3. 家具工厂)
【设计模式专题之工厂方法模式】2.积木工厂
文章链接:卡码网设计模式
题目链接:2.积木工厂
注意C++默认的构造函数是public的,但不写public时默认的数据和方法是private的,struct默认的数据和方法是public的。
cpp
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Block{
public:
virtual void produce() = 0;
};
class CircleBlock : public Block{
public:
void produce() override{
cout << "Circle Block" << endl;
}
};
class SquareBlock : public Block{
public:
void produce() override{
cout << "Square Block" << endl;
}
};
class BlockFactory{
public:
virtual Block* createBlock() = 0;
};
class CircleBlockFactory : public BlockFactory{
public:
Block* createBlock() override{
return new CircleBlock();
}
};
class SquareBlockFactory : public BlockFactory{
public:
Block* createBlock() override{
return new SquareBlock();
}
};
class BlockFactorySystem{
private:
vector<Block*> blocks;
public:
void produceBlocks(BlockFactory* factory, int quantity){
for (int i = 0; i < quantity; i++){
Block* block = factory->createBlock();
blocks.push_back(block);
block->produce();
}
}
~ BlockFactorySystem(){
for (Block* item : blocks){
delete item;
}
}
};
int main(){
BlockFactorySystem factorysystem;
int allcount;
cin >> allcount;
for (int i = 0; i < allcount; i++){
string blocktype;
int quantity;
cin >> blocktype >> quantity;
if (blocktype == "Circle"){
factorysystem.produceBlocks(new CircleBlockFactory(), quantity);
}
else if (blocktype == "Square"){
factorysystem.produceBlocks(new SquareBlockFactory(), quantity);
}
}
return 0;
}
【设计模式专题之抽象工厂模式】3. 家具工厂
文章链接:卡码网设计模式
题目链接:3. 家具工厂
注意跟工厂方法模式的区别,工厂方法模式一个工厂生产一类产品,抽象工厂模式一个工厂生产一类产品。
cpp
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
class Chair{
public:
virtual void showInfo() = 0;
};
class MordenChair : public Chair{
public:
void showInfo() override{
cout << "modern chair" << endl;
}
};
class ClassicalChair : public Chair{
public:
void showInfo() override{
cout << "classical chair" << endl;
}
};
class Sofa{
public:
virtual void displayInfo() = 0;
};
class MordenSofa : public Sofa{
public:
void displayInfo() override{
cout << "modern sofa" << endl;
}
};
class ClassicalSofa : public Sofa{
public:
void displayInfo() override{
cout << "classical sofa" << endl;
}
};
class Factory{
public:
virtual Chair* produceChair() = 0;
virtual Sofa* produceSofa() = 0;
};
class ModernFactory : public Factory{
public:
Chair* produceChair() override{
return new MordenChair();
}
Sofa* produceSofa() override{
return new MordenSofa();
}
};
class ClassicalFactory : public Factory{
public:
Chair* produceChair() override{
return new ClassicalChair();
}
Sofa* produceSofa() override{
return new ClassicalSofa();
}
};
int main(){
int N;
cin >> N;
for (int i = 0; i < N; i++){
string type;
cin >> type;
Factory* factory = nullptr;
if (type == "modern"){
factory = new ModernFactory();
}else if (type == "classical"){
factory = new ClassicalFactory();
}
Chair* chair = factory -> produceChair();
Sofa* sofa = factory -> produceSofa();
chair->showInfo();
sofa->displayInfo();
delete factory;
delete chair;
delete sofa;
}
return 0;
}
设计模式第二天打卡,加油!!!