0%

简单工厂模式

简单工厂模式

简单工厂模式是提供一个创建对象实例的功能,而无需关心具体实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private final TransactionManager transactionManager;

public SimpleTransactionFactory(TransactionManager transactionManager) {
Assert.notNull(transactionManager, "TransactionManager must not be null");
this.transactionManager = transactionManager;
}


@Override
public Transaction createTransaction(String name, int timeout) throws NotSupportedException, SystemException {
if (timeout >= 0) {
this.transactionManager.setTransactionTimeout(timeout);
}
this.transactionManager.begin();
return new ManagedTransactionAdapter(this.transactionManager);
}

用户只要产品,而不关心产品如何生产,其需要在工厂类中包含必要的逻辑判断,根据客户端的选择来动态实例化相关的类,但是每次添加新的产品,都需要在工厂中加入新的判断逻辑