学习spring源码需要的一些设计模式

一、单例设计模式
直接所懒加载吧
1、必须private 一下构造方法,要不别人直接new了
2、为何两个if?外面的if是为了避免后面每次获取都会竞争锁,里面的if是为了两个线程都通过了第一个if,后面拿到锁的线程并不知道第一个线程已经创建了。
3、volatile 这个是为了避免指令重排,lazySingleton = new LazySingleton()并不是一个原子操作,而当多线程获取的时候,类还没创建完成。

public class LazySingleton {

    private volatile static  LazySingleton lazySingleton = null;

    private LazySingleton() {
    }

    public static LazySingleton getLazySingleton(){
        if(lazySingleton == null){
            synchronized (LazySingleton.class){
                if(lazySingleton == null){
                    lazySingleton = new LazySingleton();
                }
            }
        }
        return lazySingleton;
    }

}

二、抽象工厂模式
作用:获取工厂的工厂。如例子6个类只需要三个工厂就搞定了。
为啥用抽象类不用接口?接口改动,实现它的类都需要改动。
在这里插入图片描述
在这里插入图片描述

public class A1 implements AInterface {
    @Override
    public void show() {
        System.out.println("我是"+A1.class.getName());
    }
}
public class AFactory extends AbstractFactory {

    public AInterface getA(int i){
        if(i == 1){
            return new A1();
        }else if(i == 2){
            return new A2();
        }else if(i == 3){
            return new A3();
        }else{
            return null;
        }
    }

    @Override
    public BInterface getB(int i) {
        return null;
    }
}
public interface AInterface {
    public void show();
}
public abstract class AbstractFactory {
    public abstract AInterface getA(int i);
    public abstract BInterface getB(int i);
}
public class AbstractFactoryProducer {
    public AbstractFactory getFactory(String factoryType){
        if("A".equals(factoryType)){
            return new AFactory();
        }else if("B".equals(factoryType)){
            return new BFactory();
        }else{
            return null;
        }
    }
}
public class Main {
    public static void main(String[] args) {
        new AbstractFactoryProducer().getFactory("A").getA(1).show();
    }
}

三、代理模式(源码后续分析)
就是就是一个对象代理另一个业务对象干某些事情,并在这个业务对象的基础上干一些别的事情

public class A {
    public void show(){
        System.out.println("我是业务方法");
    }
}
public class AProxy {
    A a = new A();
    public void show(){
        System.out.println("执行业务方法之前干一些事");
        a.show();
        System.out.println("执行业务方法之前干一些事");
    }
}

就这么个意思,这是静态代理,代理一个增加一个代理类,生成一个class,这。。。。
实际应用用动态代理,springAop当原对象有接口时,jdk动态代理,无接口时,cglib代理
1、jdk动态代理
1)名字特殊不算特殊,代理对象需要实现InvocationHandler 接口。
2)原始对象需要实现一个接口

public class A implements AInterface{
    public void run(){
        System.out.println("A在跑步");
    }
}

public interface AInterface {
    public void run();
}
public class ProxyM implements InvocationHandler {

    private Object obj;

    public ProxyM() {
    }
    public ProxyM(Object obj) {
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("我是代理对象前");
        //原对象的方法
        Object result = method.invoke(obj,args);
        System.out.println("我是代理对象前");
        return result;
    }



    public static void main(String[] args) {
        InvocationHandler invocationHandler = null;
        AInterface a =  new A();
        invocationHandler = new ProxyM(a);

        AInterface aProxy = (AInterface) Proxy.newProxyInstance(AInterface.class.getClassLoader()
                    ,new Class[]{
                        AInterface.class
                }
                ,invocationHandler);

        aProxy.run();
    }
}

2、cglib动态代理

public class ProxyM implements MethodInterceptor {


    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("我是代理对象前");
        Object proceed = methodInvocation.proceed();
        System.out.println("我是代理对象后");
        return proceed;
    }

    public static void main(String[] args) {
        JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        pointcut.setPattern("com.example.demo.sjms.proxy.*");
        // 配置增强类advisor
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(pointcut);
        advisor.setAdvice(new ProxyM());
        System.out.println(advisor.toString());
        
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值