8-9设计模式复习
/* 設計模式 23種 ?*/
/* 開放封閉原則: 類的改動 是通過增加代碼來實現的 ?不是修改源代碼 */
/* 比如一個類 實現了各種功能 但是后期想修改某個功能就很難 那我們把它寫成純虛函數 讓繼承的類去實現 */
/* 依賴倒置原則 : */
/* #include <iostream> #include <windows.h>using namespace std;class Ram2G { public:void Ramwork(){cout << "Ramwork 2G" << endl;} };class Rom32G { public:void Romwork(){cout << "Romwork 32G" << endl;} };class Phone//依賴于實現 { private:Ram2G *Ra;Rom32G *Ro; public:Phone(Ram2G *a, Rom32G *b){Ra = a;Ro = b;}void Phonework(){Ra->Ramwork();Ro->Romwork();}};int main() {Ram2G *Ra2 = new Ram2G;Rom32G *Ro32 = new Rom32G;Phone *p = new Phone(Ra2, Ro32);p->Phonework();system("pause");return 0; } */#include <iostream> #include <windows.h>using namespace std;class Ram { public:virtual void Ramwork() = 0; }; class Ram2G : public Ram { public:void Ramwork(){cout << "Ramwork 2G" << endl;} }; class Ram4G : public Ram { public:void Ramwork(){cout << "Ramwork 4G" << endl;} };class Rom { public:virtual void Romwork() = 0; }; class Rom32G : public Rom { public:void Romwork(){cout << "Romwork 32G" << endl;} }; class Rom64G : public Rom { public:void Romwork(){cout << "Romwork 64G" << endl;} };class Phone { private:Ram *Ra;Rom *Ro; public:Phone(Ram *a, Rom *b){Ra = a;Ro = b;}void Phonework(){Ra->Ramwork();Ro->Romwork();} };int main() {/*Ram *Ra = new Ram2G;Rom *Ro = new Rom32G;Phone *p = new Phone(Ra, Ro);p->Phonework();delete Ra;delete Ro;delete p;*/Ram *Ra1 = new Ram4G;Rom *Ro1 = new Rom64G;Phone *p1 = new Phone(Ra1, Ro1);p1->Phonework();delete Ra1;delete Ro1;delete p1;system("pause");return 0; }/* 工廠模式 */
#include <iostream> #include <windows.h> #include <stdlib.h>using namespace std;class car { public:virtual void show() = 0; };class benz : public car { public:void show(){cout << "benz" << endl;} };class baoma : public car { public:void show(){cout << "baoma" << endl;} };class carfactory { public:virtual car * showcar() = 0; };class factorybaoma :public carfactory { public:car * showcar(){return new baoma;} };class factorybenz :public carfactory { public:car * showcar(){return new benz;} };int main() {carfactory *f = new factorybaoma;f->showcar()->show();delete f;system("pause");return 0; }/* 抽象工廠 */
#include <iostream> #include <windows.h> #include <stdlib.h>using namespace std;class fruit { public :virtual void show() = 0; };class Northapple : public fruit { public:void show(){cout << " Northapple " << endl;} };class Northpear : public fruit { public:void show(){cout << " Northpear " << endl;} };class Sorthapple : public fruit { public:void show(){cout << " Sorthapple " << endl;} };class Sorthpear : public fruit { public:void show(){cout << " Sorthpear " << endl;} };class factory { public:virtual fruit *createapple() = 0;virtual fruit *createpear() = 0; };class Northfactory : public factory { public:fruit *createapple(){return new Northapple;}fruit *createpear(){return new Northpear;} };class Sorthfactory : public factory { public:fruit *createapple(){return new Sorthapple;}fruit *createpear(){return new Sorthpear;} };int main() {factory *n1 = new Northfactory;n1->createapple()->show();n1->createpear()->show();delete n1;factory *n2 = new Sorthfactory;n2->createapple()->show();n2->createpear()->show();delete n2;system("pause");return 0; }/* 建造者模式 */
#include <iostream> #include <windows.h> #include <stdlib.h> #include <string>using namespace std;class house { private:string wall;string door;string window; public:void setwall(string w){wall = w;}void setdoor(string d){door = d;}void setwindow(string wi){window = wi;}string getwall(){return wall;}string getdoor(){return door;}string getwindow(){return window;} };class builder { private:house *h; public:builder(){h = new house;}void constuctor(){h->setdoor("door");h->setwall("wall");h->setwindow("window");}void getinfo(){cout << h->getdoor() << endl;cout << h->getwall() << endl;cout << h->getwindow() << endl;} };int main() {builder *b1 = new builder;b1->constuctor();b1->getinfo();system("pause");return 0; }/* 建造者模式改良版 */
#include <iostream> #include <windows.h> #include <stdlib.h> #include <string>using namespace std;class house { private:string wall;string door;string window; public:void setwall(string w){wall = w;}void setdoor(string d){door = d;}void setwindow(string wi){window = wi;}string getwall(){return wall;}string getdoor(){return door;}string getwindow(){return window;} };class builder { private:house *h; public:virtual void constuctor() = 0;virtual void getinfo() = 0; };class buildernormal : public builder { private:house *h; public:buildernormal(){h = new house;}void constuctor(){h->setdoor("doornormal");h->setwall("wallnormal");h->setwindow("windownormal");}void getinfo(){cout << h->getdoor() << endl;cout << h->getwall() << endl;cout << h->getwindow() << endl;} };class buildervilla : public builder { private:house *h; public:buildervilla(){h = new house;}void constuctor(){h->setdoor("doorvilla");h->setwall("wallvilla");h->setwindow("windowvilla");}void getinfo(){cout << h->getdoor() << endl;cout << h->getwall() << endl;cout << h->getwindow() << endl;} };int main() {builder *b = new buildernormal;b->constuctor();b->getinfo();builder *b1 = new buildervilla;b1->constuctor();b1->getinfo();system("pause");return 0; }/* 原型模式 */
#include <iostream>using namespace std;class Person { protected:string name; public:Person(){}Person(string n){name = n;}string GetName(){return name;}virtual void show() = 0;virtual Person *clone() = 0; };class Student : public Person { private:int id; public:Student(){ }Student(string n, int i) : Person(n){id = i;}void show(){cout << name << " " << id << endl;}Person *clone(){Student *tmp = new Student;*tmp = *this;return tmp;} };int main() {Person *s1 = new Student("aa", 1);s1->show();Person *s2 = s1->clone();s2->show();return 0; }/* 組合模式 */
#include <iostream> #include <list>using namespace std;class BaseFile { public:virtual string GetName() = 0;virtual int Add(BaseFile *file) = 0;virtual int Remove(BaseFile *file) = 0;virtual list<BaseFile *> *GetChild() = 0; };class IFile : public BaseFile { private:string name; public:IFile(string n){name = n;}string GetName(){return name;}int Add(BaseFile *file){return -1;}int Remove(BaseFile *file){return -1;}list<BaseFile *> *GetChild(){return NULL;} };class Dir : public BaseFile { private:string name;list<BaseFile *> *l; public:Dir(string n){name = n;l = new list<BaseFile *>;}string GetName(){return name;}int Add(BaseFile *file){l->push_back(file);return 1;}int Remove(BaseFile *file){l->remove(file);return 1;}list<BaseFile *> *GetChild(){return l;} };void show(BaseFile *root, int gap) {for (int i = 0; i < gap; i++){ cout << "---";}if (root != NULL){cout << root->GetName() << endl;}else{return;}list<BaseFile *> *l = root->GetChild();if (l != NULL){for (list<BaseFile *>::iterator it = l->begin(); it != l->end(); it++){show((*it), gap + 1);}} }int main() { BaseFile *root = new Dir("root");BaseFile *dir1 = new Dir("home");BaseFile *file1 = new IFile("1.c");BaseFile *file2 = new IFile("2.c");BaseFile *file3 = new IFile("3.c");BaseFile *file4 = new IFile("4.c");BaseFile *file5 = new IFile("5.c");BaseFile *dir2 = new Dir("hello");dir1->Add(file1);dir1->Add(file2);dir1->Add(file3);dir1->Add(file4);dir1->Add(file5);dir2->Add(file1);dir2->Add(file2);dir2->Add(file3);dir2->Add(file4);dir2->Add(file5);root->Add(dir1);root->Add(dir2);/*list<BaseFile *> *l = dir1->GetChild();for (list<BaseFile *>::iterator it = l->begin(); it != l->end(); it++){cout << (*it)->GetName() << endl;}*/show(root, 0);return 0; }?
總結
- 上一篇: 方案一TCP 完成聊天室的编写
- 下一篇: 8-10 牛客网刷题知识点集合