Design Mode-adapter Mode

Source: Internet
Author: User

Design Mode-adapter Mode
The adapter mode defines two incompatible classes to be used together. It is a structural mode and requires Adapee and Adaptor ). why do we often encounter the problem of combining two unrelated classes? The first solution is to modify the interfaces of their classes, but if we do not have the source code, or, we do not want to modify interfaces for an application. What should I do? At the same time, modifying interfaces can also damage the entire architecture, which is also not desirable. so we thought of using the Adapter to create a Hybrid Interface (mixed blood) between the two interfaces ). just like a laptop power adapter. how to use it? As mentioned in the "think in Java" class regeneration "section, there are two methods: composition and inheritance ). suppose we have two trees: apple tree and orange tree. public class Apple {public void insertApple () {System. out. println ("insert a Apple") ;}} public class Org {public void insert () {System. out. println ("insert a Org") ;}} now we have an application that requires both Apple and orange. however, we do not know the source code of Apple or we do not want to change the source code. We can use the Adapter to implement this application. public class FruitAdapter extends Org {private Apple ap Ple; public FruitAdapter (Apple apple) {this. apple = apple;} public void insert () {apple. insertApple () ;}} in the above Code, Apple belongs to Adaptee and is the adapter. fruitAdapter is an Adapter that adapts Adaptee (Apple) to Target (Org. in fact, this is a combination of the composition method and the inheritance Method. fruitAdapter first inherits Org, then uses the new combination to generate the Apple object Apple, and then reloads the parent class insertOrg () method. From here, you also know the differences between generating objects using new and using extends to inherit the generated objects. The former does not need to be modified on the original class, or even has no need to know its internal structure and source code. if you have some experience using Java, we have found that this mode is often used. The above FruitAdapter is used to inherit from Org. If we need to inherit from both sides, that is, inherit from Org and inherit from Apple, because many inheritance are not allowed in Java, but we can implement (implements) two interfaces ). public interface IApple {public void insertApple ();} public interface IOrg {public void insert ();} below is the new Apple and Org. In addition to implementing interfaces, there is no difference with the above. public class Apple implements IApple {public void insertApple () {System. out. println ("insert a Apple") ;}} public class Org implements IOrg {public void inse Rt () {System. out. println ("insert a Org") ;}} The following is a new FruitAdapter called two-way adapter: public class FruitAdapter implements IApple, IOrg {private Apple apple; private Org; public FruitAdapter (Apple apple) {this. apple = apple;} public FruitAdapter (Org org) {this.org = org;} public void insert () {apple. insertApple () ;}} is also known as Pluggable Adapters, which can dynamically obtain one of several adapters. Using Reflection technology, you can dynamically discover Public methods in the class.

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.