What is REST architecture?

What design pattern would you use to design connecting pool?

  • The object pool pattern that uses a set of initialized objects kept ready to use a "pool" rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object to the pool rather than destroying it.

Implement a thread-safe Singleton Class

这个方法是thread-safe的, 但是如果题目中说创建一个sinlgeton class 非常expensive 那么就不能用这种方法, 要用lazy loading 加上 synchronized 来做

public class Singleton{
    private static final Singleton instance = new Singleton();

    //make the constructor private so that this class cannot be instantiated
    private Singleton(){

    }

    public static Singleton getInstance(){
        return instance;
    }

   public void showMessage(){
      System.out.println("Hello World!");
   }
}

//test
public class SingletonTest{
    public static void main(Stirng[] args){
        Singleton instance = Singleton.getInstance();
        instance.showMessage();
    }
}

//lazy loading + synchrinized
public class Singleton{
    private static final Singleton instance = null;

    //make the constructor private so that this class cannot be instantiated
    private Singleton(){

    }

    public synchronized static Singleton getInstance(){
        if(instance==null){
            instance = new Singleton();
        }
        return instance;
    }

   public void showMessage(){
      System.out.println("Hello World!");
   }
}

Suppose you have an application that uses the Singleton Design pattern for one of it’s classes. But, the problem is that the singleton is expensive to create, because a resource intensive database access is necessary to create the singleton. What can you do to possibly add some efficiency to the process of creating a singleton?

Decorator Pattern

装饰器模式详解

When and why would you favor the Decorator pattern over inheritance?