Design Pattern -- 08. Structural. Bridge. Pattern (Delphi Sample)

Source: Internet
Author: User
Intent
  • DecoupleAn independent action from its implementation so that the two can vary independently.
  • Publish interface inInheritanceHierarchy, andBuryImplementation in its own inheritance hierarchy.
  • BeyondEncapsulation,Insulation
Problem

"HardeningOf the softwareArteries"Has occurred by using subclassing of an abstract base class to provideAlternativeImplementations. This locks in compile-time binding between interface and implementation. The specified action and implementation cannot be independently extended or composed.

Motivation

Consider the domain of "thread scheduling ".

There are two types of thread schedulers, and two types of operating systems or "platforms ". given this approach to specialization, we have to define a class for each permutation of these two dimensions. if we add a new platform (say... Java's Virtual Machine), what wowould our hierarchy look like?

What if we had three kinds of thread schedulers, and four kinds of platforms? What if we had five kinds of thread schedulers, and ten kinds of platforms? The number of classes we wowould have to define is the product of the number of scheduling schemes and the number of platforms.

The Bridge design pattern proposes refactoring this exponentially explosive inheritance hierarchy into two orthogonal hierarchies-one for platform-independent implements actions, and the other for platform-dependent implementations.

Discussion

Decompose the component's interface and implementation into orthogonal class hierarchies. the interface class contains a pointer to the abstract implementation class. this pointer is initialized with an instance of a concrete implementation class, but all subsequent interaction from the interface class to the implementation class is limited to the specified action maintained in the implementation base class. the client interacts with the interface class, and it in turn "delegates" all requests to the implementation class.

The interface object is the "handle" known and used by the client; while the implementation object, or "body", is safely encapsulated to ensure that it may continue to evolve, or be entirely replaced (or shared at run-time.

Use the Bridge pattern when:
  • You want run-time binding of the implementation,
  • You have a proliferation of classes resulting from a coupled interface and numerous implementations,
  • You want to share an implementation among multiple objects,
  • You need to map orthogonal class hierarchies.
Consequences include:
  • Decoupling the object's interface,
  • Improved extensibility (you can extend (I. e. subclass) the specified action and implementation hierarchies independently ),
  • Hiding details from clients.

Bridge is a synonym for the "handle/body" idiom. this is a design mechanism that encapsulates an implementation class inside of an interface class. the former is the body, and the latter is the handle. the handle is viewed by the user as the actual class, but the work is done in the body. "The handle/body class idiom may be used to decompose a complex into action into smaller, more manageable classes. the idiom may reflect the sharing of a single resource by multiple classes that control access to it (e.g. reference counting )."

Structure

The Client doesn' t want to deal with platform-dependent details. The Bridge pattern encapsulates this complexity behind an unsupported action "wrapper ".

Bridge emphasizes identifying and decoupling "interface" into action from "implementation" into action.

Example

The Bridge pattern decouples an independent action from its implementation, so that the two can vary independently. A household switch controlling lights, ceiling fans, etc. is an example of the Bridge. the purpose of the switch is to turn a device on or off. the actual switch can be implemented as a pull chain, simple two position switch, or a variety of dimmer switches.

Check list
  1. Decide if two orthogonal dimensions exist in the domain. These independent concepts cocould be: Your action/platform, or domain/infrastructure, or front-end/back-end, or interface/implementation.
  2. Design the separation of concerns: what does the client want, and what do the platforms provide.
  3. Design a platform-oriented interface that is minimal, necessary, and sufficient. Its goal is to decouple the specified action from the platform.
  4. Define a derived class of that interface for each platform.
  5. Create the specified action base class that "has a" platform object and delegates the platform-oriented functionality to it.
  6. Define specializations of the specified action class if desired.
Rules of thumb
  • Adapter makes things work after they're designed; Bridge makes them work before they are.
  • Bridge is designed up-front to let the specified action and the implementation vary independently. Adapter is already fitted to make unrelated classes work together.
  • State, Strategy, Bridge (and to some degree Adapter) have similar solution structures. they all share elements of the "handle/body" idiom. they differ in intent-that is, they solve different problems.
  • The structure of State and Bridge are identical (could t that Bridge admits hierarchies of envelope classes, whereas State allows only one ). the two patterns use the same structure to solve different problems: State allows an object's behavior to change along with its state, while Bridge's intent is to decouple an independent action from its implementation so that the two can vary independently.
  • If interface classes delegate the creation of their implementation classes (instead of creating/coupling themselves directly), then the design usually uses the Abstract Factory pattern to create the implementation objects.
UML (Class digoal)

Delphi Sample

  1: unit Pattern;
  2: 
  3: interface
  4: 
  5: uses
  6:   SysUtils;
  7: 
  8: type
  9:   IBridge = interface
 10:   ['{FB1F4FB1-8AD8-48FD-AEA5-1E33B4628879}']
 11:     function OperationImp: String;
 12:   end;
 13: 
 14:   TAbstraction = class
 15:   private
 16:     FBridge: IBridge;
 17:   public
 18:     constructor Create(implement: IBridge);
 19:     function Operation: string;
 20:   end;
 21: 
 22:   TImplementationA = class(TinterfacedObject, IBridge)
 23:   public
 24:     function OperationImp: string;
 25:   end;
 26: 
 27:   TImplementationB = class(TInterfacedObject, IBridge)
 28:   public
 29:     function OperationImp: string;
 30:   end;
 31: 
 32: implementation
 33: 
 34: { TAbstraction }
 35: 
 36: constructor TAbstraction.Create(implement: IBridge);
 37: begin
 38: //  inherited;
 39:   FBridge := implement;
 40: end;
 41: 
 42: function TAbstraction.Operation: string;
 43: begin
 44:   WriteLn('Abstraction' + ' <<>> ' + FBridge.OperationImp);
 45: end;
 46: 
 47: { TImplemetationA }
 48: 
 49: function TImplementationA.OperationImp: string;
 50: begin
 51:   Result := 'Implementation A';
 52: end;
 53: 
 54: { TImplementationB }
 55: 
 56: function TImplementationB.OperationImp: string;
 57: begin
 58:   Result := 'Implementation B';
 59: end;
 60: 
 61: end.

--------------------------------------------------------------------------------------------------------------------------

  1: program Structural.Bridge.Pattern;
  2: 
  3: {$APPTYPE CONSOLE}
  4: 
  5: uses
  6:   SysUtils,
  7:   Pattern in 'Pattern.pas';
  8: 
  9: var
 10:   abstraction: TAbstraction;
 11: 
 12: begin
 13: //  ReportMemoryLeaksOnShutdown := DebugHook  0;
 14:   try
 15:     try
 16:       abstraction := TAbstraction.Create(TImplementationA.Create);
 17:       WriteLn(abstraction.Operation);
 18: 
 19:       abstraction.Free;
 20:       abstraction := TAbstraction.Create(TImplementationB.Create);
 21:       WriteLn(abstraction.Operation);
 22: 
 23:       ReadLn;
 24:     finally
 25:       FreeAndNil(abstraction);
 26:     end;
 27:   except
 28:     on E:Exception do
 29:       Writeln(E.Classname, ': ', E.Message);
 30:   end;
 31: end.

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.