
设计模式
让我们荡起双脚
信言不美,美言不信。
善者不辩,辩者不善。
知者不博,博者不知。
圣人不积,既以为人己愈有,既以与人己愈多。
天之道,利而不害;圣人之道,为而不争。
展开
-
Singleton单例模式
#include <iostream>#include <windows.h>#include <mutex>using namespace std;std::mutex _sMutex;template<typename Type>class Singleton{public: static Type* GetSigleton() { if (sin原创 2015-06-22 21:57:13 · 809 阅读 · 0 评论 -
设计模式:Mediator中介者模式
//main.h#pragma onceclass Mediator;class School{public: virtual void action()=0; virtual void Setname(const char *buff)=0;protected: Mediator *mt; char namebuff[10];};class Studentone : p原创 2015-08-28 22:49:42 · 644 阅读 · 0 评论 -
Memento记忆模式
//Memento记忆模式。#include <iostream>#include <string.h>using namespace std;class Memento;class Originator{public: Originator(char *s = "") { _str = new char[100]; strcpy(_st原创 2015-07-06 21:28:12 · 716 阅读 · 0 评论 -
Bridge桥接模式
#include <iostream>using namespace std;//Bridge桥接模式。class Base{public: virtual void Printf()=0;};class Son1 :public Base{public: void Printf() { cout << "Son1::Printf()" <<原创 2015-07-07 09:38:05 · 750 阅读 · 0 评论 -
Observer观察者模式
#include <iostream>#include <string.h>#include <vector>using namespace std;//观察者模式。//定义了一对多的关系,让多个观察对象同时监听一个主题对象,//当主题对象发生变化时,多个对象作出相应的响应。class School{public: School(char *s) {原创 2015-07-06 20:57:30 · 630 阅读 · 0 评论 -
C++再论单例模式
#include <iostream>#include <windows.h>#include <mutex>std::mutex gmutex;using namespace std;template<typename Type>class Singleton{public: static Type* GetSingleton() { if (s原创 2015-07-11 15:45:52 · 753 阅读 · 0 评论 -
State状态机模式
#include <iostream>using namespace std;class Contex;//即使此处声明了,如果类中调用的有Contex函数,//那么编译器也会找不到定义的地方,所有这里我将//所有的类的实现都延迟在外面,这样对于每一个类而言,//其他的类都是可见的,最大的好处就是类之间的相互调用//不会产生什么未定义类型的错误警告。class Base{publi原创 2015-07-05 09:22:40 · 955 阅读 · 0 评论 -
Stratgy策略模式
#include <iostream>using namespace std;class Base{public: void DoWhat() { this->Printf1(); this->Printf2(); } virtual void Printf1() = 0; virtual void Printf2() =原创 2015-07-01 10:18:09 · 622 阅读 · 0 评论 -
跟我一起透彻理解template模板模式
#include <iostream>using namespace std;//template模式。class Base{public: void DealWhat() { this->Printf1(); this->Printf2(); }protected: virtual void Printf1() = 0;原创 2015-06-30 14:53:59 · 839 阅读 · 0 评论 -
Prototype原型模型
#include<iostream>using namespace std;//从一个对象再创建另一个可定制的对象,无需知道任何的细节,并能提高创建的性能。class Base{public: virtual void Printf() = 0; virtual Base* Clone() { return 0; }private:};c原创 2015-06-26 23:05:42 · 681 阅读 · 0 评论 -
AbstractFactory抽象工厂模式
#include <iostream>using namespace std;class ProductAbase{public: ProductAbase(){} virtual ~ProductAbase(){} virtual void Printf() = 0;};class Product1 : public ProductAbase{public:原创 2015-06-24 21:38:31 · 1134 阅读 · 0 评论 -
Build生成者模型
#include <iostream>using namespace std;//不知道为什么其实很好解释的东西在网上搞的人晕头转向的,以下是我的理解。//一个基类衍生出许多具体实现的子类,这些子类都实现了虚基类的公共方法,//然后我们再构造一个Direver对象,让基类做为他的成员变量,在调用的时候//可以选择任意的子类对象传入然后可以调用该子类的方法,是不是很简单呢?//“生成器模式的原创 2015-06-26 10:56:42 · 671 阅读 · 0 评论 -
Factory工厂模式
#include <iostream>using namespace std;//基类。class Parent{public: virtual void Printf()=0;};class Child1 : public Parent{public: void Printf() { cout << "Child1::Printf()" <<原创 2015-06-24 19:54:42 · 701 阅读 · 0 评论 -
设计模式:解释者模式
#include #include //interpreter解释器模式。//一个解释类对封装的行为进行公共需求解释的一种方式。using namespace std;class Contex{public: Contex(string inputstr) { InterpreterStr = inputstr; }private: string Interpreter原创 2015-09-30 22:14:04 · 856 阅读 · 0 评论