Introduction to the transformer mode in the Java language structural mode (Part 1)

Source: Internet
Author: User
What is a structural model?

The structural mode describes how classes and objects are combined to form a large structure. The structural mode describes two different things: Class and Class instance. According to what they describe, there are two types of structural modes: class structure mode and instance structure mode.

The class structure mode uses inheritance to combine classes and interfaces to form a larger structure. When a class inherits from the parent class and implements an interface, the new class combines the structure of the parent class with the interface structure. The class structure mode is static. A typical example of a similar structure mode is a Class-form transformer mode.

The instance structure mode describes how to combine objects of different types to implement new functions. The instance structure mode is dynamic. A typical instance structure mode is the agent mode, which will be introduced later. Other examples include the composite mode, flight weight mode, decoration mode, and transformer mode in instance form.

There are two modes: the form of the structure mode and the form of the Instance structure mode, which is an excellent annotation of the above two forms of structure mode. This section describes the transformer mode, which has two types: Class form and instance form.

Introduction to transformer Mode

The transformer mode converts one class interface into another interface that the client expects. The transformer mode enables two classes that cannot work together to work together. As mentioned above, the transformer mode is about the structural mode of the class structure, so it is a static mode.

This is like a transformer-a transformer converts one voltage to another. When I took American appliances back to mainland China for use, I faced different voltage problems. The domestic voltage in the United States is 110 V, while that in China is 220 V. If I want to use an electrical appliance I use in the United States in mainland China, I must have a transformer that converts a 220 V voltage to a 110 V voltage. This is what the current mode does, so this mode is called the transformer mode.

Readers may also think that the adapter can also be translated into a converter (adapter) in Chinese ). In fact, the converter (adapter) is also a suitable name. For example, the connector of an electrical appliance in the United States is generally three-phase, that is, in addition to the anode and cathode, there is also a ground pole. Power outlets in buildings in mainland China generally have only two poles without a ground pole. At this time, even if the electrical appliance can indeed accept a 220 V voltage, the power outlet and the plug do not match, it also makes the electrical appliance unusable. A three-phase to two-phase converter (adapter) can solve this problem. Therefore, this mode can also be called the converter mode.

At the same time, this approach is very similar to the packaging process. The actual appearance of the encapsulated object is covered and changed by the packaging. Therefore, someone calls this mode the wrapper mode. In fact, we often write many such Wrapper Classes to wrap existing classes so that they can have interfaces that meet the needs.

The transformer mode has two different forms: Class Mode and instance mode.

The class diagram of the class form transformer mode is defined as follows.


Figure 1. Class Diagram definition of Class transformer Mode

As shown in figure 1, the schema involves the following members:

Target ). This is the expected interface. Note that because the class transformer mode is discussed here, the target cannot be a class.

Source (adaptee ). Existing interfaces to be adapted.

Transformer (adapter ). Transformer is the core of this model. The transformer converts the source interface to the target interface. Obviously, this role cannot be an interface, but must be a real class.

The Demo code of this mode is as follows:

Package com. javapatterns. Adapter. classadapter;

Public interface target

{

/**

* Class adaptee contains operation sampleoperation1.

*/

Void sampleoperation1 ();

/**

* Class adaptee doesn' t contain operation sampleoperation2.

*/

Void sampleoperation2 ();

}

Code List 1. Source Code of target.

Package com. javapatterns. Adapter. classadapter;

Public class adaptee

{

Public void

Sampleoperation1 (){}

}

Code List 2. Source Code of adaptee.

Package com. javapatterns. Adapter. classadapter;

Public class adapter extends adaptee implements target

{

/**

* Class adaptee doesn' t contain operation sampleoperation2.

*/

Public void sampleoperation2 ()

{

// Write your code here

}

}

Code List 3. source code of the adapter.

Effect of transformer Mode

First, use an actual class to adapt the source (adaptee) to the target ). In this way, if you want to adapt both the source and the source subclass, it will not work.

Second, because the transformer class is a subclass of the source class, some methods can be used to replace (override) the source in the transformer class.

Third, because only one transformer class is introduced, there is only one route to reach the target class. The problem is simplified.

The class diagram of the Instance type transformer mode is defined as follows.


As shown in figure 1, the schema involves the following members:

Target ). This is the expected interface. The target can be a real or abstract class.

Source (adaptee ). Existing interfaces to be adapted.

Transformer (adapter ). Transformer is the core of this model. The transformer converts the source interface to the target interface. Obviously, this role must be a real class.

The Demo code of this mode is as follows:

Package com. javapatterns. Adapter;

Public interface target {

/**

* Class adaptee contains operation sampleoperation1.

*/

Void sampleoperation1 ();

/**

* Class adaptee doesn' t contain operation sampleoperation2.

*/

Void sampleoperation2 ();

}

Code list 4. Source Code of target.

Package com. javapatterns. Adapter;

Public class adapter implements target {

Public adapter (adaptee ){

Super ();

This. adaptee = adaptee;

}

Public void sampleoperation1 () {adaptee. sampleoperation1 ();

}

Public void sampleoperation2 (){

// Write your code here

}

Private adaptee;

}

Code List 5. source code of the adapter.

Package com. javapatterns. Adapter;

Public class adaptee {

Public void sampleoperation1 (){}

}

Code List 6. Source Code of adaptee.

Effect of transformer mode in instance form

1. A transformer can adapt multiple sources to the same target. In other words, the same transformer can adapt both the source class and its subclass to the target interface.

Second, compared with the class-form transformer mode, it is not easy to replace the source class method. If you must replace one or more methods of the source class, you have to first create a subclass of the source class and replace the methods of the source class, then, the Child classes of the source class are adapted as the real source.

Third, although it is not easy to replace the method of the source class, it is very convenient to add some new methods. The newly added method also applies to all sources.

Under what circumstances does the transformer mode work?

Use the transformer mode in the following situations:

First, you need to use existing classes, and such interfaces do not meet your needs.

Second, you want to create a reusable class to work with some classes that are not closely related to each other, including some classes that may be introduced in the future. These source classes do not necessarily have very complex interfaces.

Third, you need to change the interfaces of multiple existing sub-classes (for example, transformer mode). If you are using transformer mode, you need to create a transformer class for each sub-class, this is not practical.

Use of transformer mode in j2se

There are many transformer classes in the standard SDK of Java language 2.0. For example:

The library package Java/AWT/event has

Componentadapter

Containeradapter

Focusadapter

Hierarchyboundsadapter

Keyadapter

Mouseadapter

Mousemotionadapter

Windowadapter

The library package javax/swing/event contains

Internalframeadapter

Mouseinputadapter

These are examples of transformer mode. It is worth noting that the creators of windowadapter cannot predict the target interface you want to use, so windowadapter cannot implement your target interface. However, after examining the use range of these transformer classes, we will find that windowadapter only needs to implement the windowlistener interface, that is, the target interface is omitted. See the following explanation.

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.