Design Pattern learning Summary-Bridge pattern)

Source: Internet
Author: User

Problem:
In object-oriented design, abstraction is a very important concept. The so-called abstraction is to ignore the special features of a class of objects and propose some common features, ignoring some details for the moment, changes to such objects can be completed in sub-classes by means of inheritance. In other words, inheritance can be used to cope with abstract changes. In a software system, abstraction is also easy to change for some reason, making the object change from two dimensions or more directions. The use of inheritance schemes often violates the single responsibility principle of a class (that is, a class has only one reason for change), and the reusability is poor. The bridge mode is a better solution than the inheritance solution.

Definition:
Bridge pattern separates the abstract part from its implementation part so that the abstraction and implementation can change independently.

Intent:
Bridge mode. The system is divided into abstract action and implemetor) two relatively independent abstract parts (the so-called "abstraction" and "Implementation" here do not mean that the subclass inherits the abstract base class to implement the specific method, but the abstract refers to the abstract of the demand, implementation refers to implementing user requirements through object combination. These two parts can be modified independently of each other. The abstract Part of implemetor (which can be extended to more changing directions) is combined into the abstract class in the form of abstract classes, in the master implementation object refined1_action of the abstract part, the dependency on the specific implementation (concreteimplemetor) of the implemetor object is isolated.
Participants:
• Abstract action role: the definition given in abstraction and a reference to the implemented object is saved.
• Modify abstract roles: Expand abstract roles, and change and modify the abstract definitions of parent classes.
• Implementor role: this role provides an interface for implementing the role, but does not provide a specific implementation. The underlying operations provided by the implementation role, while the abstract role provides a higher layer of operations based on the underlying operations.

• Concrete implementor role: this role provides the specific implementation of the role interface.

UML:

 
Instance description:

Nokia mobile phone Factory
Abstract mobile phone factories need to cope with changes in the production of different types of mobile phones (n8 and Asn production methods may be different) and different network types (GSM and CDMA production methods may be different) differences

UML:

• Use the inheritance mode:

• Bridge Mode:

Code:

///   <Summary>
/// Abstract role abstraction interface for mobile phone Production
///   </Summary>
Public Abstract Class Phonefactory
{
Protected Phonenettype;
Public Void Setnettype (phonenettype _ phonenettype)
{
Phonenettype = _ phonenettype;
}
Public Abstract Void Create ();
}
///   <Summary>
/// Mobile Production abstract role refined1_action
///   </Summary>
Public Class Factory_n8: phonefactory
{
Public Override Void Create ()
{
Phonenettype. createphone ();
System. Console. writeline ( " I am n8 " );
}
}
///   <Summary>
/// Mobile Production abstract role refined1_action
///   </Summary>
Public Class Factory_factory: phonefactory
{
Public Override Void Create ()
{
Phonenettype. createphone ();
System. Console. writeline ( " I am a hacker " );
}
}
///   <Summary>
/// Mobile phone production implementor
///   </Summary>
Public Abstract Class Phonenettype
{
Public Abstract Void Createphone ();
}
///   <Summary>
/// Mobile phone concreteimplementor
///   </Summary>
Public Class Factory_csm: phonenettype
{
Public Override Void Createphone ()
{
System. Console. writeline ( " I am a GSM network mobile phone. " );
}
}
///   <Summary>
/// Mobile phone concreteimplementor
///   </Summary>
Public Class Factory_cdma: phonenettype
{
Public Override Void Createphone ()
{
System. Console. writeline ( " I am a CDMA network mobile phone. " );
}
}
///   <Summary>
/// Client test code
///   </Summary>
Public Void Bridgetest ()
{
Phonefactory pF = New Factory_n8 ();
PF. setnettype ( New Factory_csm ());
PF. Create ();
}

Advantages:
• The Bridge Mode decouples the high coupling relationship between abstraction and implementation using the "synthesis/aggregation Reuse Principle", so that abstraction and implementation can change along their respective dimensions (through their inheritance ).
• More flexibility is added between Abstract roles and specific roles. When there is a new abstraction or implementation method, you only need to inherit an abstraction and inherit an implementation.
• Any changes to the role will not affect the client.

Disadvantages:
• The Bridge mode cannot cope with changes in the number of dimensions. to abstract another dimension type, you need to modify the abstract role and combine the new implementation into the abstract action) in violation of the open/closed Principle
Application Scenario:
When the system has multi-dimensional Angle classification, and each classification has a very strong change, it is more appropriate to use the bridge mode.

 

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.