0
点赞
收藏
分享

微信扫一扫

从Java中JDK中学习设计模式

1.单例模式

class Demo{
    private static volatile Demo demo;
    // 私有的构造方法
    private Demo(){
    }
    public Demo getSiginTon(){
        if(demo==null){// 如果不为空直接返回
            synchronized (Demo.class){// 加锁
                if(demo==null){//若是被其他线程初始化了,就不用在初始化了
                    demo = new Demo();
                }
            }
        }
        return demo;
    }
}

2.静态工厂模式

    public static Integer valueOf(int i) {
    	//Integer对象会有对象缓存,在-128-127之间
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

3.抽象工厂模式

4.原型模式

5.适配器模式

6.装饰器模式

7.外观模式

8.享元模式

9.代理模式

10.迭代器模式

11.命令模式

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}
@FunctionalInterface
public interface Callable<V> {
    V call() throws Exception;
}
举报

相关推荐

0 条评论