C # design mode (8) -- Bridge Pattern)

Source: Internet
Author: User

I. Introduction

Here we use an example of the TV remote control to bring about the bridge mode to solve the problem. First, each brand of TV has a remote control, in this case, we can think of a design that uses the remote control as an abstract class that provides all the implementations of the remote control. The remote control of other TV brands inherit this abstract class. The specific design class diagram is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/124451M21-0.png "style =" margin: 0px; padding: 0px; border: 0px; "alt =" 17101083-bdbd692970f9430897d3493025cd05c "/>

This implementation enables each TV of different models to have its own remote control implementation. This design can well cope with changes to the TV set. You only need to add a derived class, however, over time, you need to change the functions of the remote control. For example, when you need to add functions such as the remote control and return the previous functions, at this time, the above design needs to modify the interface provided by the abstract class RemoteControl. At this time, you may only need to add a method to the abstract class to solve the problem, however, the problem is that we have changed the abstract implementation. If the user needs to change the TV product model and remote control function at the same time, the above design will lead to considerable changes, obviously, this design is not a good one. However, using the bridge mode can solve this problem well. Let me take a look at how the bridge mode is implemented.

II. Introduction to the Bridge Mode 2.1 Definition

The bridging mode decouples the abstract part from the implementation part so that they can change independently. For the above problem, abstraction is also called the RemoteControl class, and the Implementation part is the On (), Off (), NextChannel () and so On, that is, the implementation of remote control ), in the above design, abstraction and implementation are combined. The purpose of the bridge mode is to separate the two. According to the principle of object-oriented encapsulation and change, we can encapsulate the changes in the implementation part, that is, the changes in the remote control function, into another class. This idea is the implementation of the bridge mode, you can solve our analysis ideas by referring to the implementation code of the bridge mode.

2.2 Bridge Mode implementation

The above definition section has given us the purpose and implementation ideas of the Bridge Mode. Let's take a look at how the Bridge Mode solves the shortcomings of the introduction section.

Abstract code:

/// <Summary> // remote control in the abstract concept, playing the abstract role // </summary> public class RemoteControl {// field private TV implementor; // attribute public TV Implementor {get {return implementor;} set {implementor = value ;}/// <summary> // enable the TV, here, the abstract class no longer provides implementation, but calls the implementation in the implementation class /// </summary> public virtual void On () {implementor. on () ;}/// <summary> // turn Off the TV // </summary> public virtual void Off () {implementor. off () ;}/// <summary> /// change channel // </summary> public virtual void SetChannel () {implementor. tuneChannel () ;}/// <summary >/// specific Remote Control /// </summary> public class ConcreteRemote: RemoteControl {public override void SetChannel () {Console. writeLine ("---------------------"); base. setChannel (); Console. writeLine ("---------------------");}}

The remote control implementation method is part of the code, that is, the implementation part of the Code. At this time, we use another abstract class TV to encapsulate the changes in the remote control function. The specific implementation is handed over to the specific model TV:

/// <Summary> // TV set, providing the abstract method // </summary> public abstract class TV {public abstract void On (); public abstract void Off (); public abstract void tuneChannel () ;}/// <summary> // Changhong TV set, override the abstract method of the base class // provide the specific implementation /// </summary> public class ChangHong: TV {public override void On () {Console. writeLine ("Changhong TV already on");} public override void Off () {Console. writeLine ("Changhong TV has been turned off");} public override void tuneChannel () {Console. writeLine ("Changhong brand TV for Channel") ;}/// <summary> /// brand TV, override the abstract method of the base class /// </summary> public class Samsung: TV {public override void On () {Console. writeLine ("the licensed TV has been turned on");} public override void Off () {Console. writeLine ("the licensed TV has been turned off");} public override void tuneChannel () {Console. writeLine ("changing the channel of a TV set with a license brand ");}}

Client call code in Bridge Mode:

/// <Summary> // use the TV remote control as an example to demonstrate the bridge mode. // </summary> class Client {static void Main (string [] args) {// create a remote control RemoteControl remoteControl = new ConcreteRemote (); // remoteControl for Changhong TV. implementor = new ChangHong (); remoteControl. on (); remoteControl. setChannel (); remoteControl. off (); Console. writeLine (); // picture board TV remoteControl. implementor = new Samsung (); remoteControl. on (); remoteControl. setChannel (); remoteControl. off (); Console. read ();}}

In the above Implementation of the bridge mode, the function implementation method of the remote control is not implemented in the remote control Abstract class, but the implementation part is used to encapsulate it in another TV class, however, the remote control only contains a reference of the TV type. At the same time, this design is very consistent with the real-life situation. I think the realization of the remote control in real life-the remote control does not contain a new station, when you enable a function such as a TV, the remote control only contains the reference of these functions on the TV, and then finds the implementation of the corresponding functions on the TV ). Through the bridge mode, we split the abstraction and implementation part, so that we can well cope with the changes in these two aspects.

2.3 class diagram in Bridge Mode

After reading the implementation of the bridge mode, to help you sort out the relationships between classes in the bridge mode, the class graph structure of the bridge mode is provided here:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/124451C12-1.png "style =" margin: 0px; padding: 0px; border: 0px; "alt =" 17113136-8a15c31e045a42b59d600c3a4a80953 "/>

Iii. Advantages and disadvantages of the Bridge Mode

After introducing the bridge mode, let's take a look at the advantages and disadvantages of the bridge mode.

Advantages:

Decouples abstract interfaces from their implementations.

Abstraction and implementation can be expanded independently without affecting each other.

The implementation details are transparent to the customer, and the specific implementation details are hidden.

Disadvantages:Increased system complexity

Iv. Use Cases

Let's take a look at the use cases of the Bridge Mode. In the following cases, the bridge mode should be used:

  1. If a system needs to add more flexibility between the abstract role and the specific role of the component, avoid establishing static connections between the two layers.

  2. The design requires that any change to the role should not affect the client, or the change to the role should be completely transparent to the client.

  3. It must span the graphics and window systems of multiple platforms.

  4. A class has two independent dimensions, and both dimensions need to be extended.

5. An example of the actual application of the Bridge Mode

The bridge mode is often used in specific system development. The bridge mode is applied to the layer-3 architecture, and the business logic layer BLL in the layer-3 architecture decouples the DAL mode from the data operation layer ), the implementation method is to reference a reference in the dal layer in the bll layer. In this way, the implementation of data operations can be dynamically changed without changing the client code. The following is a simple example code:

// Client call // similar to the Web application class Client {static void Main (string [] args) {BusinessObject MERs = new CustomersBusinessObject ("ShangHai"); customers. dataacces = new CustomersDataAccess (); MERs. add ("John"); Console. writeLine ("result of adding a member:"); MERs. showAll (); customers. delete ("Wang Wu"); Console. writeLine ("result of deleting a member:"); customers. showAll (); Console. writeLine ("updated the result of a member:"); MERs. update ("Learning_Hard"); MERs. showAll (); Console. read () ;}// BLL layer public class BusinessObject {// FIELD private DataAccess dataacess; private string city; public BusinessObject (string city) {this. city = city;} // attribute public DataAccess Dataacces {get {return dataacess;} set {dataacess = value ;}// method public virtual void Add (string name) {Dataacces. addRecord (name);} public virtual void Delete (string name) {Dataacces. deleteRecord (name);} public virtual void Update (string name) {Dataacces. updateRecord (name);} public virtual string Get (int index) {return Dataacces. getRecord (index);} public virtual void ShowAll () {Console. writeLine (); Console. writeLine ("{0} customers:", city); Dataacces. showAllRecords () ;}} public class CustomersBusinessObject: BusinessObject {public CustomersBusinessObject (string city): base (city) {}// override public override void ShowAll () {Console. writeLine ("------------------------"); base. showAll (); Console. writeLine ("------------------------") ;}/// <summary> // equivalent to the data access layer DAL in a three-tier architecture) /// </summary> public abstract class DataAccess {// add, delete, modify, and query records public abstract void AddRecord (string name); public abstract void DeleteRecord (string name ); public abstract void UpdateRecord (string name); public abstract string GetRecord (int index); public abstract void ShowAllRecords ();} public class CustomersDataAccess: dataAccess {// FIELD private List <string> MERs = new List <string> (); public CustomersDataAccess () {// read data from the database in actual business and then fill in the customers List. add ("Learning Hard"); MERs. add ("Zhang San"); MERs. add ("Li Si"); MERs. add ("");} // rewrite method public override void AddRecord (string name) {MERs. add (name);} public override void DeleteRecord (string name) {MERs. remove (name);} public override void UpdateRecord (string updatename) {MERs [0] = updatename;} public override string GetRecord (int index) {return customers [index];} public override void ShowAllRecords () {foreach (string name in mers) {Console. writeLine ("" + name );}}}
Vi. Summary

Here, we will introduce the introduction of the Bridge Mode and implement the bridge mode.Abstract and implementation decoupling, making them independent from each other.


This article is from the "LearningHard" blog, please be sure to keep this source http://learninghard.blog.51cto.com/6146675/1310140

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.