Bridging mode core points:
To deal with the multi-level inheritance structure, to deal with the multidimensional changes of the scene, the various dimensions are designed into a separate inheritance structure, so that each dimension can be independent extension at the abstraction level to establish the association.
Let's say there are categories: computers---Lenovo computers, Dell Computers, Shenzhou Computers---desktops, notebooks---Lenovo desktops, Lenovo notebooks
If you use an inherited relationship, the class will be particularly numerous, adding a brand or computer type is not a good extension.
So we divide it in three dimensions. Computer---brand---type
Package Com.iter.devbox.bridge;
/** *
@author Shearer * *
/
Public
interface Brand {
void sale ();
}
Class Lennovo implements Brand {public
void sale () {
System.out.println ("Sales Lenovo Computer");
}
Class Dell implements Brand {public
void sale () {
System.out.println ("Selling Dell Computers");
}
Class ShenZhou implements Brand {public
void sale () {
System.out.println ("Selling Shenzhou computer");
}
Package Com.iter.devbox.bridge;
/**
* Computer abstract class
* @author Shearer
* * *
/Public abstract class Computer {
protected Brand Brand; Brand public
Computer (Brand Brand) {
this.brand = Brand;
}
public void Sale () {
brand.sale ();
}
}
Desktop
class Desktop extends Computer {public
Desktop (Brand Brand) {
super (Brand);
}
@Override public
void Sale () {
super.sale ();
System.out.println ("Sales desktop");
}
Notebook
class Laptop extends Computer {public
laptop (Brand Brand) {
super (Brand);
}
@Override public
void Sale () {
super.sale ();
System.out.println ("Sales Notebook");
}
Package Com.iter.devbox.bridge;
public class Client {public
static void Main (string[] args) {
//sales Lenovo Notebook
Computer Lennovo = new Laptop (new Lennovo ());
Lennovo.sale ();
Sales of Shenzhou Desktop
Computer Shenzhou = new Desktop (new Shenzhou ());
Shenzhou.sale ();
}
Run Result:
Sales Lenovo Computer
Sales Notebook
Sales of Shenzhou Computer
Sales Desktop
If we add other brands, such as Dell, the implementation class of the brand brand interface can be added directly.
Then, if you increase the type of computer, add the implementation class of the computer interface.
Expansion is very convenient. Conforms to the opening and closing principle.