Iv. Bridging Mode
The Bridge Mode focuses on the abstract design. As the name suggests, you can create a bridge and move these abstract methods to the interface. In this way, abstraction depends on the implementation of interfaces.
The purpose of the bridge mode is to separate the implementation of the abstract and abstract methods to achieve decoupling, so that the two can change independently of each other.
Advantages of the Bridge Mode: preferential use of object synthesis/aggregation will help you maintain that each class is encapsulated and concentrated on a single task, so that the class and class inheritance layers will remain relatively small, and it is unlikely to grow into an uncontrollable giant.
The following is the sample code:
[Java]
/**
* Mobile Software
* @ Author ripper
*
*/
Public abstract class HandsetSoft {
Public abstract void run ();
}
[Java]
/**
* Mobile games
* @ Author ripper
*
*/
Public class HandsetGame extends HandsetSoft {
@ Override
Public void run (){
System. out. println ("running mobile games ");
}
}
[Java]
Public class HandsetAddressList extends HandsetSoft {
@ Override
Public void run (){
System. out. println ("Run the phone address book! ");
}
}
[Java]
/**
* Mobile phone brand
* @ Author ripper
*
*/
Public abstract class HandsetBrand {
Public abstract void run ();
Protected HandsetSoft soft;
Public HandsetBrand (HandsetSoft soft ){
This. soft = soft;
}
Public HandsetBrand (){
}
Public void setSoft (HandsetSoft soft ){
This. soft = soft;
}
Public HandsetSoft getSoft (){
Return soft;
}
}
[Java]
/**
* Mobile phone brand M
* @ Author ripper
*
*/
Public class HandsetBrandM extends HandsetBrand {
Public HandsetBrandM (){
System. out. println ("M-type mobile phone ");
}
@ Override
Public void run (){
Soft. run ();
}
}
/**
* Mobile phone brand N
* @ Author ripper
*
*/
Public class HandsetBrandN extends HandsetBrand {
Public HandsetBrandN (){
System. out. println ("mobile phone N ");
}
@ Override
Public void run (){
Soft. run ();
}
}