设计模式常见面试题

1.写一个单例类。

// 第一种形式:饿汉式单例
public class Singleton {  
    private Singleton(){}  
    private static Singleton instance = new Singleton();  
    public static Singleton getInstance(){  
        return instance;  
    }  
}  

// 第二种形式:懒汉式单例
public class Singleton {  
    private static Singleton instance = null;  
    private Singleton() {}  
    public static synchronized Singleton getInstance(){  
        if (instance==null) instance=newSingleton();  
        return instance;  
    }  
}  

2. 说说你所熟悉或听说过的设计模式以及在开发中使用到了哪些设计模式? 

    创建型:简单工厂,工厂方法,抽象工程,builder模式,singleton,prototype(clone)

    结构型:适配器,装饰,桥接,外观,合成,代理模式

    行为型:命令模式,观察者,策略模式,模板,状态模式,迭代器模式,备忘录模式

猜你喜欢

转载自labreeze.iteye.com/blog/2339590
今日推荐