Structural bridge mode design player (detailed introduction & Java code example)

Table of contents

bridge mode

definition

Related role

Solve the problem

advantage

shortcoming

scenes to be used

Bridge Mode Structure View

Application scenarios

instance class diagram

Code

operation result

[bridge mode]

definition

Bridge Pattern: Separating the abstraction from its implementation so that they can both vary independently. It is an object structure model, also known as Handle and Body model or Interface model.

Related role

Abstraction: [Abstract class]
RefinedAbstraction: Extend abstract class
Implementor: Implement class interface
ConcreteImplementor: Concrete implementation class

Solve the problem

Decouple abstraction from implementation so that they can change independently.

advantage

  • Separation of abstract interface and its implementation part;
  • The bridging mode is sometimes similar to the multiple inheritance scheme, but the multiple inheritance scheme violates the single responsibility principle of a class (that is, a class has only one reason for change), the reusability is relatively poor, and the number of classes in the multiple inheritance structure is very large, bridging Patterns are a better solution than multiple inheritance schemes;
  • The bridging mode improves the scalability of the system, and it is not necessary to modify the original system to expand any dimension in the two changing dimensions;
  • The implementation details are transparent to the client and can be hidden from the user.

shortcoming

  • It increases the difficulty of understanding and designing the system. Since the aggregation relationship is established at the abstraction layer, developers are required to design and program for abstraction;
  • It requires the correct identification of two independently changing dimensions in the system, so its scope of use has certain limitations.

scenes to be used

Let’s take a look at the usage scenarios of bridge mode. Bridge mode should be used in the following situations:

  • If a system needs to add more flexibility between the abstract and concrete roles of components, avoid creating static links between the two levels.

  • The design requires that any changes to the materialized role should not affect the client, or that changes to the materialized role should be completely transparent to the client.

  • On graphics and windowing systems that need to span multiple platforms.

  • A class has two dimensions that vary independently, and both dimensions need to be extended.

Bridge Mode Structure View

Application scenarios

If you need to develop a cross-platform video player, you can play video files in multiple formats on different operating platforms (Windows, Linux, UNIX, etc.) player.

The following are required:

(1) Draw the bridge mode structure view

(2) Give an instance class diagram and implement the code

instance class diagram

Code

package bridge;
public class AVIFile implements VideoFile {
    public void decode(String osType, String fileName) {
        System.out.println( "OS: " +osType+ "\n" + "File name: " +fileName);
    }
}

package bridge;
public class Client {
   public static void main(String args[]) {
      VideoFile vf;
      OperatingSystemVersion osType1 = new WindowsVersion();
      vf = new AVIFile();
      osType1.setVideo(vf);
      osType1.play("AVI");
      OperatingSystemVersion osType2 = new LinuxVersion();
      vf = new AVIFile();
      osType2.setVideo(vf);
      osType2.play("WMV");
      OperatingSystemVersion osType3 = new UnixVersion();
      vf = new AVIFile();
      osType3.setVideo(vf);
      osType3.play("PMEG");
   }
}

package bridge;
public class LinuxVersion extends OperatingSystemVersion {
   public void play(String fileName) {
      this.vf.decode(osType,fileName);
   }
}

package bridge;
public class MPEGFile implements VideoFile {
   public void decode(String osType, String fileName) {
      System.out.println( "Operating system: " +osType+ "File name: " +fileName);
   }
}

package bridge;
public abstract class OperatingSystemVersion {
   protected VideoFile vf;
   public void setVideo(VideoFile vf) {
      this.vf = vf;
   }
   public abstract void play(String fileName);
}

package bridge;
public class RMVBFile implements VideoFile {
   public void decode(String osType, String fileName) {
      System.out.println( "Operating system: " +osType+ "File name: " +fileName);
   }
}

package bridge;
public class UnixVersion extends OperatingSystemVersion {
   public void play(String fileName) {
      this.vf.decode(osType,fileName);
   }
}


package bridge;
public interface VideoFile {
   public void decode(String osType, String fileName);
}


package bridge;
public class WindowsVersion extends OperatingSystemVersion {
   public void play(String fileName) {
      this.vf.decode(osType,fileName);
   }
}

package bridge;
public class WMVFile implements VideoFile {
    public void decode(String osType, String fileName) {
        System.out.println( "Operating system: " +osType+ "File name: " +fileName);
    }

operation result

Leave a Comment

Your email address will not be published. Required fields are marked *