[Design mode] When to unify mobile phone software-Bridging Mode

Source: Internet
Author: User

I. Overview

Definition: separates abstract and implementation parts so that they can all change independently.


In software systems, some types of software systems have two or more dimensional changes due to their own logic. How can we deal with such "multi-dimensional changes "? How can we use object-oriented technology to make this type easily change in multiple directions without introducing additional complexity? This requires the bridge mode.

[Note] the inheritance declaration in C ++ is public.

2. Example

Two mobile phones, brand m and Brand N, and each Mobile Phone contains an address book and a game.

1) first implementation

Implementation:

The mobile phone brand is the abstract base class, and the mobile phone brand M and N inherit the base class of the mobile phone brand.

Then the game and address book categories in m and n are implemented respectively.

Finally, I used my grandfather class: mobile phone brand, and created my grandson class Address Book M (N) and game M (n)

Disadvantages:

If you add another MP3 class, you need to add a subclass (similar subclass) under mobile phone m and mobile phone n)

If you want to add a mobile phone brand, you need to add more classes.

# Include <iostream> using namespace STD; // mobile phone brand class handsetbrand {public: Virtual void run () {}}; // mobile phone brand M class handsetbrandm: Public handsetbrand {}; // mobile phone brand N class handsetbrandn: Public handsetbrand {}; // mobile phone brand M game class handsetbrandmgame: Public handsetbrandm {public: void run () {cout <"Running M-branded mobile games" <Endl ;}; // class handsetbrandngame: Public handsetbrandn {public: void run () {cout <"Running N-branded mobile games" <Endl ;}; // address book class handsetbrandmaddresslist: Public handsetbrandm {public: void run () {cout <"Run M-branded mobile phone Address Book" <Endl ;}; // address book class handsetbrandnaddresslist: Public handsetbrandn {public: void run () {cout <"Run N-brand Mobile Phone Address Book" <Endl ;}; int main () {handsetbrand * AB; AB = new handsetbrandmaddresslist (); AB-> Run (); AB = new handsetbrandmgame (); AB-> Run (); AB = new handsetbrandnaddresslist (); AB-> Run (); AB = new handsetbrandngame (); AB-> Run ();}

Principles of merging/aggregation multiplexing: Use merging/aggregation whenever possible, and do not use class inheritance whenever possible.

Aggregation: indicates a weak property relationship. Object A can contain object B, but object B is not part of object a. The relationship between the geese and the geese

Synthesis: indicates a "strong" ownership relationship, reflecting a strict overall and partial relationship, and declaring the same cycle. Wings and geese

Class is encapsulated and concentrated on a single task. In this way, the Class and Class inheritance layers will be relatively small, and it is unlikely that the growth will be a huge thing.


2) another program

Using mobile phone software as the base class allows contacts and games to inherit mobile phone software. Then different mobile phone brands inherit their respective functions.

Disadvantage: there will be major changes to the function of adding a mobile phone or adding a mobile phone brand.


# Include <iostream> using namespace STD; // mobile software class handsetsoft {public: Virtual void run () {}}; // address book class handsetaddresslist: Public handsetsoft {}; // game class handsetgame: Public handsetsoft {}; // mobile phone brand M game class handsetbrandmgame: Public handsetgame {public: void run () {cout <"Running M-branded mobile games" <Endl ;}; // class handsetbrandngame: Public handsetgame {public: void run () {cout <"Run N-brand Mobile Games" <Endl ;}; // address book class handsetbrandmaddresslist: Public handsetaddresslist {public: void run () of mobile phone brand m () {cout <"Run M-branded mobile phone Address Book" <Endl ;}; // address book class handsetbrandnaddresslist: Public handsetaddresslist {public: void run () {cout <"Run N-brand Mobile Phone Address Book" <Endl ;}; int main () {handsetsoft * AB; AB = new handsetbrandmgame (); AB-> Run (); AB = new handsetbrandngame (); AB-> Run (); AB = new handsetbrandmaddresslist (); AB-> Run (); AB = new handsetbrandnaddresslist (); AB-> Run ();}


3) loosely coupled Program

The mobile phone brand is an abstract class that contains the specific classes of each brand.

Mobile phone software is exhausting, including address book, games, etc.

Mobile phone software, as a member of the mobile phone brand, forms a mobile phone

# Include <iostream> using namespace STD; // mobile software class handsetsoft {public: Virtual void run () //; // virtual function implementation polymorphism (either defined or implemented) {}}; // mobile game class handsetgame: Public handsetsoft {public: void run () {cout <"run mobile game" <Endl ;}}; // mobile phone address book class handsetaddresslist: Public handsetsoft {public: void run () {cout <"run mobile phone Address Book" <Endl ;}; // mobile phone MP3 playback class handsetmp3: public handsetsoft {public: void run () {cout <"running mobile MP3 player" <Endl ;}; // mobile phone brand class handsetbrand {protected: handsetsoft * Soft; // set the mobile app public: void sethandsetsoft (handsetsoft * SOF) {This-> soft = sof ;}// run virtual void run () // virtual function {}}; // mobile phone brand N class handsetbrandn: Public handsetbrand {public: void run () {SOFT-> Run () ;}}; // mobile phone brand M class handsetbrandm: public handsetbrand {public: void run () {SOFT-> Run () ;}}; // mobile phone brand S Class handsetbrands: Public handsetbrand {public: void run () {SOFT-> Run () ;}}; int main () {handsetbrand * AB; AB = new handsetbrandn (); AB-> sethandsetsoft (New handsetgame ()); AB-> Run (); AB-> sethandsetsoft (New handsetaddresslist (); AB-> Run (); AB = new handsetbrandm (); AB-> sethandsetsoft (New handsetgame (); AB-> Run (); AB-> sethandsetsoft (New handsetaddresslist (); AB-> Run ();}

III. Basic Code of the Bridge Mode

# Include <iostream> using namespace STD; Class implementor // each widget class (such as mobile phone software) {public: Virtual void operation () // multi-state virtual functions must be implemented (provided to the virtual function table entry address) {}}; class concreteimplementora: Public implementor {public: void operation () {cout <"execution of specific method to implement a" <Endl ;}; class concreteimplementorb: Public implementor {public: void operation () {cout <"Implementation of Method B execution" <Endl ;}; class execution action // The overall composition (similar to the mobile brand abstract class) {protected: implementor * implementor; public: void setimplementor (implementor * implementor) {This-> implementor = implementor;} virtual void operation () {implementor-> operation () ;}; class refinedabstraction: public internal action {public: void operation () {implementor-> operation () ;}; int main () {role action * AB = new refined1_action (); AB-> setimplementor (New concreteimplementora ()); // combine a to the entire AB-> operation (); AB-> setimplementor (New concreteimplementorb (); // combine B to the entire AB-> operation (); system ("pause ");}

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.