
C++设计模式初探
文章平均质量分 79
FanTasyCC
这个作者很懒,什么都没留下…
展开
-
简单工厂模式(C++描述)
一.简单工厂模式又称静态工厂方法模式(Static Factory Method),它不是Gof 所讲的23种设计模式之一,但是它却是我们在编码过程中经常使用的方法之一。 1.静态工厂方法统一管理对象的创建。静态工厂方法通过传入的参数判断决定创建哪一个产品的实例,封装了对象的创建,客户端只管消费,实现了对责任(模块)的分割。2.静态工厂方法推迟了产品的实例化。通过XML配置文件就能改变原创 2009-03-12 22:43:00 · 18907 阅读 · 7 评论 -
TemplateMethod模式
<br />// ProxyTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;//定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。TemplateMethod 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤class AbstractClass{public:原创 2010-06-21 21:51:00 · 785 阅读 · 0 评论 -
Proxy模式
<br />// ProxyTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;//为其他对象提供一种代理以控制对这个对象的访问class ISubject{public: virtual int Add(int a, int b) = 0; virtual int Sub(i原创 2010-06-21 20:31:00 · 1158 阅读 · 0 评论 -
Decorator模式
<br />// DecoratorTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>//动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator 模式相//比生成子类更为灵活class IShow{public: virtual void Singing(void) = 0;};class CT原创 2010-06-20 22:09:00 · 808 阅读 · 0 评论 -
Composite模式
<br />// compositeTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <Windows.h>//将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对//单个对象和组合对象的使用具有一致性#include <list>#include <string>#include <algorithm>#原创 2010-06-20 16:01:00 · 890 阅读 · 0 评论 -
Adapt模式
// builderTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"class IAdd{public: virtual int Add(int a, int b) = 0;};//有一个类实现了这个功能(利用已有的轮子,避免重复发明)class COtherAdd{public: virtual int OtherAdd(int a, in原创 2010-06-19 22:39:00 · 1700 阅读 · 0 评论 -
Prototype模式
<br />class IOS {public: virtual IOS* Clone(void) const = 0;};class CWinodowXP : public IOS{public: virtual IOS* Clone(void) const { return new CWinodowXP(*this);//用了默认的拷贝构造函数 }};class CWindow7 : public IOS{public: virtua原创 2010-06-19 22:10:00 · 1334 阅读 · 0 评论 -
Bridge模式
<br />// builderTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"//实现类的抽象基类,定义了实现Abastraction的基本操作,而它的派生类实现这些接口class Implementor{public: virtual void Operation(void) = 0;};class CAImplementor : public原创 2010-06-19 23:26:00 · 669 阅读 · 0 评论 -
Builder模式
<br />// builderTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"//将一个复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。(GoF)class CDoor{};class CWindow{};class CWall{};class CFloor{};class CCeiling{};原创 2010-06-19 17:00:00 · 968 阅读 · 0 评论 -
单例模式的一种实现方法
class CSingleton{private: /* 禁掉构造函数 拷贝构造函数 */ CSingleton(void); CSingleton(const CSingleton& oth);public: static CSingleton& Instance(void) { static CSingleton s_singleton; retur原创 2010-03-16 21:18:00 · 1440 阅读 · 0 评论 -
工厂方法模式(C++描述)
工厂方法模式是对简单工厂模式的改进。首先看看简单工厂模式的缺点。软件是之所以区别于程序,是因为它可以被人们使用,并能间接创造效益。需求是软件开发的核心,忽视用户的需求,软件本身就没有存在的价值。假如Nokia又新开发了一款新手机N99,对于采用简单工厂模式设计的系统,我们的做法是:1.新增一个CN99类继承自CNokia抽象类,这个符合OCP原则。注:OCP:Open-Clos原创 2009-03-14 00:48:00 · 10390 阅读 · 8 评论