[Design Pattern] Strukturelle Muster: Bridge


Thomas Darimont

Premium-User
Hallo,

dieser Beitrag erklärt das Strukturelle Muster: Bridge
Java:
package de.tutorials.design.patterns.structural;

import org.jpatterns.gof.BridgePattern;
import org.jpatterns.gof.BridgePattern.RefinedAbstraction;

public class BridgePatternExample {
  public static void main(String[] args) {
    
    Client c = new Client();
    //c.use(new BB()); //compile time error
    
    c.use(new Bridge_A_to_B(new BB()));
    
  }
  
  static class Client{
    public void use(A a){
      a.operationA();
    }
  }
  
  @BridgePattern.Abstraction
  static abstract class A{
    public abstract void operationA();
  }
  
  @BridgePattern.Implementor
  static abstract class B{
    public abstract void operationB();
  }
  
  @BridgePattern.ConcreteImplementor
  static class BB extends B{
    public void operationB(){
      System.out.println("operationB"); 
    }
  }
  
  @RefinedAbstraction
  static class Bridge_A_to_B extends A{

    private B b;
    
    public Bridge_A_to_B(BB bb) {
     this.b = bb;
    }
    
    @Override
    public void operationA() {
      this.b.operationB();
    }
  }
}
Ausgabe:
Code:
operationB
Gruß Tom