Headfirst设计模式的C++实现——复合模式
observer.h
#ifndef _OBSERVER_H_
#define _OBSERVER_H_ #include <string>
class Observer {
public:
virtual void update(const std::string &type) = ;
};
#endif
quack_observable.h
#ifndef _QUACK_OBSERVABLE_H_
#define _QUACK_OBSERVABLE_H_ #include "observer.h" class QuackObservable {
public:
virtual void register_observer(Observer *observer) = ;
virtual void notify_observers() = ;
};
#endif
duck.h
#ifndef _DUCK_H_
#define _DUCK_H_ #include "quack_observable.h"
class Duck : public QuackObservable{
public:
virtual void quack() = ;
};
#endif
observable.h
#ifndef _OBSERVABLE_H_
#define _OBSERVABLE_H_ #include "quack_observable.h"
#include <vector> class Observable : public QuackObservable {
private:
std::vector<Observer *> observers;
const std::string type;
public:
Observable( const std::string &_type ) : type(_type) {}
void register_observer(Observer *observer) { observers.push_back( observer ); }
void notify_observers() {
for ( std::vector<Observer *>::iterator it = observers.begin();
it < observers.end();
it ++ ) {
(*it)->update( type );
}
}
};
#endif
mallard_duck.h
#ifndef _MALLARD_DUCK_H_
#define _MALLARD_DUCK_H_ #include <iostream>
#include "duck.h"
#include "observable.h" class MallardDuck : public Duck{
private:
Observable observable;
public:
MallardDuck() :observable("Mallard duck") {}
void quack() { std::cout << "Quack" << std::endl; notify_observers(); }
void register_observer(Observer *observer) { observable.register_observer(observer); }
void notify_observers() { observable.notify_observers(); }
}; #endif
redhead_duck.h
#ifndef _REDHEAD_DUCK_H_
#define _REDHEAD_DUCK_H_ #include <iostream>
#include "duck.h"
#include "observable.h"
class RedheadDuck : public Duck{
private:
Observable observable;
public:
RedheadDuck() :observable("Redhead Duck") {}
void quack() { std::cout << "Quack" << std::endl; notify_observers();}
void register_observer(Observer *observer) { observable.register_observer(observer); }
void notify_observers() { observable.notify_observers(); }
}; #endif
duck_call.h
#ifndef _DUCK_CALL_H_
#define _DUCK_CALL_H_ #include <iostream>
#include "duck.h"
#include "observable.h" class DuckCall : public Duck{
private:
Observable observable;
public:
DuckCall() :observable("Duck call") {}
void quack() { std::cout << "Kwak" << std::endl; notify_observers(); }
void register_observer(Observer *observer) { observable.register_observer(observer); }
void notify_observers() { observable.notify_observers(); }
}; #endif
rubber_duck.h
#ifndef _RUBBER_DUCK_H_
#define _RUBBER_DUCK_H_ #include <iostream>
#include "duck.h"
#include "observable.h"
class RubberDuck : public Duck{
private:
Observable observable;
public:
RubberDuck() :observable("Rubber Duck") {}
void quack() { std::cout << "Squeak" << std::endl; notify_observers();}
void register_observer(Observer *observer) { observable.register_observer(observer); }
void notify_observers() { observable.notify_observers(); }
}; #endif
countable_duck.h
#ifndef _COUNTEABLE_DUCK_H_
#define _COUNTEABLE_DUCK_H_ #include "duck.h"
class CountableDuck : public Duck{
public:
CountableDuck( Duck *_duck ) : duck(_duck) {}
void quack() { duck->quack(); quack_count ++; }
void register_observer(Observer *observer) { duck->register_observer(observer); }
void notify_observers() { duck->notify_observers(); }
static int get_quack_count() { return quack_count; }
private:
Duck *duck;
static int quack_count;
};
#endif
countable_duck.cpp
#include "countable_duck.h" int CountableDuck::quack_count = ;
ivector.h
#ifndef _IVECTOR_H_
#define _IVECTOR_H_ #include <vector>
#include <stdlib.h> template<class T> class IVector {
public:
IVector() : pos() {}
void add( const T& t ) { t_v.push_back(t); }
bool has_next() { return pos < t_v.size(); }
T* next() {
if ( has_next() ) {
return &(t_v[pos++]);
}
return NULL;
}
void back_to_begin() { pos = ; }
private:
int pos;
std::vector<T> t_v;
};
#endif
duck_factory.h
#ifndef _DUCK_FACTORY_H_
#define _DUCK_FACTORY_H_ #include "mallard_duck.h"
#include "redhead_duck.h"
#include "duck_call.h"
#include "rubber_duck.h"
#include "countable_duck.h"
#include <string> class SimpleDuckFactory {
public:
static Duck* create_duck( std::string type ) {
if ( type == "mallard" ) { return new CountableDuck( new MallardDuck ); }
if ( type == "redhead" ) { return new CountableDuck( new RedheadDuck ); }
if ( type == "call" ) { return new CountableDuck( new DuckCall ); }
if ( type == "rubber" ) { return new CountableDuck( new RubberDuck ); }
return NULL;
}
}; class AbstractDuckFactory {
public:
static Duck *create_mallard_duck() { return new CountableDuck( new MallardDuck ); }
static Duck *create_redhead_duck() { return new CountableDuck( new RedheadDuck ); }
static Duck *create_duck_call() { return new CountableDuck( new DuckCall ); }
static Duck *create_rubber_duck() { return new CountableDuck( new RubberDuck ); }
};
#endif
flock.h
#ifndef _FLOCK_H_
#define _FLOCK_H_ #include "duck.h"
#include "ivector.h" class Flock : public Duck{
public:
void quack() {
ducks.back_to_begin();
while( ducks.has_next() ) {
(*(ducks.next()))->quack();
}
}
void add( Duck *duck ) { ducks.add(duck); }
void register_observer(Observer *observer) {
ducks.back_to_begin();
while( ducks.has_next() ) {
(*(ducks.next()))->register_observer(observer);
}
}
void notify_observers() {
ducks.back_to_begin();
while( ducks.has_next() ) {
(*(ducks.next()))->notify_observers();
}
}
private:
IVector<Duck *> ducks;
};
#endif
goose.h
#ifndef _GOOSE_H_
#define _GOOSE_H_ #include <iostream>
class Goose {
public:
void honk() { std::cout << "Honk" << std::endl; }
};
#endif
goose_adapter.h
#ifndef _GOOSE_ADAPTER_H_
#define _GOOSE_ADAPTER_H_ #include "goose.h"
#include "duck.h"
#include "observable.h" class GooseAdapter : public Duck{
public:
GooseAdapter( Goose *_goose ) : goose(_goose), observable("Goose pretending to be a duck") {}
void quack() { goose->honk(); notify_observers();}
void register_observer(Observer *observer) { observable.register_observer(observer); }
void notify_observers() { observable.notify_observers(); }
private:
Goose *goose;
Observable observable;
};
#endif
quackologist.h
#ifndef _QUACK_OLOGIST_H_
#define _QUACK_OLOGIST_H_ #include "observer.h"
#include <iostream>
#include <string> class QuackOlogist : public Observer {
public:
void update(const std::string &type ) { std::cout << "Quackologist: " << type << " just quacked." << std::endl; }
};
#endif
main.cpp
#include "goose_adapter.h"
#include "duck_factory.h"
#include "countable_duck.h"
#include <iostream>
#include "flock.h"
#include "quackologist.h" void simulate(Duck *duck) {
if ( NULL != duck ) { duck->quack(); }
} int main() {
Flock *flock_of_mallard = new Flock;
flock_of_mallard->add( AbstractDuckFactory::create_mallard_duck() );
flock_of_mallard->add( AbstractDuckFactory::create_mallard_duck() ); Flock *flock_of_ducks = new Flock; flock_of_ducks->add( new CountableDuck(new GooseAdapter( new Goose ))); flock_of_ducks->add( AbstractDuckFactory::create_redhead_duck() );
flock_of_ducks->add( AbstractDuckFactory::create_duck_call() );
flock_of_ducks->add( AbstractDuckFactory::create_rubber_duck() ); flock_of_ducks->add( flock_of_mallard ); QuackOlogist quackologist;
flock_of_ducks->register_observer(&quackologist); simulate( flock_of_ducks ); std::cout << "quack count: " << CountableDuck::get_quack_count() << std::endl;
};
Headfirst设计模式的C++实现——复合模式的更多相关文章
- headfirst设计模式(5)—工厂模式体系分析及抽象工厂模式
先编一个这么久不写的理由 上周我终于鼓起勇气翻开了headfirst设计模式这本书,看看自己下一个设计模式要写个啥,然后,我终于知道我为啥这么久都没写设计模式了,headfirst的这个抽象工厂模式, ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂
简单工厂:工厂依据传进的参数创建相应的产品. http://www.cnblogs.com/DelphiDesignPatterns/archive/2009/07/24/1530536.html { ...
- 《Head First 设计模式》学习笔记——复合模式
模型-视图-控制器(MVC模式)是一种很经典的软件架构模式.在UI框架和UI设计思路中扮演着很重要的角色.从设计模式的角度来看,MVC模式是一种复合模式.它将多个设计模式在一种解决方式中结合起来,用来 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi代码---工厂模式之抽象工厂[转]
1 2 {<HeadFirst设计模式>工厂模式之抽象工厂 } 3 { 抽象工厂的产品 } 4 { 编译工具:Delphi7.0 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus[转]
1 2{<HeadFirst设计模式>之组合模式 } 3{ 组合与单项的抽象父类 } 4{ 编译工具:Delphi2007 for win32} 5{ E-M ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---工厂模式之工厂方法[转]
1 2{<HeadFirst设计模式>工厂模式之工厂方法 } 3{ 产品类 } 4{ 编译工具 :Delphi20 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转]
模板方法模式定义了一个算法骨架,允许子类对算法的某个或某些步骤进行重写(override). 1 2{<HeadFirst设计模式>之模板方法模式 } 3{ 编译工具: Del ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---迭代器模式之DinerMenu[转]
容器的主要职责有两个:存放元素和浏览元素.根据单一职责原则(SRP)要将二者分开,于是将浏览功能打包封装就有了迭代器. 用迭代器封装对动态数组的遍历: 1 2{<HeadFirst设计模式& ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---策略模式之MiniDuckSimulator[转]
1 2{<HeadFirst设计模式>之策略模式 } 3{ 本单元中的类为策略类 } 4{ 编译工具: Delphi7.0 } 5{ E- ...
随机推荐
- MKMapView的内存释放问题
MKMapView的内存释放问题 by 伍雪颖 - (void)dealloc { self.mapView.showsUserLocation = NO; self.mapView.userTrac ...
- mysql 5.1 到 mysql 5.2的出现的索引BTREE问题 use near 'USING BTREE
转自:http://hi.baidu.com/our_poll/item/669c5ce885b33ff1e0a5d4fc 我本机测试是安装的 mysql 5.1 , 但服务器上确是使用的 mysql ...
- Swift计算属性
除存储属性外,类.结构体和枚举可以定义计算属性,计算属性不直接存储值,而是提供一个 getter 来获取值,一个可选的 setter 来间接设置其他属性或变量的值. struct Point { va ...
- online ddl 工具之pt-online-schema-change
MySQL ddl 的问题现状 在运维mysql数据库时,我们总会对数据表进行ddl 变更,修改添加字段或者索引,对于mysql 而已,ddl 显然是一个令所有MySQL dba 诟病的一个功能, ...
- Python dictionary implementation
Python dictionary implementation http://www.laurentluce.com/posts/python-dictionary-implementation/ ...
- Javascript-获取URL请求参数
function getUrlParam(){ var param = [], hash; var url = window.location.href;//获取网页的url va ...
- 【张泽华】android视频教程下载地址及上课源代码
http://note.youdao.com/share/?id=f39bf918842c7b0673a033d35d734073&type=notebook#/1AC357745ED74BC ...
- HDU--杭电--1195--Open the Lock--深搜
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- oracle--varchar2
1. varchar2列最多占用4000字节,但是能容纳多少字符就不一定了 使用sql查看字符集: select userenv('language') from dual;--select * fr ...
- Samba服务详解
Samba文件服务器 本章结构 服务简介 SMB协议 Server Message Block,服务消息块 CIFS协议 Common Internet File System,通用互联网文件系统 S ...