[Original-tutorial-serialization] "android big talk Design Patterns"-structural patterns of design patterns Chapter 1: Bridging patterns the most important thing is to have a heart that makes MM happy.

Source: Internet
Author: User
Tags abstract definition
<Big talk design model> description and copyright notice of this tutorial

Guoshi studio is a technical team dedicated to enterprise-level application development on the Android platform. It is committed to the best Android Application in China.ProgramDevelopment institutions provide the best Android enterprise application development training services.

Official contact information for Enterprise Training and Development Cooperation:

Tel: 18610086859

Email: hiheartfirst@gmail.com

QQ: 1740415547

QQ: 148325348

Guoshi studio is better for you!

L this document references and uses free images and content on the Internet, and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

L if you have any questions or suggestions about this document, go to the official blog

Http://www.cnblogs.com/guoshiandroid/ (with the following contact information), we will carefully refer to your suggestions and modify this document as needed to benefit more developers!

L The latest and complete content of "big talk design mode" will be regularly updated on the official blog of guoshi studio. Please visit the blog of guoshi studio.

Http://www.cnblogs.com/guoshiandroid/get more updates.

Bridging Mode The most important thing is to makeMmHappy heart 

Bridging ModeUse Cases: 

Mm in the morning. Good morning. mm in the evening. Good evening. mm in new clothes. Nice clothes. mm in the hair style, say your hair looks pretty. Don't ask me the question of "how to say a new hairstyle to mm in the morning". You just need to combine it with bridge. The key is to have a heart that will make mm happy, as long as you have such a heart, you are no longer...

Explanation of the Bridge Mode: 

Bridge pattern is one of the construction 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.
Decouple an execution action from its implementation so that the two can
Vary independently.

Bridging ModeUMLFigure:

The proxy mode involves the following roles:

Abstract action role: an abstract class interface (interface or abstract class), and saves a reference to the implemented object.

Refined
Extends action) Role: extends the abstract role, and changes and modifies the abstract definition of the parent class.

Implementor role: this role provides an interface for implementing the role, but does not provide a specific implementation. It must be noted that this interface is not necessarily the same as the interface definition of abstract roles. In fact, these two interfaces can be very different. An implemented role should only provide underlying operations, while an abstract role should only provide operations at a higher level based on underlying operations.

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

The UML diagram of the proxy mode is as follows:

 

In-depth analysis of Bridge Mode:

A philosopher said, "You will never see the same river." without considering the right or wrong in philosophy, this is true from the perspective of change, because even though a small river, there has not been any change in space, but it has already changed in time coordinates. Everything in the world is doing this change, but you put a stone in your fish tank. A few days later, you will find that it is still the rock. This shows that although things have changed to multiple dimensions, some things are relatively stable, however, multi-dimensional changes of some things are indeed encouraging. We use inheritance to solve the expansion problem of changing and unstable things. How can we use object-oriented technology to make things easily change in multiple directions, isn't implementation very complicated? This requires the bridge mode.

The bridge mode is a structural mode. It mainly deals with two or more types of latitude changes due to the inherent series of types. That is to say, abstraction should not depend on implementation details, and implementation details should depend on abstraction.

The bridging mode is used to separate abstraction and behavior so that they can be modified and expanded independently and freely.

In the basic concept of object-oriented design, the concept of object is actually composed of two parts: attribute and behavior. We can think that attributes are static and abstract, generally, actions are contained in an object. However, in some cases, we need to classify these actions to form a general behavior interface, this is the usefulness of the bridge model.

We can think that the bridge mode classifies the attributes and behaviors of an object. Then we abstract and embody the attributes and behaviors separately, while keeping the attribute part referencing the behavior.

Scenario Analysis andCodeImplementation:

In the above application scenario, mm can be regarded as a refined abstracted action role instantiation object, while various sweet words are concrete
Implementor) role instantiation, as shown in:

Create an abstract action role

PackageCom. diermeng. designpattern. bridge;

 

/*

* Abstract action role

*/

Public Abstract ClassGirl {

/*

* Reference specific implementation roles

*/

PrivateSweetword;

 

PublicGIRL (sweetword ){

This. Sweetword = sweetword;

}

 

PublicSweetword getsweetword (){

ReturnSweetword;

}

 

Public VoidSetsweetword (sweetword
Sweetword ){

This. Sweetword = sweetword;

}

 

Public Abstract VoidSayhi ();

}

 

Create an implementation role:

PackageCom. diermeng. designpattern. bridge;

/*

* Implemented roles

*/

Public InterfaceSweetword {

Public VoidPraise ();

}

 

Refined
Role action) role:

package COM. diermeng. designpattern. bridge. impl;

Import COM. diermeng. designpattern. bridge. girl;

Import COM. diermeng. designpattern. bridge. sweetword;

/*

* refined abstract action) role

*/

Public class mm extends girl {

Public mm (sweetword) {

super (sweetword );

}

public void sayhi () {

This . getsweetword (). praise ();

}

}

Specific implementation role 1:

PackageCom. diermeng. designpattern. Bridge. impl;

 

ImportCom. diermeng. designpattern. Bridge. sweetword;

/*

* Specific implementation roles

*/

Public ClassSweetwordoneImplementsSweetword {

 

Public VoidPraise (){

System.Out. Println ("Mm, good morning ^_^ ");

}

 

}

Specific implementation role 2:

PackageCom. diermeng. designpattern. Bridge. impl;

 

ImportCom. diermeng. designpattern. Bridge. sweetword;

/*

* Specific implementation roles

*/

Public ClassSweetwordtwoImplementsSweetword {

 

Public VoidPraise (){

System.Out. Println ("mm good evening ^_^ ");

}

 

}

Specific implementation role 3:

PackageCom. diermeng. designpattern. Bridge. impl;

 

ImportCom. diermeng. designpattern. Bridge. sweetword;

/*

* Specific implementation roles

*/

Public ClassSweetwordthreeImplementsSweetword {

 

Public VoidPraise (){

System.Out. Println ("your new hairstyle looks pretty good:-o ");

}

 

}

Specific implementation role 4:

PackageCom. diermeng. designpattern. Bridge. impl;

 

ImportCom. diermeng. designpattern. Bridge. sweetword;

/*

* Specific implementation roles

*/

Public ClassSweetwordfourImplementsSweetword {

 

Public VoidPraise (){

System.Out. Println ("your clothes are beautiful:-o ");

}

 

}

Create a test client:

PackageCom. diermeng. designpattern. Bridge. client;

 

ImportCom. diermeng. designpattern. Bridge. girl;

ImportCom. diermeng. designpattern. Bridge. sweetword;

ImportCom. diermeng. designpattern. Bridge. impl. mm;

ImportCom. diermeng. designpattern. Bridge. impl. sweetwordfour;

ImportCom. diermeng. designpattern. Bridge. impl. sweetwordone;

ImportCom. diermeng. designpattern. Bridge. impl. sweetwordthree;

ImportCom. diermeng. designpattern. Bridge. impl. sweetwordtwo;

/*

* Test the client

*/

Public ClassBridgetest {

Public Static VoidMain (string [] ARGs ){

 

Sweetword sweetwordone =NewSweetwordone ();

Sweetword sweetwordtwo =NewSweetwordtwo ();

Sweetword sweetwordthree =New
Sweetwordthree ();

Sweetword sweetwordfour =NewSweetwordfour ();

 

Girl girl1 =New
Mm (sweetwordone );

Girl1.sayhi ();

 

Girl girl2 =New
Mm (sweetwordtwo );

Girl2.sayhi ();

 

Girl girl3 =New
Mm (sweetwordthree );

Girl3.sayhi ();

 

Girl girl4 =NewMm (sweetwordfour );

Girl4.sayhi ();

 

 

 

}

}

The running result is as follows:

Mm, good morning ^_^

Mm good evening ^_^

Your new hair looks pretty good:-o

Your clothes are so beautiful:-o

 

Advantages and disadvantages of the Bridge Mode:

Advantages:

The bridge mode provides more flexible functions than the inheritance relationship. It can use abstraction and separation to reduce coupling. When there is a new abstraction or implementation method, you only need to inherit an abstraction and inherit an implementation.

Disadvantages:

If you want to reabstract another type, you need to modify the abstraction.

Introduction to the actual application of the Bridge Mode:

The bridge mode is generally used in the following scenarios:

If a system requires more flexibility between the abstract role and the specific role of the component, it will avoid establishing static connections between the two layers.

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.

A component has more than one abstract role and actual role, and the system needs dynamic coupling between them. Although there is no problem in using inheritance in the system, the abstract roles and specific roles need to be changed independently, and the design requirements need to be managed independently.

Tip:

The bridge mode is a relatively complex structural mode. The Bridge Mode separates abstraction and implementation, reduces coupling, and improves maintainability and reusability of classes. The Bridge Mode abstracts and implements different changing latitudes, facilitating modularization.

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.