Home  >  Article  >  Java  >  How to implement the adapter pattern of Java design pattern

How to implement the adapter pattern of Java design pattern

WBOY
WBOYforward
2023-04-25 10:37:061453browse

    What is the adapter pattern

    The adapter pattern (Adapter) is defined as follows: Convert the interface of a class into another interface that the customer wants, so that the original Classes that cannot work together because of incompatible interfaces can work together. The adapter pattern is divided into two types: class structural pattern and object structural pattern. The former has a higher degree of coupling between classes than the latter, and requires programmers to understand the internal structure of relevant components in the existing component library, so it is relatively rarely used. some.

    Advantages

    1. The client can transparently call the target interface through the adapter.

    2. Existing classes are reused. Programmers do not need to modify the original code and reuse existing adapter classes.

    3. Decouple the target class and the adapter class to solve the problem of inconsistent interfaces between the target class and the adapter class.

    4. Comply with the opening and closing principle in many business scenarios.

    Disadvantages

    1. The adapter writing process needs to be comprehensively considered in conjunction with the business scenario, which may increase the complexity of the system.

    2. Increase the difficulty of code reading and reduce code readability. Excessive use of adapters will make the system code messy.

    Knowledge Point

    Class Adapter Pattern: It can be implemented using multiple inheritance. For example, C can define an adapter class to inherit both the business interface of the current system and the components that already exist in the existing component library. Interface; Java does not support multiple inheritance, but you can define an adapter class to implement the business interface of the current system while inheriting components that already exist in the existing component library.

    Object adapter mode: You can introduce components that have been implemented in the existing component library into the adapter class, which also implements the business interface of the current system. Now let's introduce their basic structure.

    Adapter mode implementation

    Case: Hongmao uses fire crystals to trigger the Fire Dance Whirlwind Sword Qi

    Target interface: Changhong Sword Qi

    Adaptee type: Fire Dance Whirlwind Sword Qi

    Adapter type: Fire Crystal

    Hongmao can only activate the Changhong Sword Qi at present, but he wants to activate the fire Wu Xuanfeng Sword Qi

    Can only use the power of fire crystal to transform Changhong Sword Qi into Fire Wu Xuanfeng Sword Qi

    Note: I am here to adapt Huowu Xuanfeng Sword Qi to Changhong Jianqi

    Class Adapter

    Changhong Jianqi

    Declares an interface for Changhong Jianqi, including an abstract method to activate Changhong Jianqi

    public interface Chang {
        void chang();
    }
    Fire Dance Whirlwind Sword Qi

    Fire Dance Whirlwind Sword Qi category, declares a method to activate the Fire Dance Whirlwind Sword Qi.

    public class Huo {
        public void huo() {
            System.out.println("火舞旋风剑气");
        }
    }
    fire crystal stone

    The fire crystal stone class inherits the Fire Dance Whirlwind Sword Qi class (purpose: you can use the methods in the class after inheriting), and implements the Changhong Sword Qi interface (purpose : Method to realize the activation of Changhong Sword Qi). Among the methods for activating the Changhong Sword Qi, the method for activating the Fire Dance Whirlwind Sword Qi is used.

    public class JingShi extends Huo implements Chang {
        @Override
        public void chang() {
            huo();
        }
    }
    Test
    public class Demo {
        public static void main(String[] args) {
            Chang haha = new JingShi();
            haha.chang();
        }
    }

    How to implement the adapter pattern of Java design pattern

    Object Adapter

    Changhong Jianqi

    Declares an interface of Changhong Jianqi, including An abstract method to activate the Changhong Sword Qi

    public interface Chang {
        void chang();
    }
    火狐whirlwind Sword Qi

    Fire Dance Whirlwind Sword Qi class declares a method to activate the Fire Dance Whirlwind Sword Qi.

    public class Huo {
        public void huo() {
            System.out.println("火舞旋风剑气");
        }
    }
    fire crystal stone

    The fire crystal stone class implements the Changhong Sword Qi interface, declares the properties of a Fire Dance Whirlwind Sword Qi, and implements the method of stimulating the Changhong Sword Qi. The method of activating the Fire Dance Whirlwind Sword Qi is included in the method of activating the Changhong Sword Qi.

    public class JingShi implements Chang {
        private Huo huo;
        JingShi() {
        }
        JingShi(Huo huo) {
            this.huo = huo;
        }
        @Override
        public void chang() {
            huo.huo();
        }
    }
    Test

    New a Fire Dance Whirlwind Sword Qi object, new a Fire Crystal Stone object, and pass the Fire Dance Whirlwind Sword Qi object into the Fire Crystal Stone object.

    public class Demo {
        public static void main(String[] args) {
            Huo huo = new Huo();
            Chang haha = new JingShi(huo);
            haha.chang();
        }
    }

    How to implement the adapter pattern of Java design pattern

    The above is the detailed content of How to implement the adapter pattern of Java design pattern. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete