I was wondering if both of examples (or neither of them) are bridge pattern. So the first one:
class ButtonImpl {
public:
virtual void click() = 0;
};
// concrete implementation
class ToggleButtonImpl : public ButtonImpl {
public:
void click() {
cout << "Push\n";
}
};
// concrete implementation
class PushButtonImpl : public ButtonImpl {
public:
void click() {
cout << "Push\n";
cout << "Pop\n";
}
};
// abstraction
class Button {
public:
Button(ButtonImpl* buttonImpl) : m_buttonImpl{ buttonImpl }
{
}
void click() {
m_buttonImpl->click();
}
private:
ButtonImpl* m_buttonImpl;
};
and the second one:
// implementation
class ButtonImpl {
public:
virtual void click() = 0;
};
// concrete implementation
class WindowsButtonImpl : public ButtonImpl {
public:
void click() { cout << "windows system\n"; }
};
// concrete implementation
class LinuxButtonImpl : public ButtonImpl {
public:
void click() { cout << "linux system\n"; }
};
// abstraction
class Button {
public:
Button(ButtonImpl* buttonImpl) : m_buttonImpl{ buttonImpl } {}
virtual void click() = 0;
protected:
ButtonImpl* m_buttonImpl;
};
// concrete abstraction
class ToggleButton : public Button {
public:
ToggleButton(ButtonImpl* buttonImpl) : Button(buttonImpl) {};
void click() {
m_buttonImpl->click();
if(state) {
cout << "Push\n";
state = 0;
} else {
cout << "Pop\n";
state = 1;
}
}
private:
bool state;
};
// concrete abstraction
class PushButton : public Button {
public:
PushButton(ButtonImpl* buttonImpl) : Button(buttonImpl) {};
void click() {
m_buttonImpl->click();
cout << "Push\n";
cout << "Pop\n";
}
};
I'm constantly thinking that both examples are somehow correct with structure of this pattern, and in my opinion with its description. But I cannot find any proper exaplanation to answer this question.