Home > Java > javaTutorial > An example of object structural pattern of bridge pattern in Java

An example of object structural pattern of bridge pattern in Java

黄舟
Release: 2017-09-11 10:53:10
Original
1241 people have browsed it

This article mainly introduces the relevant information about the detailed explanation of the bridge mode in Java - the object structural mode. I hope that through this article you can master this part of the knowledge. Friends in need can refer to it

## Bridge pattern in #Java——Detailed explanation of examples of object structural pattern

1. Intention

The abstract part Separate from its implementation parts so that they can all be changed independently.


2. Applicability

Use the Bridge pattern in some of the following situations

You don’t want to be confused between the abstraction and its implementation part There is a fixed binding relationship between them. This may be the case, for example, because parts of the implementation should be selectable or switchable while the program is running.


The abstraction of a class and its implementation should be extendable by generating subclasses. At this time, the Bridge pattern allows you to combine different abstract interfaces and implementation parts, and extend them respectively.


Modifications to an abstract implementation part should have no impact on customers, that is, customer code does not need to be recompiled.


(C++) You want to completely hide the implementation part of the abstraction from clients. In C++, the representation of a class is visible in the class interface.


3. Structure

##4. Code

public interface Implementor {
 /**
  * 实现抽象部分的具体方法
  */
 public void operationImpl();
}
Copy after login

public class ConcreteImplementorA implements Implementor {
 @Override
 public void operationImpl() {
  System.out.println("ConcreteImplementorA");
 }
}
Copy after login

public class ConcreteImplementorB implements Implementor {
 @Override
 public void operationImpl() {
  System.out.println("ConcreteImplementorB");
 }
}
Copy after login

public abstract class Abstraction {
 private Implementor mImplementor;

 /**
  * 通过实现部分对象的引用构造抽象部分的对象
  *
  * @param implementor 实现部分对象的引用
  */
 public Abstraction(Implementor implementor){
  mImplementor = implementor;
 }

 public void operation(){
  mImplementor.operationImpl();
 }
}
Copy after login

public class RefinedAbstraction extends Abstraction {

 /**
  * 通过实现部分对象的引用构造抽象部分的对象
  *
  * @param implementor 实现部分对象的引用
  */
 public RefinedAbstraction(Implementor implementor) {
  super(implementor);
 }

 public void refinedOperation(){
  //对 Abstraction中的方法进行扩展。

  System.out.println("refinedOperation");
  operation();
 }
}
Copy after login

public class Client {

 public static void main(String[] args){
  Abstraction abstraction = new RefinedAbstraction(new ConcreteImplementorA());
  abstraction.operation();
 }
}
Copy after login

The above is the detailed content of An example of object structural pattern of bridge pattern in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template