课时44设计模式简介
设计模式简介
面向对象设计的第一个原则:针对接口编程,而不是针对实现编程
接口->指针
实现->实例
若已存在一个类Class A,现在希望复用Class A,则有以下两种方法。
通过继承实现复用
通过组合实现复用
面向对象设计的第二个原则:优先使用对象组合,而不是类继承
课时45观察者模式
如果两个对象是松耦合的,则他们可以相互作用,但彼此的依赖性很小。
#include <iostream> #include <assert.h> #include <list> class Observer { public: virtual ~Observer() = 0 { } virtual void update(float, float, float) = 0; }; class Subject { public: virtual ~Subject() = 0 { } virtual void registerObserver(Observer *) = 0; virtual void removeObserver(Observer *) = 0; virtual void notifyObserver() const = 0; }; class CurrentCondition :public Observer { public: CurrentCondition() :m_temperature(0.0), m_humidity(0.0) { } ~CurrentCondition() { } void update(float temperature, float humitity, float pressure) { m_temperature = temperature; m_humidity = humitity; m_pressure = pressure; display(); } void display() { std::cout.setf(std::ios::showpoint); std::cout.precision(3); std::cout << "Current condition " << m_temperature; std::cout << "C, and " << m_humidity << "% humidity" << std::endl; } private: float m_temperature; float m_humidity; float m_pressure; }; class ForecastCondition :public Observer { public: ForecastCondition() :m_currentPressure(0.0), m_lastPressure(0.0) { } ~ForecastCondition() { } void update(float temperature, float humitity, float pressure) { m_lastPressure = m_currentPressure; m_currentPressure = pressure; display(); } void display() { std::cout.setf(std::ios::showpoint); std::cout.precision(3); std::cout << "Current condition " << m_temperature; std::cout << "C, and " << m_humidity << "% humidity" << std::endl; } private: float m_temperature; float m_humidity; float m_pressure; float m_lastPressure; float m_currentPressure; }; class WeatherData :public Subject { public: WeatherData() :m_temperature(0.0), m_humidity(0.0), m_pressure(0.0) { } ~WeatherData() { } void registerObserver(Observer *o) { assert(o); m_observers.push_back(o); } void removeObserver(Observer *o) { m_observers.remove(o); } void notifyObserver()const { for (std::list<Observer *>::const_iterator it = m_observers.begin(); it != m_observers.end(); ++it) { Observer *obj = *it; obj->update(m_temperature, m_humidity, m_pressure); } } void setMeasurements(float temperature, float humitity, float pressure) { m_temperature = temperature; m_humidity = humitity; m_pressure = pressure; } private: float m_temperature; float m_humidity; float m_pressure; std::list<Observer*> m_observers; private: WeatherData(const WeatherData&); }; int main() { CurrentCondition current; ForecastCondition forecast; WeatherData weatherdata; weatherdata.registerObserver(¤t); weatherdata.registerObserver(&forecast); weatherdata.setMeasurements(14, 65, 30.4f); weatherdata.setMeasurements(19, 70, 29.2f); weatherdata.setMeasurements(12, 90, 29.2f); system("pause"); return 0; }
课时46策略模式
#include <iostream> #include <assert.h> #include <memory> class FlyBehavior//飞的抽象类 { public: virtual ~FlyBehavior() = 0 { } virtual void fly() const = 0; }; class FlyNoWay :public FlyBehavior//不会飞 { public: void fly() const { std::cout << "I can't fly" << std::endl; } }; class FlyRocketPowered :public FlyBehavior//火箭飞 { public: void fly() const { std::cout << "I'm flying with a rocket" << std::endl; } }; class FlyWithWings :public FlyBehavior//翅膀飞 { public: void fly() const { std::cout << "I'm flying" << std::endl; } }; class QuackBehavior//叫的抽象类 { public: virtual ~QuackBehavior() = 0 { } virtual void quack() const = 0; }; class FakeQuack :public QuackBehavior//假叫 { public: void quack() const { std::cout << "Qwak" << std::endl; } }; class MuteQuack :public QuackBehavior//不叫 { public: void quack() const { std::cout << "Silence" << std::endl; } }; class Quack :public QuackBehavior//嘎 { public: void quack() const { std::cout << "Quack" << std::endl; } }; class Squeak :public QuackBehavior//吱 { public: void quack() const { std::cout << "Squeak" << std::endl; } }; class Duck//鸭的抽象类 { public: void setFlyBehavior(FlyBehavior *fb) { assert(fb); _flyBehavior = std::auto_ptr<FlyBehavior>(fb); } void setQuackBehavior(QuackBehavior *qb) { assert(qb); _quackBehavior = std::auto_ptr<QuackBehavior>(qb); } void performFly() const { _flyBehavior->fly(); } void performQuack() const { _quackBehavior->quack(); } void swim() const { std::cout << "All ducks float, even decoys!" << std::endl; } virtual void display() const = 0; protected: Duck(FlyBehavior *flyBehavior, QuackBehavior *quackBehavior) :_flyBehavior(flyBehavior), _quackBehavior(quackBehavior) { assert(flyBehavior); assert(quackBehavior); } virtual ~Duck() { } private: std::auto_ptr<FlyBehavior>_flyBehavior;//智能指针 std::auto_ptr<QuackBehavior>_quackBehavior; Duck(const Duck&);//Disable copy constructor void operator=(const Duck&);//Disable assignment operator }; class DecoyDuck :public Duck//诱饵鸭 { public: DecoyDuck() :Duck(new FlyNoWay(), new MuteQuack()) { } void display() const { std::cout << "I'm a duck Decoy" << std::endl; } }; class MallardDuck :public Duck//野鸭 { public: MallardDuck() :Duck(new FlyWithWings(), new Quack()) { } void display() const { std::cout << "I'm a real Mallard duck" << std::endl; } }; class ModeldDuck :public Duck//假鸭 { public: ModeldDuck() :Duck(new FlyNoWay(), new FakeQuack()) { } void display() const { std::cout << "I'm a model duck" << std::endl; } }; class RedHead :public Duck//红头鸭 { public: RedHead() :Duck(new FlyWithWings(), new Quack()) { } void display() const { std::cout << "I'm a real Red Head duck" << std::endl; } }; class RubberDuck :public Duck//橡皮鸭 { public: RubberDuck() :Duck(new FlyNoWay(), new Squeak()) { } void display() const { std::cout << "I'm a rubber duck" << std::endl; } }; int main() { std::auto_ptr<MallardDuck>mallard(new MallardDuck()); std::auto_ptr<RubberDuck>rubberduck(new RubberDuck()); std::auto_ptr<DecoyDuck>decoy(new DecoyDuck()); std::auto_ptr<ModeldDuck>model(new ModeldDuck()); mallard->performFly(); mallard->performQuack(); rubberduck->performFly(); rubberduck->performQuack(); decoy->performFly(); decoy->performQuack(); model->performFly(); model->performQuack(); model->setFlyBehavior(new FlyRocketPowered()); model->performFly(); system("pause"); return 0; }