Design Mode learning-Bridge Mode

Source: Internet
Author: User

Bridging Mode

GOOD: separates abstract and implementation parts so that they can be changed independently.

The meaning here is not to separate abstract base classes from specific classes, but the real system may have multi-angle classification, and each category may change, this multi-angle separation allows them to change independently and reduce coupling between them. That is, if inheritance cannot implement the "open-closed principle", the bridge mode should be considered. For example, "Mobile Phone" can be classified by brand or

 

[Html]
# Include <iostream>
# Include <string>
# Include <vector>
Using namespace std;
 
// Mobile phone software
Class HandsetSoft
{
Public:
Virtual void Run () = 0;
};
 
// Game software
Class HandsetGame: public HandsetSoft
{
Public:
Virtual void Run ()
{
Cout <"running mobile games" <endl;
}
};
 
// Address Book software
Class HandSetAddressList: public HandsetSoft
{
Public:
Virtual void Run ()
{
Cout <"Mobile Phone Address Book" <endl;
}
};
 
// Mobile phone brand
Class HandsetBrand
{
Protected:
HandsetSoft * m_soft;
Public:
Void SetHandsetSoft (HandsetSoft * temp)
{
M_soft = temp;
}
Virtual void Run () = 0;
};
 
// M brand
Class HandsetBrandM: public HandsetBrand
{
Public:
Virtual void Run ()
{
M_soft-> Run ();
}
};
 
// N brands
Class HandsetBrandN: public HandsetBrand
{
Public:
Virtual void Run ()
{
M_soft-> Run ();
}
};
 
// Client
Int main ()
{
HandsetBrand * brand;
Brand = new HandsetBrandM ();
Brand-> SetHandsetSoft (new HandsetGame ());
Brand-> Run ();
Brand-> SetHandsetSoft (new HandSetAddressList ());
Brand-> Run ();
 
Return 0;
}
 

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.