Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the bridging pattern of Java Design pattern

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to realize the bridging pattern of Java design pattern". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the bridging pattern of Java design pattern".

What is bridging mode?

The Bridge pattern is defined as follows: abstract and implementation are separated so that they can change independently. It is realized by using combinatorial relations instead of inheritance relations, thus reducing the coupling degree of the two variable dimensions of abstraction and implementation.

Advantages

1. Separation of abstraction and implementation, strong expansibility

2. Conform to the principle of opening and closing

3. In line with the principle of synthetic reuse

4. Its implementation details are transparent to customers.

Shortcoming

Because the aggregation relationship is based on the abstraction layer, developers are required to design and program for abstraction, and can correctly identify two independently changing dimensions in the system, which increases the difficulty of system understanding and design.

Knowledge point

We can separate the abstract part from the implemented part, cancel the inheritance relationship between them, and use the combinatorial relationship instead.

Bridge mode implementation

Case study: the owner of the Blue Rabbit Palace buys a dress

The skirt is divided into two dimensions, color and style.

The colors are yellow and red.

Styles are divided into long skirt and short skirt

Abstraction characters: color

Extended abstraction (Refined Abstraction) characters: yellow and red

Implementor roles: Styl

Specific Concrete Implementor roles: long skirts and short skirts

Color

Color interface, declaring a show () abstract method

Public interface Color {void show ();} yellow

Yellow class and implement color interface

Public class ColorYellow implements Color {@ Override public void show () {System.out.println ("yellow");} red

Red class and implement color interface

Public class ColorRed implements Color {@ Override public void show () {System.out.println ("red");} skirt

Skirt class, declaring a color property and an abstract method

Abstract class Qun {protected Color color; protected Qun () {} protected Qun (Color color) {this.color = color;} public abstract void shows ();} skirt

Inherit the skirt class and implement abstract methods

Public class QunChang extends Qun {protected QunChang () {} protected QunChang (Color color) {super (color);} @ Override public void shows () {System.out.println (long skirt); color.show ();}} short skirt

Inherit the skirt class and implement abstract methods

Public class QunDuan extends Qun {protected QunDuan () {} protected QunDuan (Color color) {super (color);} @ Override public void shows () {System.out.println ("short skirt"); color.show ();}} Test

New A red object is passed to the skirt as an argument.

Public class Demo {public static void main (String [] args) {Color color = new ColorRed (); Qun qun = new QunChang (color); qun.shows ();}}

Thank you for reading, the above is the content of "how to realize the bridge pattern of Java design pattern". After the study of this article, I believe you have a deeper understanding of how to realize the bridge pattern of Java design pattern, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report