工厂模式在金融业务中的应用及其框架实现
// 产品接口
public interface Trade {
void execute();
}
// 具体产品类A
public class EquityTrade implements Trade {
@Override
public void execute() {
System.out.println("Executing equity trade");
}
}
// 具体产品类B
public class BondTrade implements Trade {
@Override
public void execute() {
System.out.println("Executing bond trade");
}
}
// 工厂接口
public interface TradeFactory {
Trade createTrade();
}
// 具体工厂类A
public class EquityTradeFactory implements TradeFactory {
@Override
public Trade createTrade() {
return new EquityTrade();
}
}
// 具体工厂类B
public class BondTradeFactory implements TradeFactory {
@Override
public Trade createTrade() {
return new BondTrade();
}
}
// 客户端代码
public class FactoryPatternDemo {
public static void main(String[] args) {
TradeFactory equityFactory = new EquityTradeFactory();
Trade equityTrade = equityFactory.createTrade();
equityTrade.execute();
TradeFactory bondFactory = new BondTradeFactory();
Trade bondTrade = bondFactory.createTrade();
bondTrade.execute();
}
}