One of the Java design patterns is the bridge mode.

Source: Internet
Author: User
Tags repetition

This article introduces the bridge (Bridge) Concepts, usage, and usage of the PatternBridging ModeFor development.

Bridge Mode Concept

The bridge mode is one of the constructor design patterns. Based on the minimum design principle of classes, the bridge mode enables different classes to assume different responsibilities by using encapsulation, aggregation, inheritance, and other behaviors. Its main feature is to separate abstract actions from implementation, so as to maintain the independence of each part and expand their functions.

Application scenarios of Bridge Mode

Object-orientedProgramThe design (OOP) contains the concept of class inheritance (subclass inherits the parent class). If a class or interface has multiple concrete implementation subclasses, if these subclasses have the following features:
-Subclass attributes that are relatively parallel.
-Conceptual crossover exists.
-Variability.
We can use the bridge mode to abstract and specifically, and reconstruct the relevant classes.

For ease of understanding, let's give an example. For example, a car has two sub-classes, truck and bus ), they have [set the engine] This action. You can set them as 1500cc (car1500), 2000cc (car2000) cars by setting different engine specifications.
In this way, both 1500cc trucks, 2000cc trucks, 1500cc buses, and 2000cc buses can be sub-categories of automobiles:
-Subclass attributes that are relatively parallel. The type of a car, which is in parallel with the engine specification of the car, has no conceptual repetition.
-Conceptual crossover exists. Both trucks and buses have 1500cc and 2000cc engine specifications.
-Variability. In addition to trucks and buses, there may be rescue trains. In addition to CC and 2000cc engine specifications, there may also be CC vehicles.

In this way, how can we design automobiles?

Method 1

Design all possible child classes by inheriting them. We may think of the following inheritance relationship:
Automobile category: Car
Vehicle subclass-by type: Bus, Truck
Vehicle subclass-engine classification: bus1500, bus2000, truck1500, truck2000
In this way, the engine setting action is implemented by each subclass.

However, if you need to add a firecar and an engine specification of CC, The subclass to be implemented will include:
Bus1500, bus2000, bus2500, truck1500, truck2000, truck2500, firecar1500, firecar2000, and firecar2500.
That is to say, in this design method, the number of child classes will increase with the geometric level.
In addition, bus1500 and truck1500 have the same engine specifications and their engine setting actions should be the same, but now they are divided into different child classes, so it is difficult to avoid repeated actions.

Method 2

How to set different engines for bus and truck respectively
Automobile category: Car
Automobile subclass: Bus, Truck

The following describes how to set the 1500cc and 2000cc engines in the bus class:
Bus extends car {
Public setengine1500cc ();
Public setengine2000cc ();
}

In the truck class, the following methods are also provided for setting the 1500cc and 2000cc engines:
Truck extends car {
Public setengine1500cc ();
Public setengine2000cc ();
}

In this case, the number of child classes is controlled. On the one hand, if an engine type is added, all vehicle subclasses need to be modified. On the other hand, different vehicle subclasses need to provide the same method even if the engine settings are the same.

In actual application development, the above two methods will cause a full migration and a large number of duplicates.Code.

The bridge mode can effectively solve such problems.

Client
Bridge users
Invalid action
Abstract class interface (interface or abstract class)
Maintain reference to implementor
Refined Response Action
Define action subclass
Implementor
Action Implementation class interface (the Action interface defines high-level operations based on the implementor Interface)
Concreteimplementor
Implementor subclass

Bridge application example

Let's take a look at how to use the bridge model to design automotive products.

Abstract-Vehicle Action class: automobile class and its subclass:
Car: automobile category
Truck: Car subclass-truck class.
Bus: automobile subclass-bus type.

Behavior implementation-implementor: behavior class and subclass set by the Automobile Engine
Setcarengine: automotive engine setting Interface
Setcarengine1500cc: Set the 1500cc Engine
Setcarengine2000cc: Set the 2000cc Engine

Code:

Package zieckey. designpatterns. Study. bridge;

// Test

Public class client
{
Public static void main (string [] argv)
{
Engine engine1500 = new engine1500cc ();
Engine engine2200 = new engine2200cc ();

Vehicle bus1500 = new bus (engine1500 );
Vehicle bus2200 = new bus (engine2200 );
Bus1500.setengine ();
Bus2200.setengine ();

Vehicle truck1500 = new truck (engine1500 );
Vehicle truck2200 = new truck (engine2200 );
Truck1500.setengine ();
Truck2200.setengine ();
}
}

Package zieckey. designpatterns. Study. bridge;

/**
*
* The vehicle class (vehicle) is assumed to have two sub-classes: truck class (truck) and bus class (bus ),
* They have the [set engine] action. They are set using different engine specifications,
* You can set them to 1500cc (car1500) or 2000cc (car2000) vehicles.
* In this way, both 1500cc trucks, 2000cc trucks, 1500cc buses, and 2000cc buses can be sub-classes of automobiles:
*-The sub-class attributes are relatively parallel. The type of a car, which is in parallel with the engine specification of the car, has no conceptual repetition.
*-Conceptual crossover exists. Both trucks and buses have 1500cc and 2000cc engine specifications.
*-Variability. In addition to trucks and buses, there may be rescue trains. In addition to CC and 2000cc engine specifications, there may also be CC vehicles.
*
* @ Author
* @ Since 2008/06/23
*/
Public abstract class Vehicle
{
Private engine;

Vehicle (engine)
{
This. setengine (engine );
}

Public abstract void setengine ();

Public void setengine (engine)
{
This. Engine = engine;
}

Public engine getengine ()
{
Return engine;
}
}

Package zieckey. designpatterns. Study. bridge;

// Define action subclass: This is a subclass of the abstract class of automobiles.

Public class bus extends Vehicle
{
Public Bus (engine)
{
Super (engine );
}

@ Override
Public void setengine ()
{
System. Out. Print ("set Bus Engine :");
Getengine (). setengine ();
}
}

Package zieckey. designpatterns. Study. bridge;

// Define action subclass: This is a subclass of the abstract class of automobiles.

Public class truck extends Vehicle
{
Public truck (engine)
{
Super (engine );
}

@ Override
Public void setengine ()
{
System. Out. Print ("Set Truck Engine :");
Getengine (). setengine ();
}

}

Package zieckey. designpatterns. Study. bridge;

// Automotive behavior Interface

Public Interface Engine
{
Public void setengine ();
}

Package zieckey. designpatterns. Study. bridge;

/** Concreteimplementor */
// Behavior implementation subclass

Public class engine2200cc implements Engine
{

Public void setengine ()
{
System. Out. println ("engine 2200cc ");
}

}

Package zieckey. designpatterns. Study. bridge;

/** Concreteimplementor */
// Behavior implementation subclass

Public class engine1500cc implements Engine
{

Public void setengine ()
{
System. Out. println ("engine 1500cc ");
}

}

 

Conclusion: The Bridge mode is a mode in which abstraction is separated from its implementation. It is mainly used when things are a group of changes, and the operation methods (implementations) of these things are also a group of changes, that is, they are all changing.

Address: http://space.itpub.net/9399028/viewspace-687882

Related Article

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.