市场交易中使用到建造者模式的例子
时间: 2024-06-11 09:10:04 浏览: 9
股票交易系统中,可以使用建造者模式来创建订单对象。订单对象包括股票代码、价格、数量、买卖类型等属性。使用建造者模式可以将订单对象的创建过程分离出来,使得订单对象的创建更加灵活和可扩展。具体实现可以参考下面的代码示例:
```java
public class StockOrder {
private String stockCode;
private double price;
private int quantity;
private String orderType;
public StockOrder(String stockCode, double price, int quantity, String orderType) {
this.stockCode = stockCode;
this.price = price;
this.quantity = quantity;
this.orderType = orderType;
}
// 省略getter和setter方法
}
public interface OrderBuilder {
OrderBuilder setStockCode(String stockCode);
OrderBuilder setPrice(double price);
OrderBuilder setQuantity(int quantity);
OrderBuilder setOrderType(String orderType);
StockOrder build();
}
public class BuyOrderBuilder implements OrderBuilder {
private StockOrder stockOrder;
public BuyOrderBuilder() {
this.stockOrder = new StockOrder("", 0.0, 0, "buy");
}
public OrderBuilder setStockCode(String stockCode) {
this.stockOrder.setStockCode(stockCode);
return this;
}
public OrderBuilder setPrice(double price) {
this.stockOrder.setPrice(price);
return this;
}
public OrderBuilder setQuantity(int quantity) {
this.stockOrder.setQuantity(quantity);
return this;
}
public OrderBuilder setOrderType(String orderType) {
return this;
}
public StockOrder build() {
return this.stockOrder;
}
}
public class SellOrderBuilder implements OrderBuilder {
private StockOrder stockOrder;
public SellOrderBuilder() {
this.stockOrder = new StockOrder("", 0.0, 0, "sell");
}
public OrderBuilder setStockCode(String stockCode) {
this.stockOrder.setStockCode(stockCode);
return this;
}
public OrderBuilder setPrice(double price) {
this.stockOrder.setPrice(price);
return this;
}
public OrderBuilder setQuantity(int quantity) {
this.stockOrder.setQuantity(quantity);
return this;
}
public OrderBuilder setOrderType(String orderType) {
return this;
}
public StockOrder build() {
return this.stockOrder;
}
}
public class OrderDirector {
public StockOrder constructBuyOrder(String stockCode, double price, int quantity) {
return new BuyOrderBuilder()
.setStockCode(stockCode)
.setPrice(price)
.setQuantity(quantity)
.build();
}
public StockOrder constructSellOrder(String stockCode, double price, int quantity) {
return new SellOrderBuilder()
.setStockCode(stockCode)
.setPrice(price)
.setQuantity(quantity)
.build();
}
}
public class Client {
public static void main(String[] args) {
OrderDirector director = new OrderDirector();
StockOrder buyOrder = director.constructBuyOrder("AAPL", 130.0, 100);
System.out.println(buyOrder);
StockOrder sellOrder = director.constructSellOrder("AAPL", 140.0, 50);
System.out.println(sellOrder);
}
}
```
在上述代码中,StockOrder表示订单对象,BuyOrderBuilder和SellOrderBuilder表示订单对象的建造者,OrderDirector表示订单对象的创建指导者。在客户端代码中,通过调用OrderDirector的constructBuyOrder或constructSellOrder方法来创建订单对象。通过使用建造者模式,可以将订单对象的创建过程分离出来,同时也可以方便地扩展订单对象的属性和建造者的实现。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)