Design Patterns - Bridge Patterns

bridge mode

definition

Separating the abstract part from the implementation (behavior) part so they can both change independently.
The approach of the bridge pattern is to abstract the change part (implementation), and separate the change part from the main class (abstract),
so as to completely separate the changes of multiple dimensions. Finally, provide a management class to combine changes in different dimensions to meet business needs through this combination.


Specific case

This case is to realize the function of installing an engine in a
car





1. Traditional methods

public interface Car{
public void installEngine200();
public void installEngine300();
}    
    
public class Benz implements Car{
@Override
public void installEngine200(){
System.out.println("Benz车组装Engine200");
    }
@Override
public void installEngine300(){
System.out.println("Benz车组装Engine300");
    }
}                                

public class Bwm implements Car{
@Override
public void installEngine200(){
System.out.println("Bwm车组装Engine200");
    }
@Override
public void installEngine300(){
System.out.println("Benz车组装Engine300");
    }
}                                


public class Client {
 public static void main ( String [] args ) {
 //Install 200 engine in Mercedes-Benz
 Car benz = new Benz ();
 benz .installEngine200 ();
 // Install 300 engine in BMW
 Car bwm ​​= new Bwm () ;
 bwm .installEngine300 ()
 ;     }
 }

                                                    

Disadvantages: As long as an engine type method is added to the Car interface, an empty implementation must also be added to its specific implementation class (if the car does not need this engine), such as adding a model 400 engine
      corresponding to Mercedes-Benz and BMW This method has to be added in the middle, but my Mercedes-Benz car does not need this kind of engine, which causes the redundancy of the code, so this method is not easy to expand, so the second method is used: bridge mode






  
  
  

2. Use bridge mode

//Implementor : Engine defines the implementation interface (that is, the engine interface).
// with the implementation (behavior) part
public interface Engine {
public void addEngine();
}    

//ConcreteImplementor : Engine200 ; Engine300 implements the methods in the engine interface.
//Concrete implementation of the interface
public class Engine200 implements Engine {
@Override
public void addEngine() {
System.out.println("组装Engine200");
    }
}                

public class Engine300 implements Engine {
@Override
public void addEngine() {
System.out.println("组装Engine300");
    }
}                



//Abstraction : Car defines an abstract interface.
// abstract part
public abstract class Car {
 private Engine engine ; // Holds an implementation part object to form an aggregation relationship
 public Car( Engine engine ) {
 this . engine = engine ;
     }
 public Engine getEngine () {
 return engine ;
     }
 public void setEngine ( Engine engine ) {
 this . engine = engine ;
     }
 public abstract void install ();
 }    
            
            
            
    


//RefinedAbstraction :Benz ;Bwm extends the Abstraction class.
public class Benz extends Car {
    public Benz(Engine engine) {
        super(engine);
    }
    @Override
    public void install() {
        System.out.println("Benz车安装");
        this.getEngine().addEngine();
    }
}
public class Bwm extends Car {
public Bwm(Engine engine) {
super(engine);
    }
@Override
public void install() {
System.out.println("Bwm车安装");
this.getEngine().addEngine();
    }
}                                    

// test code
    public static void main(String[] args) {
        //创建实现(行为)  ->Engine引擎
        //第一种 引擎  
        Engine engine200=new Engine200();
        //第二种 引擎
        Engine engine300=new Engine300();
        //创建抽象    ->车
        Car benz=new Benz(engine200);
        benz.install();
        
        Car bwm=new Bwm(engine300);
        bwm.install();
    }
}


运行结果: 

Benz car installation 
and assembly Engine200 
Bwm car installation 
and assembly Engine300


Application scenarios

1. If you do not want to use a fixed binding relationship between the abstraction and the implementation part, you can use the bridge mode to separate the abstraction and implementation parts, and then dynamically set the specific implementation that the abstraction part needs to use during the running of the program. You can also Dynamically switch the specific implementation.


2. If there is a situation where both the abstract part and the implementation part should be extensible, the bridge mode can be used, so that the abstract part and the implementation part can be changed independently, so that they can flexibly expand separately, instead of being mixed together and expanding while affecting the other side.


3. If you want to implement part of the modification without affecting the customer, you can use the bridge mode. The customer is running the object-oriented interface and implement the partial modification


. 3. If you want to implement the partial modification, it will not affect the customer. The bridging mode can be used, the client is running on the abstract interface, and the modification of the implementation part can be independent of the abstract part, so it will not affect the client, and it can be said to be transparent to the client.


4. If the implementation scheme of inheritance is adopted, it will lead to many subclasses. In this case, you can consider using the bridge mode, analyze the reasons for the change of functions, and see if they can be separated into different latitudes, and then separate them through the bridge mode. , thereby reducing the number of subclasses.













Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325565967&siteId=291194637