日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

8-9设计模式复习

發布時間:2025/3/19 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 8-9设计模式复习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/* 設計模式 23種 ?*/
/* 開放封閉原則: 類的改動 是通過增加代碼來實現的 ?不是修改源代碼 */
/* 比如一個類 實現了各種功能 但是后期想修改某個功能就很難 那我們把它寫成純虛函數 讓繼承的類去實現 */

#include <iostream> #include <windows.h>using namespace std;/*class Bankworker//一開始的想法 一個類實現多個操作 { public:void savemoney(){cout << " savemoney " << endl;}void getmoney(){cout << " getmoney " << endl;}void transformmoney(){cout << " transformmoney " << endl;} };*/class Bankworker//把這個類函數變成 純虛函數 { public:/*virtual void savemoney() = 0;virtual void getmoney() = 0;virtual void transformmoney() = 0;*/virtual void work() = 0;};class Savework : public Bankworker//繼承的類進行實現 savemoney { public:void work(){cout << " savemoney " << endl;}};class Getwork : public Bankworker//繼承的類進行實現 getmoney { public:void work(){cout << " getmoney " << endl;}};class transformwork : public Bankworker//繼承的類進行實現 transformmoney { public:void work(){cout << " transformmoney " << endl;}};int main() {/*Bankworker *b1 = new Bankworker;b1->getmoney();b1->savemoney();*/Bankworker *b1 = NULL;b1 = new Savework;b1->work();b1 = new Getwork;b1->work();b1 = new transformwork;b1->work();system("pause");return 0; }

/* 依賴倒置原則 : */

/* #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; }

?

總結

以上是生活随笔為你收集整理的8-9设计模式复习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。