Τετάρτη 15 Ιουλίου 2020

Bridge Pattern (OOP)

Bridge pattern is like Adapter Pattern, but we have separate the abstraction from implementation. In Adapter pattern we need a ISomething interface for Client, and we get Adapter who inherits from ISomething. Inside Adapter is the Adaptee object. In Bridge we have a IAbstract (like ISomething) but we have more than one classes which inherits from IAbstract,  and maybe we have more than one Adaptee. Here we have a second interface the IImplementor and we have classes which inherits from it, the Implementor1, the Implementor2. The Abstract1 class comstructed with a pointer to a IImplementor type object (and all which inherits from them). We can extend IAbstract to Abstraction2 without affect the Implementor1 or Implementor2. We can extend both, IAbstract and IImplementor to Abstractrion3 and Implementor3, adding a method operation1.in Abstraction3 and operationImp2 in Implemantor3. The final M object is a IAbstract type.


\\ Bridge Pattern


class IAbstract {
      module operation {
            error "Abstract"
      }
}
Class Abstraction1 as iAbstract {
Private:
      impl=pointer()
Public:
      module operation {
            .impl=>operationImp
      }
Class:
      module Abstraction1 (p as *IImplementor) {
            .impl<=p
      }
}
Class Abstraction2 as iAbstract {
Private:
      impl=pointer()
Public:
      module operation {
            .impl=>operationImp
      }
      module operation1 {
            Print "extend Abstraction"
      }
Class:
      module Abstraction2 (p as *IImplementor) {
            .impl<=p
      }
}


Class IImplementor {
      module operationImp {
            error "Abstact"
      }
}
Class Implementor1 as IImplementor {
      module operationImp {
            Print "result from Implementor1"
      }      
}
Class Implementor2 as IImplementor {
      module operationImp {
            Print "result from Implementor2"
      }      
}
M=Abstraction1(Pointer(Implementor1()))
M.operation
M=Abstraction1(Pointer(Implementor2()))
M.operation
M=Abstraction2(Pointer(Implementor1()))
M.operation
M.operation1
\\ extends both Abstraction and Implementor
Class Abstraction3 as Abstraction2 {
Private:
      impl=pointer()
Public:
      module operation {
            .impl=>operationImp
      }
      module operation1 {
            .impl=>operationImp2
      }
Class:
      module Abstraction3 (p as *Implementor3) {
            .impl<=p
      }
}
Class Implementor3 as Implementor2 {
      module operationImp2 {
            Print "result 2 from Implementor3"
      }      
}
M=Abstraction3(Pointer(Implementor3()))
M.operation
M.operation1
Print M is type IAbstract
Print M is type Abstraction3


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.