Java design Pattern (eight): Bridging mode Bridge__java

Source: Internet
Author: User
Tags lenovo

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.





Related Article

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.