精心编写的适配器模式、装饰模式

精心编写的适配器模式、装饰模式_第1张图片

希望客官开心的观看

精心编写的适配器模式、装饰模式_第2张图片

前言

前面介绍了三种工厂模式,面试除了工厂模式之外还有其他的模式会问,本次我将说说适配器模式,装饰模式,还是老规矩,放在一个.java里面,复制即可食用

精心编写的适配器模式、装饰模式_第3张图片

适配器模式

适配器模式分为类适配器对象适配器。类适配器比较直接,继承待适配类,实现目标接口,使得待适配类满足目标接口的特定功能。直白一点说需要加接口Traget,在接口中调用Adaptee中的方法.这样就可以实现新的扩展.

下面是类适配器

流程图:

精心编写的适配器模式、装饰模式_第4张图片

测试表代码

package sample;

//类适配器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() {
            //处理过程
            super.requestAdaptee();
            //处理过程
        }
    }


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


结果:

system.out.println(adaptee)

对象适配器

对象适配器与类适配器不同之处在于,类适配器通过继承来完成适配,对象适配器则是通过关联来完成,这里稍微修改一下 TargetAdaptee 类即可将转变为对象适配器

流程图:

精心编写的适配器模式、装饰模式_第5张图片

测试表代码

    static class TargetAdaptee implements  Target{
        Adaptee adaptee = new Adaptee();
        @Override
        public void request() {
            //处理过程
            adaptee.requestAdaptee();
            //处理过程
        }
    }

装饰模式

装饰模式,顾名思义,就是将某个类重新装扮一下,使得它比原来更“漂亮”,或者在功能上更强大,这就是装饰器模式所要达到的目的。但是作为原来的这个类的使用者还不应该感受到装饰前与装饰后有什么不同,即用法不变,否则就破坏了原有类的结构了,所以装饰器模式要做到对被装饰类的使用者透明,这是对装饰器模式的一个基本要求

流程图:

精心编写的适配器模式、装饰模式_第6张图片

package sample;

//装饰模式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("装饰器添加的输出");
            System.out.println("system.out.print(Apple)");
            System.out.println("装饰器输出结束");
        }
    }

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

        //装饰模式
        Apple decoratorApple = new ConcreteDecorator(apple);
        decoratorApple.sout();
    }
}


结果:

system.out.print(apple)

装饰器添加的输出
system.out.print(Apple)
装饰器输出结束

总结

适配器模式的意义

将一个接口转变成另一个接口,目的是通过改变接口来达到重复使用的目的。

装饰器模式的意义

不改变被装饰对象的接口,而是保持原有的接口,增强原有对象的功能,或改变原有对象的处理方式而增提高性能。

今天周天,大家注意休息,明天就要上班了

精心编写的适配器模式、装饰模式_第7张图片

创作不易,如果本篇文章能帮助到你,请给予支持,赠人玫瑰,手有余香,虫虫蟹蟹观众姥爷了

你可能感兴趣的:(设计模式,设计模式,java,接口,spring)