某单据管理系统中要求实现对采购、销售、库存的单据管理,系统的采购类的单据中有如采购单、收货单、应付帐单、付款单和退货单等,现使用抽象工厂方法设计模式设计该系统,绘制类图并编程实现。 实验分析:单据的本质就是资金的“收入”与“支出”,所以分为收入与支出两个工厂来生产单据。 采购(Purchase):采购单(支出)、退货单(收入) 销售(Sale):退款单(支出)、销售单(收入) 库存(Stock):入库单(支出)、出库单(收入)
时间: 2023-06-18 12:08:56 浏览: 292
设计模式:单例设计模式(全部实现方式)工厂设计模式,抽象工厂模式
以下是该系统的抽象工厂方法设计模式的类图:
![类图](https://i.ibb.co/9WJNz8G/Bill-Management-System.png)
具体实现如下:
```java
// 抽象工厂
interface BillFactory {
PurchaseBill createPurchaseBill();
SaleBill createSaleBill();
StockBill createStockBill();
}
// 支出工厂
class ExpenseFactory implements BillFactory {
public PurchaseBill createPurchaseBill() {
return new PurchaseBill();
}
public SaleBill createSaleBill() {
return new RefundBill();
}
public StockBill createStockBill() {
return new InStockBill();
}
}
// 收入工厂
class IncomeFactory implements BillFactory {
public PurchaseBill createPurchaseBill() {
return new RefundBill();
}
public SaleBill createSaleBill() {
return new SaleBill();
}
public StockBill createStockBill() {
return new OutStockBill();
}
}
// 抽象单据类
abstract class Bill {
protected String id;
protected Date date;
protected double amount;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public abstract void printBill();
}
// 采购类单据
abstract class PurchaseBill extends Bill {}
class PurchaseOrderBill extends PurchaseBill {
public void printBill() {
System.out.println("采购单:id=" + id + ", date=" + date + ", amount=" + amount);
}
}
class PurchaseReceiptBill extends PurchaseBill {
public void printBill() {
System.out.println("收货单:id=" + id + ", date=" + date + ", amount=" + amount);
}
}
class PayableBill extends PurchaseBill {
public void printBill() {
System.out.println("应付帐单:id=" + id + ", date=" + date + ", amount=" + amount);
}
}
class PaymentBill extends PurchaseBill {
public void printBill() {
System.out.println("付款单:id=" + id + ", date=" + date + ", amount=" + amount);
}
}
class PurchaseReturnBill extends PurchaseBill {
public void printBill() {
System.out.println("退货单:id=" + id + ", date=" + date + ", amount=" + amount);
}
}
// 销售类单据
abstract class SaleBill extends Bill {}
class RefundBill extends SaleBill {
public void printBill() {
System.out.println("退款单:id=" + id + ", date=" + date + ", amount=" + amount);
}
}
class SalesBill extends SaleBill {
public void printBill() {
System.out.println("销售单:id=" + id + ", date=" + date + ", amount=" + amount);
}
}
// 库存类单据
abstract class StockBill extends Bill {}
class InStockBill extends StockBill {
public void printBill() {
System.out.println("入库单:id=" + id + ", date=" + date + ", amount=" + amount);
}
}
class OutStockBill extends StockBill {
public void printBill() {
System.out.println("出库单:id=" + id + ", date=" + date + ", amount=" + amount);
}
}
// 测试
public class Test {
public static void main(String[] args) {
// 支出工厂
BillFactory expenseFactory = new ExpenseFactory();
PurchaseBill purchaseOrderBill = expenseFactory.createPurchaseBill();
purchaseOrderBill.setId("PO0001");
purchaseOrderBill.setDate(new Date());
purchaseOrderBill.setAmount(1000);
purchaseOrderBill.printBill();
SaleBill refundBill = expenseFactory.createSaleBill();
refundBill.setId("R0001");
refundBill.setDate(new Date());
refundBill.setAmount(500);
refundBill.printBill();
StockBill inStockBill = expenseFactory.createStockBill();
inStockBill.setId("IS0001");
inStockBill.setDate(new Date());
inStockBill.setAmount(800);
inStockBill.printBill();
// 收入工厂
BillFactory incomeFactory = new IncomeFactory();
PurchaseBill purchaseReturnBill = incomeFactory.createPurchaseBill();
purchaseReturnBill.setId("PR0001");
purchaseReturnBill.setDate(new Date());
purchaseReturnBill.setAmount(200);
purchaseReturnBill.printBill();
SaleBill salesBill = incomeFactory.createSaleBill();
salesBill.setId("S0001");
salesBill.setDate(new Date());
salesBill.setAmount(3000);
salesBill.printBill();
StockBill outStockBill = incomeFactory.createStockBill();
outStockBill.setId("OS0001");
outStockBill.setDate(new Date());
outStockBill.setAmount(500);
outStockBill.printBill();
}
}
```
阅读全文