• 设计模式


    工厂模式 BeanFactory
    装饰器模式 BeanWrapper
    代理模式 AopProxy
    单例模式 ApplicationContext
    委派模式 DispatcherServlet
    策略模式 HandlerMapping
    适配器模式 HandlerApdapter
    模板方法模式 JdbcTemplate
    观察者模式 ContextLoaderListener

    单例模式

    单例模式一

    public class Singleton {  
        private volatile static Singleton singleton;  
        private Singleton (){}  
        public static Singleton getSingleton() {  
        if (singleton == null) {  
            synchronized (Singleton.class) {  
            if (singleton == null) {  
                singleton = new Singleton();  
            }  
            }  
        }  
        return singleton;  
        }  
    }
    public class Singleton {
     
        /* 私有构造方法,防止被实例化 */
        private Singleton() {
        }
     
        /* 此处使用一个内部类来维护单例 , SingletonFactory默认在classLoader中不会被加载, 只有在使用到该静态类时才会被加载 */
        private static class SingletonFactory {
            private static Singleton instance = new Singleton();
        }
     
        /* 获取实例 */
        public static Singleton getInstance() {
            return SingletonFactory.instance;
        }
     
        /* 如果该对象被用于序列化,可以保证对象在序列化前后保持一致 */
        public Object readResolve() {
            return getInstance();
        }
    }

    三种适配器模式的应用场景:

    类的适配器模式:当希望将一个类转换成满足另一个新接口的类时,可以使用类的适配器模式,创建一个新类,继承原有的类,实现新的接口即可。

    对象的适配器模式:当希望将一个对象转换成满足另一个新接口的对象时,可以创建一个Wrapper类,持有原类的一个实例,在Wrapper类的方法中,调用实例的方法就行。

    接口的适配器模式:当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类即可。

     

    类适配器

    public class Source { 
        public void method1() {
            System.out.println("this is original method!");
        }
    }
    
    public interface Targetable {
     
        /* 与原类中的方法相同 */
        public void method1();
     
        /* 新类的方法 */
        public void method2();
    }
    
    public class Adapter extends Source implements Targetable {
        @Override
        public void method2() {
            System.out.println("this is the targetable method!");
        }
    }
    
    public class AdapterTest { 
        public static void main(String[] args) {
            Targetable target = new Adapter();
            target.method1();
            target.method2();
        }
    }

    对象适配器

    public class Wrapper implements Targetable {
     
        private Source source;
        
        public Wrapper(Source source){
            super();
            this.source = source;
        }
        @Override
        public void method2() {
            System.out.println("this is the targetable method!");
        }
     
        @Override
        public void method1() {
            source.method1();
        }
    }
    
    public class AdapterTest {
     
        public static void main(String[] args) {
            Source source = new Source();
            Targetable target = new Wrapper(source);
            target.method1();
            target.method2();
        }
    }

    接口适配器

    public interface Sourceable {    
        public void method1();
        public void method2();
    }
    public abstract class Wrapper2 implements Sourceable{    
        public void method1(){}
        public void method2(){}
    }
    public class SourceSub1 extends Wrapper2 {
        public void method1(){
            System.out.println("the sourceable interface's first Sub1!");
        }
    }
    public class SourceSub2 extends Wrapper2 {
        public void method2(){
            System.out.println("the sourceable interface's second Sub2!");
        }
    }
    
    public class WrapperTest {
     
        public static void main(String[] args) {
            Sourceable source1 = new SourceSub1();
            Sourceable source2 = new SourceSub2();
            
            source1.method1();
            source1.method2();
            source2.method1();
            source2.method2();
        }
    }

    装饰器模式

    顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例


    外观模式

    为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

    客户端不需要知道系统内部的复杂联系,整个系统只需提供一个"接待员"即可.

    举个栗子: 去医院看病,可能要去挂号、门诊、划价、取药,让患者或患者家属觉得很复杂,如果有提供接待人员,只让接待人员来处理,就很方便。

    举个栗子2: 电脑整机是 CPU、内存、硬盘的外观。有了外观以后,启动电脑和关闭电脑都简化了。

    外观模式是为了解决类与类之家的依赖关系的,像spring一样,可以将类和类之间的关系配置到配置文件中,而外观模式就是将他们的关系放在一个Facade类中,降低了类类之间的耦合度,该模式中没有涉及到接口,看下类图:(我们以一个计算机的启动过程为例)

    public class Computer {
        private CPU cpu;
        private Memory memory;
        private Disk disk;
        
        public Computer(){
            cpu = new CPU();
            memory = new Memory();
            disk = new Disk();
        }
        
        public void startup(){
            System.out.println("start the computer!");
            cpu.startup();
            memory.startup();
            disk.startup();
            System.out.println("start computer finished!");
        }
        
        public void shutdown(){
            System.out.println("begin to close the computer!");
            cpu.shutdown();
            memory.shutdown();
            disk.shutdown();
            System.out.println("computer closed!");
        }
    }
  • 相关阅读:
    左右下划线,中间文字
    sql语言动词
    SQL语言的四个组成部分
    MySQL执行一条查询语句的内部执行过程
    07 | 行锁功过:怎么减少行锁对性能的影响?
    06 | 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
    05 | 深入浅出索引(下)
    04 | 深入浅出索引(上)
    03 | 事务隔离:为什么你改了我还看不见?
    02 | 日志系统:一条SQL更新语句是如何执行的?
  • 原文地址:https://www.cnblogs.com/snow-man/p/10695910.html
Copyright © 2020-2023  润新知