Wzorzec projektowy most - bridge
Stronę tą wyświetlono już: 4016 razy
Opis wzorca projektowego most
Wzorzec projektowy most należy do grupy wzorców strukturalnych. Umożliwia on oddzielenie interfejsu danych od interfejsu realizacji określonego zadania. Często też służy jako podział interfejsu układu sterującego od układu sterowanego (np. pilot - urządzenie). Ten wzorzec umożliwia stworzenie warstwy opisu ii oddzielnej warstwy realizacji np. pod różne platformy systemowe.
Przykładowy diagram UML wzorca projektowego most
Na poniższym diagramie UML pokazane zostało połączenie sterowania za pomocą pilota określonym urządzeniem. Istnieje tutaj interfejs iPilot, po którym dziedziczą klasy PilotLamp i PilotTV. To obiekty tych klas są tworzone przez programistę natomiast dostęp i operacje na nich są realizowane za pomocą interfejsu iPilot.
Interfejs iPilot agreguje interfejs iDevice w polu klasy device. Za interfejsem iDevice może stać jedna z dwóch klas: Lamp i TV. Te klasy odpowiadają za wykonanie sterowania a tworzenie ich instancji zostało zaszyte wewnątrz klas PilotLamp i PilotTV.
Przykładowa implementacja wzorca projektowego most
- #include <iostream>
- #include <string>
- class iDevice{
- public:
- virtual void on() = 0;
- virtual void off() = 0;
- };
- class Lamp : public iDevice{
- public:
- virtual void on(){
- std::cout << "Lamp state: (on)" << std::endl << std::endl;
- }
- virtual void off(){
- std::cout << "Lamp state: (off)" << std::endl << std::endl;
- }
- };
- class TV : public iDevice{
- public:
- virtual void on(){
- std::cout << "TV state: (on)" << std::endl << std::endl;
- }
- virtual void off(){
- std::cout << "TV state: (off)" << std::endl << std::endl;
- }
- };
- class iPilot{
- protected:
- iDevice *device;
- public:
- virtual void on() = 0;
- virtual void off() = 0;
- inline iPilot() : device(NULL){}
- virtual ~iPilot() {
- if(device){
- delete device;
- device = NULL;
- }
- }
- };
- class PilotTV : public iPilot{
- bool onOff;
- public:
- PilotTV() : onOff(false){
- device = new TV;
- }
- virtual void on(){
- if(!onOff){
- std::cout << "Turn on your God, turn on TV" << std::endl;
- onOff = true;
- if(device)
- device->on();
- }else{
- std::cout << "Your TV God is turned on already!!!" << std::endl << std::endl;
- }
- }
- virtual void off(){
- if(onOff){
- std::cout << "Turn off your God, turn off TV" << std::endl;
- onOff = false;
- if(device)
- device->off();
- }else{
- std::cout << "Your God is turned off already!!!" << std::endl << std::endl;
- }
- }
- virtual ~PilotTV(){
- std::cout << "Kill your God, kill your TV!!!" << std::endl << std::endl;
- }
- };
- class PilotLamp : public iPilot{
- bool onOff;
- public:
- PilotLamp() : onOff(false){
- device = new Lamp;
- }
- virtual void on(){
- if(!onOff){
- std::cout<<"Turn on your light, turn on Lamp" << std::endl;
- onOff = true;
- if(device)
- device->on();
- }else{
- std::cout<<"Your Lamp is turned on already!!!" << std::endl << std::endl;
- }
- }
- virtual void off(){
- if(onOff){
- std::cout<<"Turn off your light, turn off Lamp" << std::endl;
- onOff = false;
- if(device)
- device->off();
- }else{
- std::cout<<"Your Lamp is turned off already!!!" << std::endl << std::endl;
- }
- }
- virtual ~PilotLamp(){
- std::cout << "Kill your light, kill your Lamp!!!" << std::endl << std::endl;
- }
- };
- int main(){
- iPilot* tvPilot = new PilotTV;
- tvPilot->on();
- tvPilot->off();
- tvPilot->off();
- iPilot* lampPilot = new PilotLamp();
- lampPilot->on();
- lampPilot->on();
- lampPilot->off();
- std::cout<<"Cleaning memory mess" << std::endl << std::endl;
- if(tvPilot){
- delete tvPilot;
- tvPilot = 0;
- }
- if(lampPilot){
- delete lampPilot;
- lampPilot = NULL;
- }
- std::cin.get();
- return 0;
- }
Wynik działania powyższego kodu:
Turn on your God, turn on TV TV state: (on) Turn off your God, turn off TV TV state: (off) Your God is turned off already!!! Turn on your light, turn on Lamp Lamp state: (on) Your Lamp is turned on already!!! Turn off your light, turn off Lamp Lamp state: (off) Cleaning memory mess Kill your God, kill your TV!!! Kill your light, kill your Lamp!!!

Tytuł:
React 17. Wzorce projektowe i najlepsze praktyki. Projektowanie i rozwijanie nowoczesnych aplikacji internetowych. Wydanie III
Autor:
Carlos Santana Roldán

Tytuł:
Wzorce projektowe. Rusz głową! Tworzenie rozszerzalnego i łatwego w utrzymaniu oprogramowania obiektowego. Wydanie II
Autor:
Eric Freeman, Elisabeth Robson

Tytuł:
Wzorce projektowe w .NET Core 3. Projektowanie zorientowane obiektowo z wykorzystaniem C# i F#
Autor:
Dmitri Nesteruk

Tytuł:
Wzorce projektowe. Elementy oprogramowania obiektowego wielokrotnego użytku
Autor:
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

Tytuł:
Projektowanie interfejsów., Sprawdzone wzorce projektowe. Wydanie III
Autor:
Jenifer Tidwell, Charles Brewer, Aynne Valencia-Brooks

Tytuł:
Kubernetes. Wzorce projektowe. Komponenty wielokrotnego użycia do projektowania natywnych aplikacji chmurowych
Autor:
Bilgin Ibryam, Roland Huß

Tytuł:
Wzorce projektowe w .NET. Projektowanie zorientowane obiektowo z wykorzystaniem C# i F#
Autor:
Dmitri Nesteruk

Tytuł:
Programowanie zorientowane obiektowo. Wzorce projektowe. Wydanie II
Autor:
Alan Shalloway, James R. Trott

Tytuł:
Java EE 8. Wzorce projektowe i najlepsze praktyki
Autor:
Rhuan Rocha, Joao Purificacao

Tytuł:
Systemy reaktywne. Wzorce projektowe i ich stosowanie
Autor:
Roland Kuhn Dr., Brian Hanafee, Jamie Allen