Well written adapter mode, decoration mode

Time:2021-3-14

Well written adapter mode, decoration mode

I hope you can enjoy it

Well written adapter mode, decoration mode

preface

There are three typesFactory modelIn addition to factory mode, there are other modes of interview, which I will talk about this timeAdapter mode,Decoration modeIt’s still the old rule. Put it in theIt's in. JavaCopy and serve

Well written adapter mode, decoration mode

Adapter mode

Adapter modes are divided intoClass Adapter andobject adapter . The class adapter is more direct, inherits the class to be adapted, realizes the target interface, and makes the class to be adapted meet the specific functions of the target interface. To put it bluntly, we need to add interfacesTragetCall in interfaceAdapteeIn this way, the newextend.

Here is the class adapter

flow chart:

Well written adapter mode, decoration mode

Test table code

package sample;

//Class adapter demo
public class AdapteeClassDemo {

    interface Target{
        public void request() ;
    }


    static class Adaptee {
        public void requestAdaptee() {
            System.out.println("system.out.println(adaptee)");
        }
    }

    static class TargetAdaptee extends Adaptee implements  Target{

        @Override
        public void request() {
            //Treatment process
            super.requestAdaptee();
            //Treatment process
        }
    }


    public static void main(String[] args) {
        Target targetAdaptee = new TargetAdaptee();
        targetAdaptee.request();
    }
}

result:

system.out.println(adaptee)

object adapter

The difference between an object adapter and a class adapter is that the class adapter adapts through inheritance, while the object adapter adapts through association. Here, you can change the targetadaptee class to an object adapter by slightly modifying it

flow chart:

Well written adapter mode, decoration mode

Test table code

static class TargetAdaptee implements  Target{
        Adaptee adaptee = new Adaptee();
        @Override
        public void request() {
            //Treatment process
            adaptee.requestAdaptee();
            //Treatment process
        }
    }

Decoration mode

Decoration mode, as the name suggests, is to re dress a class to make it more “beautiful” or more powerful. This is the goal of decorator mode. However, users of the original class should not feel the difference between before decoration and after decoration, that is, the usage remains unchanged, otherwise the structure of the original class will be destroyed, so the decorator mode should be transparent to the users of the decorated class, which is a basic requirement of the decorator mode

flow chart:

Well written adapter mode, decoration mode

package sample;

//Decoration mode demo
public class DecoratorDemo {

    interface Apple{
        public void sout() ;
    }


    static class Adaptee {
        public void requestAdaptee() {
            System.out.println("system.out.println(adaptee)");
        }
    }

    static class ConcreteApple implements  Apple{
        @Override
        public void sout() {
            System.out.println("system.out.print(apple)");
        }
    }

    static class Decorator implements  Apple{
        Apple apple;
        Decorator(Apple apple){
            super();
            this.apple=apple;
        }
        @Override
        public void sout() {
            apple.sout();
        }
    }

    static class ConcreteDecorator extends   Decorator{
        Apple apple;
        ConcreteDecorator(Apple apple){
            super(apple);
        }
        @Override
        public void sout() {
            System.out.println();
            System.out.println ("output added by decorator");
            System.out.println("system.out.print(Apple)");
            System.out.println (end of decorator output);
        }
    }

    public static void main(String[] args) {
        Apple apple = new ConcreteApple();
        apple.sout();

        //Decoration mode
        Apple decoratorApple = new ConcreteDecorator(apple);
        decoratorApple.sout();
    }
}

result:

system.out.print(apple)

Output added by decorator
system.out.print(Apple)
Decorator output end

summary

The significance of adapter pattern

The purpose of changing one interface into another is to achieve the purpose of reuse by changing the interface.

The significance of decorator model

It does not change the interface of the decorated object, but keeps the original interface, enhances the function of the original object, or changes the processing mode of the original object to increase the performance.

Today’s Sunday, everyone pay attention to rest, tomorrow will go to work

Well written adapter mode, decoration mode

It’s not easy to create. If this article can help you, please give me your support. Give me a rose, a fragrance in my hand, and I’ll be happy with you

Recommended Today

OC basis

IOS development interview essential skills chart.png What are objects and what are the objects in OC? An object is an instance of a class; Is an instance created through a class, which is generally called an instance object; Common objects in OC include instance objects, class objects, and metaclass objects; What is a class? What […]