JAVA design Pattern (19)-Bridging (bridge) mode __java

Source: Internet
Author: User

definition: separates the abstract part from its implementation, enabling them to change independently.

Type: Object-structured pattern

class Diagram:


structure of bridging mode

Abstraction : An interface to an abstract class that maintains a pointer to a Implementor type object, in this case ipluginplatform. refinedabstraction : Expands the interface defined by abstraction, in this case, Pluginversiona, pluginversionb. implementor : Defines the interface of the implementation class, which is not necessarily identical to the abstraction; in fact, the two interfaces can be completely different. Generally speaking, the Implementor interface provides only basic operations, while abstraction defines a higher level of operation based on these basic operations. concreteimplementor : Implement implementor and define its specific implementation.


Sample

If you want to design a plug-in for a system, first of all, this system needs cross-platform, secondly, because the plugin will often version upgrade, so the system needs to support a lot of version plug-ins. Along with the requirements, we may consider the solution this way.
Because of the need for cross-platform, cross platform is implemented through abstraction and inheritance.


And because each platform needs to support multiple versions of the plug-in, continue to extend the different versions under each platform.



The code is as follows:

Abstract class IPlugin {public
	abstract void Loadplugin ();

	public abstract void Unloadplugin ();

	public void Start () {
		//Add Operation
	} public

	void Stop () {
		//Add Operation
	}
}

class Wind Owsplugin extends IPlugin {
	@Override public
	void Loadplugin () {
	}

	@Override public
	Void Unloadplugin () {
	}
}

class WinPluginVer1 extends Windowsplugin {
	@Override public
	void start () {
		loadplugin ();
		Super.start ();
	}

	@Override public
	void Stop () {
		unloadplugin ();
		Super.stop ();
	}

Class WinPluginVer2 extends Windowsplugin {
	@Override public
	void Start () {
		loadplugin ();
		Super.start ();
	}

	@Override public
	void Stop () {
		unloadplugin ();
		Super.stop ();
	}

This omits the platform outside of Windows, as well as code other than the ver1,2 version.
The above is indeed a solution, but it is not a good way. Just imagine what you would do if you had one more platform now. You can only add an inheritance branch, and you need to append multiple versions of the implementation to the platform. And if you upgrade a version of what to do. The implementation of this version can only be appended to each of the platforms. The class structure expands as follows:


It can now be seen that the platforms and versions of the two-level implementations of the system are coupled together, resulting in repeated implementations and class bloat for each function append. It's hard to imagine how the system structure would explode when you add n platforms and n versions of support. Along with the design, coding, testing and other costs and the multiplication of risk.

Bridge Mode Implementation code

Abstract class Interface--> plug-in abstract classes iplugin{protected Ipluginplatform platform;
	public void Setplatform (Ipluginplatform platform) {this.platform = platform;
	public abstract void Start ();
public abstract void Stop ();
	The different version number implements class Pluginversiona extends iplugin{@Override public void Start () {Platform.loadplugin ();
	@Override public void Stop () {platform.unloadplugin ();
	The class Pluginversionb extends iplugin{@Override public void Start () {Platform.loadplugin ();
	@Override public void Stop () {platform.unloadplugin ();

	}//Abstract class interface--> platform abstract class ipluginplatform{public abstract void Loadplugin ();
public abstract void Unloadplugin (); //windows platform Implementation class Windowsplugin extends ipluginplatform{@Override public void Loadplugin () {System.out.printl
	N ("Windowsplugin loading ...");
	@Override public void Unloadplugin () {System.out.println ("Windowsplugin unloading ..."); } class Linuxplugin extends Ipluginplatform{@Override public void Loadplugin () {System.out.println ("Linuxplugin loading ...");
	@Override public void Unloadplugin () {System.out.println ("Linuxplugin unloading ...");  }//Client calls public class Bridgeclient {/** * @param args */public static void main (string[] args) {IPlugin
		
		Plugin = new Pluginversiona ();
		Ipluginplatform platform = new Windowsplugin ();
		
		Plugin.setplatform (platform);
		Plugin.start ();
	Plugin.stop (); }
}

Bridge Mode applicable scenario

You don't want to have a fixed binding relationship between the abstraction and the implementation part of it. The abstraction of a class and its implementation should be augmented by the method of generating subclasses. Modifications to an abstract implementation section should not have an impact on the customer, i.e. the client's code does not have to be recompiled. (c + +) you want to completely hide the implementation part of the abstraction from the customer. You want to share implementations across multiple implementations, but at the same time ask the customer not to know this.


Advantages of Bridging mode

Detach the interface and its implementation part. A realization may not necessarily be fixed on an interface. The implementation of an abstract class can be configured at run time, and an object can even change its implementation while it is running.

Separating abstraction and implementor helps to reduce the dependency on the implementation part of the compile time, and when changing an implementation class, it is not necessary to recompile the abstraction class and its client programs. In order to ensure binary compatibility between different versions of a class library, this must be the nature.

In addition, the separation of interface and implementation helps to layering, resulting in a better structured system, and the high-level part of the system needs only to know abstraction and implementor.
Increase scalability. You can expand the abstraction and implementor hierarchies independently. Realize the transparency of the details to the customer. You can hide the implementation details from the customer.



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.