Easy to master Java bridging mode _java

Source: Internet
Author: User
Tags lenovo

definition: separates the abstract part from the implementation part so that they can be changed independently.

Features: bridging mode is based on the minimal design principle of classes, and allows different classes to assume different responsibilities by using behaviors such as encapsulation, aggregation, and inheritance. Its main feature is to separate the abstract (abstraction) from the Behavior implementation (implementation), so as to maintain the independence of the parts and to deal with their functional expansion.

Enterprise-class development and applications in common frameworks: multiple-inheritance architecture

It is difficult to clarify the role of a bridging pattern only from the definition, and here's an example to illustrate:

Computer City selling computers, computer types divided into tablets, laptops, desktop computers, computer brands and Lenovo, Dell, Asus, Shenzhou. If you build a class from multiple inheritance angles, you need to build a computer abstraction class, and then the tablet, laptop, desktop computers inherit the abstract computer, and the different computer brands will inherit these computer classes and then implement the method, If you want to add a computer type, the different computer brands will inherit the class, or add a computer brand, and then inherit the different categories separately, so that the creation becomes complicated.

And the idea of bridging mode is realized from different dimensions, by increasing the degree of coupling to reduce the development effort, such as computer classification and computer brand classification is two dimensions, if we are implemented from two dimensions, so we can reduce the development complexity, this is the single principle of Java development.

The specific implementation code is as follows:

 package Com.test.bridge;

Interface Brand {public void sale ();}
 Class Dell implements Brand {public void sale () {System.out.println ("selling Dell branded Computers");
 Class Lenovo implements brand{public void sale () {System.out.println ("selling Lenovo PCs");
 } class Computer {protected Brand Brand;
 Public Computer (Brand b) {this.brand = b;
 public void Sale () {Brand.sale ();
 } class Desktop extends computer{public Desktop (Brand b) {super (b);
 @Override public void Sale () {Super.sale ();
 SYSTEM.OUT.PRINTLN ("Computer type for desktop computer");
 } class Laptop extends computer{public laptop (Brand b) {super (b);
 public void Sale () {Super.sale ();
 SYSTEM.OUT.PRINTLN ("Computer type for Notebook");
 } class Pad extends computer{public pad (Brand b) {super (b);
 @Override public void Sale () {Super.sale ();
 SYSTEM.OUT.PRINTLN ("Computer type is a tablet computer");
 } public class Demo {public static void main (string[] args) {//Sales Lenovo notebook Brand B = new Lenovo (); Computer C = new LapTop (b);
 
 C.sale ();
 Sales of Dell Desktop Brand B1 = new Dell ();
 Computer C1 = new Desktop (B1);
 C1.sale (); }
}

Through the above example, we can see that we put the brand (Brand) in the form of objects into the class of computer classification, which effectively reduces the complexity of the computer, but also the geometry is reduced, and in two directions to achieve, and let our ideas more clearly.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.