DesignPattern/32.谈谈低耦合原则/02.Code/rightcode.cpp

41 lines
581 B
C++

#include <iostream>
#define ON true
#define OFF false
class Object
{
virtual ~Object(){}
virtual void on() = 0;
virtual void off() = 0;
};
class Lamp : public Object
{
public:
void on() override {
// ...
}
void off() override {
// ...
}
};
class Button
{
public:
Button(Object& obj): mObj(obj){}
void touch() {
if (mState == ON) {
mState = OFF;
mObj.off();
}
else {
mState = ON;
mObj.on();
}
}
private:
Object* mObj;
bool mState;
};