建立型模式-單例模式-工廠模式

yyx發表於2021-02-02

單例模式

處理資源訪問衝突,表示全域性唯一類

//餓漢式
public class Hungry {
    private static final Hungry hungry = new Hungry();
    private Hungry(){}
    public Hungry getInstance(){
        return hungry;
    }
}
//懶漢式
public class Lazy {
    private static Lazy lazy = null;
    public Lazy getInstance(){
        if (lazy == null){
            lazy = new Lazy();
            return lazy;
        }
        else return lazy;
    }

}
//雙重檢測
public class DoubleLock {
    private static  DoubleLock doublelock = null;
    private DoubleLock(){};
    public DoubleLock getInstance(){
        if (doublelock == null) {
            synchronized (DoubleLock.class){
                if (doublelock == null) {
                doublelock = new DoubleLock();
                return doublelock;
                }
            }
        }
        return doublelock;
    }
}
//靜態內部類
public class InnerClass {
    private InnerClass(){
        if (LazyInnerClass.innerClass != null){
            throw new RuntimeException("不能使用私有構造器建立新的例項");
        }
    }
    public InnerClass getInstance(){
        return LazyInnerClass.innerClass;
    }
    private static class LazyInnerClass{
        private static final InnerClass innerClass = new InnerClass();
    }
}
//列舉
public enum  EnumSingle {
    INSTANCE;
    public static EnumSingle getInstance(){
        return INSTANCE;
    }

}

從業務概念上,有些資料在系統中只應該儲存一份,就比較適合設計為單例類。比如,系統的配置資訊類。除此之外,我們還可以使用單例解決資源訪問衝突的問題。

JDK中Runtime類就是使用的餓漢式載入,執行時的狀態在當前狀態就只有一份。

java.awt.Desktop中的getDesktop()獲取你的桌面,使用的是懶漢式載入。

工廠模式

當我們需要建立很多物件的時候可以使用工廠方法,利用工廠來建立物件從而隱藏建立的細節,並且通過new工廠介面實現對應需要建立物件的子類工廠可以建立不同的物件並且容易擴充套件工廠

public interface Book {
    public String getName();
    public String getAuth();
    public Double getPrice();
}
public class JavaBook implements Book {
    private String name;
    private String auth;
    private Double price;

    public JavaBook(String name, String auth, Double price) {
        this.name = name;
        this.auth = auth;
        this.price = price;
    }

    public String getName() {
        return this.name;
    }

    public String getAuth() {
        return this.auth;
    }

    public Double getPrice() {
        return this.price;
    }
}

使用工廠模式建立

public interface BookFactory {
    Book Create();
}
public class JavaBookFactory implements BookFactory {
    @Override
    public Book Create() {
        return new JavaBook("1","2",3.0);
    }
}
public class Test {
    public static void main(String[] args) {
        BookFactory bookFactory = new JavaBookFactory();
        JavaBook create = (JavaBook)bookFactory.Create();
        System.out.println(create.getAuth());
    }
}

Java原始碼中的Calender

private static Calendar createCalendar(TimeZone zone,
                                       Locale aLocale)
{
    CalendarProvider provider =
        LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)
                             .getCalendarProvider();
    if (provider != null) {
        try {
            return provider.getInstance(zone, aLocale);
        } catch (IllegalArgumentException iae) {
            // fall back to the default instantiation
        }
    }

應用場景:

  • JDK中Calendar的getlnstance方法
  • JDBC中的Connection物件的獲取
  • Spring中IOC容器建立管理bean物件
  • 反射中Class物件的newlnstance方法

相關文章