從Spring框架看設計模式如何靈活使用

Singleton 單例模式

單例模式是確保每個應用程序只存在一個實例的機制。默認情況下,Spring將所有bean創建爲單例。

單例模式

你用@Autowired獲取的bean,全局唯一。

@RestController
public class LibraryController {
    
    @Autowired
    private BookRepository repository;

    @GetMapping("/count")
    public Long findCount() {
        System.out.println(repository);
        return repository.count();
    }
}

工廠方法模式

Spring 定義了BeanFactory接口,抽象對象容器:

public interface BeanFactory {

    getBean(Class<T> requiredType);
    getBean(Class<T> requiredType, Object... args);
    getBean(String name);

    // ...
]

每一個getBean 方法其實就是一個工廠方法。

代理模式(Proxy)

代理模式

在Spring中,對於事務,我們可以加一個@Transactional註解,

@Service
public class BookManager {
    
    @Autowired
    private BookRepository repository;

    @Transactional
    public Book create(String author) {
        System.out.println(repository.getClass().getName());
        return repository.create(author);
    }
}

Spring框架,通過AOP做Proxy。

Decorator裝飾器模式

Spring 中的TransactionAwareCacheDecorator 就做了對Cache 的包裝:

public interface Cache {
    String getName();

    Object getNativeCache();

    @Nullable
    Cache.ValueWrapper get(Object var1);

    @Nullable
    <T> T get(Object var1, @Nullable Class<T> var2);

    @Nullable
    <T> T get(Object var1, Callable<T> var2);

    void put(Object var1, @Nullable Object var2);
}

TransactionAwareCacheDecorator 實現了Cache接口,構造時傳入一個targetCache,在調用put等方法時,增加了自己裝飾邏輯在裏面。


public class TransactionAwareCacheDecorator implements Cache {
    private final Cache targetCache;

    public TransactionAwareCacheDecorator(Cache targetCache) {
        Assert.notNull(targetCache, "Target Cache must not be null");
        this.targetCache = targetCache;
    }

    public void put(final Object key, @Nullable final Object value) {
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                public void afterCommit() {
                    TransactionAwareCacheDecorator.this.targetCache.put(key, value);
                }
            });
        } else {
            this.targetCache.put(key, value);
        }

    }

最佳實踐

  • 裝飾模式是繼承的有力補充。相比於繼承,裝飾模式可以增加代碼的可維護性、擴展性、複用性。在一些情況下裝飾模式可以替代繼承,解決類膨脹問題。

  • 裝飾模式有利於程序的可擴展性。在一個項目中,有很多因素考慮不周,特別是業務的變更。通過裝飾模式重新封裝一個裝飾類,可以避免修改繼承體系中的中間類,而是使用裝飾類修飾中間類,這樣原有的程序沒有變更,通過擴展完成了這次變更。

組合模式(Composite)

Spring actuate 提供HealthIndicator, 用於監控服務健康狀態。

@FunctionalInterface
public interface HealthIndicator {

    /**
     * Return an indication of health.
     * @return the health for
     */
    Health health();

}

實現類裏,有一個CompositeHealthIndicator, 可以add多個HealthIndicator,放入indicators裏,最後返回health時,聚合所有indicatorsHealth


public class CompositeHealthIndicator implements HealthIndicator {

    private final Map<String, HealthIndicator> indicators;

    private final HealthAggregator healthAggregator;

    /**
     * Create a new {@link CompositeHealthIndicator}.
     * @param healthAggregator the health aggregator
     */
    public CompositeHealthIndicator(HealthAggregator healthAggregator) {
        this(healthAggregator, new LinkedHashMap<>());
    }

    /**
     * Create a new {@link CompositeHealthIndicator} from the specified indicators.
     * @param healthAggregator the health aggregator
     * @param indicators a map of {@link HealthIndicator}s with the key being used as an
     * indicator name.
     */
    public CompositeHealthIndicator(HealthAggregator healthAggregator,
            Map<String, HealthIndicator> indicators) {
        Assert.notNull(healthAggregator, "HealthAggregator must not be null");
        Assert.notNull(indicators, "Indicators must not be null");
        this.indicators = new LinkedHashMap<>(indicators);
        this.healthAggregator = healthAggregator;
    }

    public void addHealthIndicator(String name, HealthIndicator indicator) {
        this.indicators.put(name, indicator);
    }

    @Override
    public Health health() {
        Map<String, Health> healths = new LinkedHashMap<>();
        for (Map.Entry<String, HealthIndicator> entry : this.indicators.entrySet()) {
            healths.put(entry.getKey(), entry.getValue().health());
        }
        return this.healthAggregator.aggregate(healths);
    }

}

感謝您的認真閱讀。

如果你覺得有幫助,歡迎點贊支持!

不定期分享軟件開發經驗,歡迎關注作者, 一起交流軟件開發:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章