Design Pattern in. Net -- Discuss adapter pattern from instance

Source: Internet
Author: User

In Series 1, the RM and MPEG classes inherit the videomedia abstract class, while the videomedia class implements the imedia interface, which only provides the play () method. Ice soda is intended to provide RM, MPEG with different attributes and methods than audiomedia. For example, for Video Media, there should be a method to adjust the screen size, such as resize (). This method is not available for the imedia interface.

So how does one provide the resize () method not available in the imedia interface for the RM and MPEG classes? Naturally, through this question, we will introduce the proposition of the adapter model. First, assume that allCode, We cannot change. This includes the relationships between exposed interfaces, classes, and interfaces, and cannot achieve new goals through encoding. Only in this way can the adapter mode be introduced.

Anyone familiar with the adapter mode knows that the adapter mode is divided into two types: the adapter mode of the class and the adapter mode of the object. Below I try to illustrate and implement the two methods according to this example. Before implementing the adapter mode, it is necessary to look at the original class structure:

The orange class on the left is the audio media type, and the blue class on the right is the video media type. All these types, including classes and interfaces, cannot be changed. Now our goal is to make RM and MPEG have the resize () method. First define an interface ivideomedia, which has the resize () method.

Next we will implement the requirement according to the adapter mode.

I. Adapter mode of the class

Since RM and MPEG have the resize () method, the best way is to directly implement the ivedioscreen interface. However, restricted by conditions, these two types cannot be modified. The only feasible method is to introduce a new class type for the corresponding class, which is the so-called adapter class in the adapter mode. It is like a adapter that implements the ivedioscreen interface and inherits the original rm or MPEG class to retain the original behavior. The class diagram is as follows:

The class rmadapter and mpegadapter in the figure are the objects obtained through the adapter mode. They retain the original behavior while having the ivedioscreen function.
The Code is as follows:
Public interface ivedioscreen
{
Void resize ();
}

Public class rmadapter: Rm, ivedioscreen
{
Public void resize ()
{
MessageBox. Show ("change the RM screen's size .");
}
}

Public class mpegadapter: MPEG, ivedioscreen
{
Public void resize ()
{
MessageBox. Show ("change the mpeg screen's size .");
}
}

Many people may have noticed that when using this method to create an adapter, there is a limitation that we must create a corresponding adapter class for every class to be wrapped (wrapping. The RM mentioned above corresponds to rmadapter and MPEG corresponds to mpegadapter. This must be the case. Why? Although RM and MPEG inherit the same abstract class vediomedia, their play () methods may be different. In this case, the adpater class can retain the behavior nature of its original play () method only by directly inheriting the specific class.

An important idea in OOP is to try to use aggregation instead of inheritance. Let's look at the adapter mode in another way.

Ii. Object Adapter Mode

The main difference between the adapter mode of an object and the adapter mode of the class is that the object to be wrapped is not inherited, but is put into the adapter in an aggregate manner, then, call the method of its object in delegate mode. The implementation class diagram is as follows:

Comparing the class diagrams of the two implementation methods, we can draw two conclusions:
1. The Object Adapter mode reduces the number of objects;
2. More loose coupling;

The Code is as follows:
Public interface ivedioscreen
{
Void resize ();
}

Public class vedioadapter: ivedioscreen
{
Private vediomedia _ vedio;

Public vedioadapter (vediomedia vedio)
{
_ Vedio = vedio;
}

Public void play ()
{
_ Vedio. Play ();
}

Public void resize ()
{
If (_ vedio is RM)
MessageBox. Show ("change the RM screen's size .");
Else
MessageBox. Show ("change the mpeg screen's size .");
}
}

The vedioadapter formed in this way does not have a direct relationship with RM and MPEG, and waits for the client to use the adapter by passing parameters in the constructor, to pass the specific vediomedia object to the adapter, it seems that the coupling is more loose and more flexible.

Let's take a look at the differences between the two when the client is called:
1. Adapter mode of the class
Public class client
{
Public static void main ()
{
Rmadapter = new rmadapter ();
Mpegadapter = new mpegadapter ();

Rmadapter. Play ();
Rmadapter. Resize ();
Mpegadapter. Play ();
Mpegadapter. Resize ();
}
}
2. Object Adapter Mode
Public class client
{
Public static void main ()
{
Vedioadapter rmadapter = new vedioadapter (New RM ());
Vedioadapter mpegadapter = new vedioadapter (New MPEG ());

Rmadapter. Play ();
Rmadapter. Resize ();
Mpegadapter. Play ();
Mpegadapter. Resize ();
}
}

In fact, we can also make some improvements to the Object Adapter mode, that is, replacing the constructor with properties or methods to pass the wrapped object. The code is modified as follows:
Public class vedioadapter: ivedioscreen
{
Private vediomedia _ vedio;

Public vediomedia vedio
{
Set {_ vedio = value ;}
}
......
}
In this way, the above client call is simpler:
Public class client
{
Public static void main ()
{
Vedioadapter adapter = new vedioadapter ();
Adapter. vedio = new RM ();
Adapter. Play ();
Adapter. Resize ();

Adapter. vedio = New MPEG ();
Adapter. Play ();
Adapter. Resize ();
}
}

By using the adapter mode, the new interface is extended, and the original type does not need to be changed. This is the essence of the adapter mode and the reason why it is named adapter. At the same time, we should note that when using the adapter mode, we must evaluate the current situation, select the optimal method based on the specific situation, or adopt the adapter mode of the class, or use the Object Adapter mode. The decision lies with you. The menu is sent to you. Look at your pockets and think about what kind of food you want.

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.