feature: add lowering coupling discussions and methods.

master
FengJungle 2022-02-27 11:50:35 +08:00
parent a206c10a5c
commit 8071fabb51
6 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,5 @@
{
"files.associations": {
"iosfwd": "cpp"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -0,0 +1,41 @@
#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;
};

View File

@ -0,0 +1,34 @@
#include <iostream>
#define ON true
#define OFF false
class Lamp
{
public:
void on() {
// ...
}
void off() {
// ...
}
};
class Button
{
public:
Button(Lamp& lamp): mLamp(lamp){}
void touch() {
if (mState == ON) {
mState = OFF;
mLamp.off();
}
else {
mState = ON;
mLamp.on();
}
}
private:
Lamp& mLamp;
bool mState;
};

View File

@ -141,3 +141,8 @@ Jungle设计模式系列
博客地址https://blog.csdn.net/sinat_21107433/article/details/123150564
32.啊,如何遵循“低耦合”设计原则?
博客地址https://blog.csdn.net/sinat_21107433/article/details/123160949