Viewing design Patterns from Java class libraries (3)

Source: Internet
Author: User

The last one mainly introduced several created design patterns Abstractfactroy,factorymethod and Singliton. Their common features are used to create objects. The next thing that comes up here is a couple of structural patterns. The so-called structural pattern is used to solve the process of creating a system structure by making a reasonable and effective combination of classes or objects to obtain a larger structure. This is mainly about bridge mode and decorator mode. The bridge model may require more understanding because it is, to a large extent, an example of the basic design ideas and principles of the design pattern.

Bridge mode

When Java was just launched, AWT was a hot topic, although there was a tendency to replace it with swing. But I have always felt that AWT has its advantages, at least it uses more local code than swing, and can provide users with a familiar local operating system interface. If you run an AWT-based program in Windows XP, the gorgeous interface theme in XP can be easily applied to the AWT program, and Swing doesn't, because AWT calls this code and uses a local form control. Of course, swing also has its advantages, not to generalize.

In simple terms, AWT provides programmers with an abstraction of the form interface system, whereas in an internal implementation there are different implementations for each operating system, which is the concept of the Peer. When the programmer invokes the AWT object, the call is forwarded to the object's corresponding peer, and the local object method is called by peer to complete the display of the object. For example, if you use AWT to create an instance of a menu class, you will create an instance of the menus in the program runtime and actually execute the menu's reality and management by creating the same bit body. Different systems, with different implementations of the same body, the Solaris JDK will produce a motif menu of the same body, the JDK under Windows will produce a Windows menu of the same body, and so on. The use of the same-body enables the development of Cross-platform window tools to be extremely rapid, as the use of the same-body can avoid the realization of methods already contained in the local window controls.

Figure nine: Components and their peers in AWT

In fact, from the design point of view, this is an abstraction and implementation of the process of separation--awt is abstract, the same place is the implementation of the abstraction and realization of each become an object system, they are connected by a bridge, they can develop their own level of object, without concern on the other hand. This is the idea provided by bridge mode. Bridge mode can provide dynamic switching in different implementations without having to recompile the program.

Typically, bridge mode works with the abstractfactory pattern, and the abstractfactory creates an object system that is specifically implemented. In particular, when there is only one implementation, the Implementor abstract class can be removed. In this way, one by one corresponding relationships between abstraction and implementation are established, but this does not impair the meaning of bridge mode. This is known as the degraded bridge model.

Most of the time, the method between the abstraction level and the implementor level is not one by one corresponding, that is to say, the abstraction and implementor are not simple message forwarding. Typically, we implement abstraction as an abstract class rather than an interface. Define the underlying, or atomic, method at the implementor level, while at the abstraction level define some of the high-level, abstract methods based on the atomic method. In this way, the abstraction and implementor can be divided more clearly, and the structure of the class is clearer.

Figure 10: Bridge mode partitioning of the system

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.