"Onlookers" design pattern (15)--Structural type bridge pattern

Source: Internet
Author: User

Bridge mode is one of the most complicated patterns in software design mode , which separates objects from their specific behavior and specific characteristics, so that they can change independently of each other. A Thing object is only an abstract concept. such as "Circle", "triangle" is attributed to the abstract "shape", while "Draw Circle", "triangle" is the implementation of the behavior of the "Paint" class, and then called by "shape" paint. ----WIKIPEDIA


Personal Understanding

The core of bridge mode is decoupling, abstracting concrete things into general things by abstraction, that is, something with generality, changing the inheritance relationship between two characters into the aggregation relation, which is to change the strong association between them into weak association. Therefore, the so-called decoupling in bridge mode refers to the use of aggregation relationships rather than inheritance relationships between abstractions and implementations of a software system, so that they can vary relatively independently. This is the purpose of bridge mode.


Example

For a car company, the most commonly used should be the production of cars, selling cars, for these two should be said that most of the car companies should be consistent. So take this as a background example. (Note: Do not know what reason, can not upload pictures, the structure of the class diagram in the source code, the need for readers to see).


First of all companies business, we can provide a lucrative interface. This allows the abstract class to be coupled with the implementation of the interface class.

Public abstract class Carcompanybusi {protected Companyimplementor carcompany;public Carcompanybusi ( Companyimplementor carcompany) {this.carcompany = Carcompany;} public abstract void Getmoney ();}

public class Carcompanybussiness extends Carcompanybusi{public carcompanybussiness (Companyimplementor company) {Super [company]; this.carcompany = Company;} @Overridepublic void Getmoney () {This.carCompany.produceCar (); This.carCompany.sellCar ();}}


Implementing an interface class primarily defines a company's specific behavior

Public interface Companyimplementor {public void Producecar ();p ublic void Sellcar ();
its implementation class
public class Hongqicompany implements Companyimplementor {@Overridepublic void Producecar () {System.out.println ("Production car" );} @Overridepublic void Sellcar () {System.out.println ("car Sold");}}

Advantages of bridge mode


1. Abstraction and implementation separation: it is a design pattern that is designed to address the drawbacks of integration. In this mode, the implementation can be unconstrained by the abstraction and no longer be bound at a fixed level of abstraction.

2. Excellent ability to expand

3. The implementation details are transparent to the customer. The customer does not care about the implementation of the details, it has been encapsulated by the abstraction layer through the aggregation relationship.


Bridge mode Usage Scenarios

1. It is not desirable or inappropriate to use integrated scenarios such as inheritance hierarchy transitions, the inability to refine design particles, and the need to consider using bridge mode.

2. Interface or abstract class instability scenario: Knowing that the interface is not stable also want to realize or inherit to achieve business needs, it is not worth the candle, but also the comparison of the failure of the practice.

3. High reusability Requirements Scenario: The finer the granularity of the design, the more likely it is to be reused, whereas inheritance is limited by the parent class, and it is not possible to have too fine granularity.

Code download

Source



"Onlookers" design pattern (15)--Structural type bridge pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.