-
总览
-
分析
-
适配器
A->B
需要借助辅助.适配器就是辅助.-
拓展坞,电脑只有
typec
,拓展坞提供了网线转type c
输入,usb
转typec
输入,hmmi
转typec
. -
拓展坞就是,拓展坞就是适配器类,拓展坞实现了
type c
接口,支持usb->c
.
#include<vector> #include<iostream> class Typec { public: virtual void Input() = 0; }; class Interface { public: virtual void Standard() = 0; }; class USB : public Interface { public: void Standard() { std::cout << "usb xie yi" << std::endl; } }; class HDMI : public Interface { public: void Standard() { std::cout << "hdmi xie yi" << std::endl; } }; class TuoZhanWu : public Typec { public: void ChaRu(Interface* jiekou) { interfaces.push_back(jiekou); } void Input() { for(auto jiekou:interfaces) { std::cout << "-----------------------" << std::endl; jiekou->Standard(); std::cout << "zhuan typec" << std::endl; } } private: std::vector<Interface*> interfaces; }; int main() { TuoZhanWu tuozhanwu; USB usb; HDMI hdmi; tuozhanwu.ChaRu(&usb); tuozhanwu.ChaRu(&hdmi); tuozhanwu.Input(); }
- 上面实现了拓展坞案例。将任意接口转
typec
输入.
-
桥接
#include<iostream> class Clothes { public: virtual void What() = 0; }; class Dress : public Clothes { public: void What() { std::cout << "qun zi" << std::endl; } }; class Jacket : public Clothes { public: void What() { std::cout << "jia ke" << std::endl; } }; class Person { public: virtual void show() = 0; protected: Clothes* clothes_; }; class Teacher : public Person { public: Teacher(Clothes* clothes){ this->clothes_ = clothes; } void show() { std::cout << "laoshi chuan le yishen" << std::endl; clothes_->What(); } }; class Doctor : public Person { public: Doctor(Clothes* clothes){ this->clothes_ = clothes; } void show() { std::cout << "yisheng chuan le yi shen" << std::endl; clothes_->What(); } }; int main() { Person* laoshi = new Teacher(new Dress()); Person* yisheng = new Doctor(new Jacket()); laoshi->show(); yisheng->show(); }
-
过滤器模式
#include<vector> #include<iostream> enum Size { SL = 0, L, XL, XXL, XXXL }; class Clothes { public: virtual void What() = 0; virtual int Size() = 0; }; class Dress : public Clothes { public: void What() { std::cout << "qun zi" << std::endl; } int Size() {return XL;} }; class Jacket : public Clothes { public: void What() { std::cout << "jia ke" << std::endl; } int Size() {return XXL;} }; class Child : public Clothes { public: void What() { std::cout << "tong zhuang" << std::endl; } int Size() {return SL;} }; class Filter { public: virtual std::vector<Clothes*> filter(std::vector<Clothes*>) = 0; }; class XL_XXL : public Filter { public: std::vector<Clothes*> filter(std::vector<Clothes*> clothes){ std::vector<Clothes*> temp; for(auto data:clothes) { if(XL == data->Size() || XXL == data->Size()) { temp.push_back(data); } } return temp; } }; class XLFilter : public Filter { public: std::vector<Clothes*> filter(std::vector<Clothes*> clothes){ std::vector<Clothes*> temp; for(auto data:clothes) { if(XL == data->Size()) { temp.push_back(data); } } return temp; } }; class MyFilter : public Filter { public: MyFilter(Filter* a,Filter* b):a(a),b(b){} std::vector<Clothes*> filter(std::vector<Clothes*> clothes) { auto ret = a->filter(clothes); return b->filter(ret); } private: Filter *a,*b; }; int main() { std::vector<Clothes*> clothes; clothes.push_back(new Dress()); clothes.push_back(new Jacket()); clothes.push_back(new Child()); Filter* filter1 = new XL_XXL(); Filter* filter2 = new XLFilter(); auto ret = filter1->filter(clothes); for(auto i:ret) { i->What(); } std::cout << "------------" << std::endl; auto ret2 = filter2->filter(ret); for(auto i:ret2) { i->What(); } std::cout << "------------" << std::endl; Filter * combine = new MyFilter(filter1,filter2); auto ret3 = combine->filter(clothes); for(auto i:ret3) { i->What(); } }
-
组合模式
#include<vector> #include<iostream> class Tree { public: Tree(bool add_ten = false) { if(add_ten) { for(int i = 0 ; i < 10; i++) { AddNode(new Tree()); } } } void AddNode(Tree* node) { nodes.push_back(node); } void show() { for(auto i: nodes) { std::cout << i << ":" << i->nodes.size() << std::endl; i->show(); } } private: std::vector<Tree*> nodes; }; int main() { Tree a; Tree b(true); Tree c(true); a.AddNode(&b); a.AddNode(&c); a.show(); }
-
装饰器
-
链表的形式组合自己,链表不仅仅是单链表可以是双向,可以是树或
B+
树之类的. -
核心在于动态的添加.
-
比如造句,核心是一个基本物体,然后一个装饰类.核心是多态。
-
代表末尾的本体和可以带链的装饰。甚至本体可以是带链的装饰,不过起始是没有链的。
-
继承了相同的接口,但是侧重点不一样。
#include<vector> #include<iostream> class Animal { public: virtual void Name() = 0; }; class Elephant : public Animal { public: void Name() override { std::cout << "da xiang" << std::endl; } }; class MiaoShu : public Animal { public: MiaoShu(Animal* miao_shu) : miao_shu_(miao_shu) { } void Name() override = 0; protected: Animal* miao_shu_; }; class ChiBang : public MiaoShu { public: ChiBang(Animal* miao_shu) : MiaoShu(miao_shu) { } void Name() override { std::cout << "hui fei," << std::endl; miao_shu_->Name(); } }; class KuiJia : public MiaoShu { public: KuiJia(Animal* miao_shu) : MiaoShu(miao_shu) { } void Name() override { std::cout << "kai jia," << std::endl; miao_shu_->Name(); } }; int main() { Animal* da_xiang = new Elephant(); MiaoShu * chi_bang = new ChiBang(da_xiang); MiaoShu * kui_jia = new KuiJia(chi_bang); kui_jia->Name(); }
-
-
外观模式
#include<iostream> class Button { public: virtual void push() = 0; }; class PinDaoJia : public Button { public: void push() override { std::cout << "xia yi ge pin dao." << std::endl; } }; class PinDaoJian : public Button { public: void push() override { std::cout << "shang yi ge pin dao." << std::endl; } }; class CaiDan : public Button { public: void push() override { std::cout << "cai dan." << std::endl; } }; class YaoKongQi { public: void Jia() { pin_dao_jia->push(); } void Jian() { pin_dao_jian->push(); } void Menu() { cai_dan->push(); } private: Button* pin_dao_jia = new PinDaoJia(); Button* pin_dao_jian = new PinDaoJian(); Button* cai_dan = new CaiDan(); }; int main() { YaoKongQi yaokong; yaokong.Jia(); yaokong.Jian(); yaokong.Menu(); }
-
享元模式
-
代理模式
#include<iostream> class Sale { public: virtual void Sign() = 0; }; class Owner : public Sale { public: void Sign(){ std::cout << "[qian he tong.]" << std::endl; } }; class Saler : public Sale { public: Saler(Sale* ownder):owner_(ownder){} void Sign(){ std::cout << "zhao maijia,kan fang,qian he tong,jian cha he tong." << std::endl; std::cout << "he tong mei wen ti." << std::endl; owner_->Sign(); std::cout << "shou qu zhong jie fei." << std::endl; } private: Sale* owner_; }; int main() { Sale * fang_dong = new Owner(); Saler * zhong_jie = new Saler(fang_dong); zhong_jie->Sign(); }
-
-
总结
C++ 15 接口类型(组合)设计模式总结
最新推荐文章于 2024-05-13 23:55:07 发布