Design Pattern: adapter mode-class Adapter

Source: Internet
Author: User

Http://www.35java.com/zhibo/forum.php? MoD = viewthread & tid = 271 & extra = Page % 3d2

Another method of the adapter mode is the class adapter mode. In this mode, the adapter directly inherits adaptee (the new category to be introduced) to own the members and methods in it, in C ++, you can do this:

In C ++, multiple inheritance can be performed, but not in Java. To use the class adapter in Java, you must make some modifications. On the one hand, you must inherit the adaptee and implement the target interface:

CodeThe implementation is as follows:

Public class adapter extends adaptee implements target {
//....
}

Of course, this requires that your original target define a common interface, so the class adapter is rarely used in Java. In fact, objectadapter is also recommended, this adapter mode is flexible. For example, you can add a setter on the adapter to change the adaptee at any time.

In Java, one application of class adapter achieves multi-inheritance. You must listen to others many times. interfaces can achieve multi-inheritance, what's going on?

In fact, to discuss this issue, you must first have an understanding of the Multi-inheritance in C ++. When a newbie reads the book and says that the interface can achieve multi-inheritance, it's not just a matter of time, especially for beginners who have never learned C ++, if you want to know more about C ++ multi-inheritance, please first take a lookMultiple inheritance (1)AndMulti-inheritance (2).

Java cannot be inherited multiple times, but why is interface (Interface) used in Java to achieve the effect of multi-inheritance? First, one of the effects is as follows:Multi-inheritance (2)"When multiple inheritance is described in, one of the base classes is often used as the private entity, while the others are used to represent a complete abstract interface .", In Java, this effect can be achieved through interfaces.Multi-inheritance (2)Abstract category in, a complete abstract interface, the way this effect is achieved, suchInterface TypeFor more information about how to use interfaces in Java, see program implementation for the two topics) to achieve the "one" effect of multi-inheritance in C ++.

Let's look at another situation.

If there are someclass and otherclass categories, you want the someandother category to have the operations defined in both the someclass and otherclass categories, and you can perform multi-type operations, multi-inheritance can be used in C ++, but it is obvious that multi-inheritance cannot be used in Java. What should I do? You can take a detour in the design. First, use two interfaces to define the public methods of someclass and otherclass, for example:
Public interface isome {
Public void dosome ();
}

Public interface iother {
Public void doother ();
}

Then let someclass and otherclass implement two interfaces respectively:
Public class someclass implementsisome {
Public void dosome (){
....
}
}

Public class otherclassimplements iother {
Public void doother (){
....
}
}

How does someandother have two defined operations, someclass and otherclass? Can I perform multiple operations? Someandother can inherit one of them and owns one of them, for example:
Public class someandother extendssomeclass implements iother {
Private iother Other = new otherclass ();

Public void doother (){
Other. doother ();
}
}

Although not satisfied, it at least solved the current problem. Of course, this is only one of them. After all, C ++ is C ++ and Java is Java. the syntax of the two is not one-to-one, it can be changed based on actual needs.

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.