Java 23 design modes 4 (serialization)

Source: Internet
Author: User

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

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.