1. 工厂方法实现计算器的四则运算
#include <iostream>
#include <memory>
using namespace std;
class cal
{
public:
float num1() { return a; }
void num1(float x) { a = x; }
float num2() { return b; }
void num2(float x) { b = x; }
virtual float result() = 0;
private:
float a;
float b;
};
class add : public cal
{
public:
float result()
{
return num1() + num2();
}
};
class sub : public cal
{
public:
float result()
{
return num1() - num2();
}
};
class mul : public cal
{
public:
float result()
{
return num1() * num2();
}
};
class dev : public cal
{
public:
float result()
{
if (num2() == 0)
{
cout << "除数为0!" << endl;
return 0;
}
else
{
return num1() / num2();
}
}
};
class factory
{
public:
virtual shared_ptr<cal> getcal() = 0;
};
class addf : public factory
{
public:
shared_ptr<cal> getcal()
{
return shared_ptr<cal>(new add());
}
};
class subf : public factory
{
public:
shared_ptr<cal> getcal()
{
return shared_ptr<cal>(new sub());
}
};
class mulf : public factory
{
public:
shared_ptr<cal> getcal()
{
return shared_ptr<cal>(new mul());
}
};
class devf : public factory
{
public:
shared_ptr<cal> getcal()
{
return shared_ptr<cal>(new dev());
}
};
int main()
{
shared_ptr<factory> factory(new mulf);
shared_ptr<cal> c = factory->getcal();
c->num1(3);
c->num2(4);
cout << c->result() << endl;
return 0;
}
2. 建造者模式实现造一栋普通房子和一栋别墅:门,墙,房顶,
#include <iostream>
using namespace std;
class house
{
private:
string m_house;
string m_door;
string m_wall;
string m_roof;
public:
void sethouse(string house) { m_house = house; }
void setdoor(string door) { m_door = door; }
void setwall(string wall) { m_wall = wall; }
void setroof(string roof) { m_roof = roof; }
void display()
{
cout << m_house << "," << m_door << "," << m_wall << "," << m_roof << endl;
}
};
class Assemble
{
public:
virtual void assemblehouse() = 0;
virtual void assembledoor() = 0;
virtual void assemblewall() = 0;
virtual void assembleroof() = 0;
virtual house *gethouse() = 0;
};
class Assemblecommon : public Assemble
{
private:
house *h;
public:
Assemblecommon() { h = new house; }
void assemblehouse() { h->sethouse("普通房子"); }
void assembledoor() { h->setdoor("机械门"); }
void assemblewall() { h->setwall("石灰墙"); }
void assembleroof() { h->setroof("瓷砖瓦"); }
house *gethouse()
{
return h;
}
};
class Assemblecottage : public Assemble
{
private:
house *h;
public:
Assemblecottage() { h = new house; }
void assemblehouse() { h->sethouse("别墅"); }
void assembledoor() { h->setdoor("密码门"); }
void assemblewall() { h->setwall("大理石墙"); }
void assembleroof() { h->setroof("琉璃瓦"); }
house *gethouse()
{
return h;
}
};
class constructor
{
public:
constructor(Assemble *assemble)
{
m_Assemble = assemble;
}
void assemblehouse()
{
m_Assemble->assemblehouse();
m_Assemble->assembledoor();
m_Assemble->assemblewall();
m_Assemble->assembleroof();
}
house *gethouse()
{
return m_Assemble->gethouse();
}
private:
Assemble *m_Assemble;
};
int main()
{
Assemble *a = new Assemblecommon;
constructor *c = new constructor(a);
c->assemblehouse();
c->gethouse()->display();
delete c;
delete a;
a = new Assemblecottage;
c = new constructor(a);
c->assemblehouse();
c->gethouse()->display();
delete c;
delete a;
return 0;
}
3. 包装模式实现一个手抓饼材料的添加代码,继承输出:培根肉松榨菜老干妈手抓饼
#include <iostream>
using namespace std;
class food
{
public:
virtual void show() = 0;
};
class SZB : public food
{
public:
SZB() {}
SZB(string n) : name(n) {}
void show()
{
cout << name << endl;
}
private:
string name;
};
class decorator : public food
{
public:
void decorate(food *f)
{
this->f = f;
}
void show()
{
if (f != NULL)
{
f->show();
}
}
protected:
food *f;
};
class bacon : public decorator
{
public:
void show()
{
cout << "培根";
decorator::show();
}
};
class floss : public decorator
{
public:
void show()
{
cout << "肉松";
decorator::show();
}
};
class zhacai : public decorator
{
public:
void show()
{
cout << "榨菜";
decorator::show();
}
};
class laoganma : public decorator
{
public:
void show()
{
cout << "老干妈";
decorator::show();
}
};
int main()
{
food *f = new SZB("手抓饼");
bacon *ba = new bacon;
floss *fl = new floss;
zhacai *zh = new zhacai;
laoganma *la = new laoganma;
la->decorate(f);
zh->decorate(la);
fl->decorate(zh);
ba->decorate(fl);
ba->show();
return 0;
}
|