一、原型模式
#include <iostream> #include <string> using namespace std; class Person { protected: string name; int age; public: Person() { } Person(string s,int a) { this->name = s; this->age = a; } public: virtual void show() = 0; virtual Person *clone() = 0; }; class Student :public Person { private: int id; public: Student() { } Student(string s,int a,int i):Person(s,a) { this->id = i; } void show() { cout << "name = " << name << " age = " << age << " id = " << id << endl; } Person *clone() { Student *tmp = new Student; *tmp = *this; return tmp; } }; int main(int argc, char const *argv[]) { Person *p1 = new Student("zzz", 18, 21); Person *p2 = p1->clone(); p2->show(); return 0; }
二、组合模式
#include <iostream> #include <string> #include <list> using namespace std; class iFile { public: virtual int Add(iFile *file) = 0; virtual string Getname() = 0; virtual int remove(iFile *file) = 0; virtual list<iFile *> *GetChild() = 0; }; class File:public iFile { private: string name; public: File(string n) { name = n; } virtual int Add(iFile *file) { return -1; } virtual string Getname() { return name; } virtual int remove(iFile *file) { return -1; } virtual list<iFile *> *GetChild() { return nullptr; } }; class Dir:public iFile { private: string name; list<iFile *> *l; public: Dir(string n) { name = n; l = new list<iFile *>; } virtual int Add(iFile *file) { l->emplace_back(file); return 1; } virtual string Getname() { return name; } virtual int remove(iFile *file) { l->remove(file); return 1; } virtual list<iFile *> *GetChild() { return l; } }; void show(iFile *root, int gap) { for (size_t i = 0; i < gap; i++) { cout << "---"; } if (root != NULL) { cout << root->Getname() << endl; } else { return; } auto l = root->GetChild(); if (l != nullptr) { for (auto it = l->begin(); it != l->end();it++) { show((*it), gap + 1); } } } int main(int argc, char const *argv[]) { iFile *dir1 = new Dir("day1"); iFile *dir2 = new Dir("day2"); iFile *dir3 = new Dir("day3"); iFile *file1 = new File("1.c"); iFile *file2 = new File("2.c"); iFile *file3 = new File("3.c"); iFile *file4 = new File("4.c"); iFile *file5 = new File("5.c"); iFile *file6 = new File("6.c"); iFile *file7 = new File("7.c"); dir2->Add(file1); dir2->Add(file2); dir2->Add(file3); dir2->Add(file4); dir2->Add(file5); dir2->Add(file6); dir3->Add(file1); dir3->Add(file3); dir3->Add(file5); dir3->Add(file7); dir1->Add(dir2); dir1->Add(dir3); show(dir1, 0); return 0; }
三、代理模式
#include <iostream> #include <string> using namespace std; class Sale { public: virtual void SaleBook() = 0; }; class BookStore:public Sale { private: static int count; public: void SaleBook() { cout << "书店卖书" << endl; count++; } static int GetCount() { return count; } }; class TaoBao:public Sale { private: Sale *sale; public: TaoBao(Sale *s) { sale = s; } void SaleBook() { cout << "网上卖书" << endl; sale->SaleBook(); } }; int BookStore::count = 0; int main(int argc, char const *argv[]) { Sale *store = new BookStore; Sale *taobao = new TaoBao(store); taobao->SaleBook(); store->SaleBook(); cout << BookStore::GetCount() << endl; return 0; }
四、装饰模式
#include <iostream> #include <string> using namespace std; class Phone { private: string name; public: virtual void function() = 0; }; class CallPhone:public Phone { private: Phone *phone; public: // CallPhone(Phone *p) // { // phone = p; // } void function() { // phone->function(); cout << "打电话的功能" << endl; } }; class MusicPhone:public Phone { private: Phone *phone; public: MusicPhone(Phone *p) { phone = p; } void function() { phone->function(); cout << "听音乐的功能" << endl; } }; class PhotoPhone:public Phone { private: Phone *phone; public: PhotoPhone(Phone *p) { phone = p; } void function() { phone->function(); cout << "相机的功能" << endl; } }; int main(int argc, char const *argv[]) { cout << "******************************" << endl; Phone *callPhone = new CallPhone(); callPhone->function(); cout << "******************************" << endl; Phone *musicphone = new MusicPhone(callPhone); musicphone->function(); cout << "******************************" << endl; Phone *photophone = new PhotoPhone(musicphone); photophone->function(); return 0; }
五、适配器模式
#include <iostream> using namespace std; class Current { public: virtual int GetCurrent() = 0; }; class Current_220:public Current { public: int GetCurrent() { return 220; } }; class Adapter:public Current { private: Current *current; public: Adapter(Current *c) { current = c; } int GetCurrent() { return 12; } }; class Phone { private: Current *current; public: Phone(Current *c) { current = c; } void check() { if (current->GetCurrent() == 220) { cout << "手机爆炸" << endl; } else if(current->GetCurrent() == 12) { cout << "手机正在充电" << endl; } } }; int main(int argc, char const *argv[]) { Current *c = new Current_220(); Current *a = new Adapter(c); Phone *p = new Phone(a); p->check(); return 0; }