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;
}