目录
1 系统设计与建模
2 开闭原则
3.组合模式
4 工厂方法
5.代码重构
1 系统设计与建模
| 用例ID | UC1 | |
| 用例名称 | 处理销售 | |
| 场景 | 顾客选定欲购买的商品并到收银台计价结账 | |
| 触发事件 | 收银员开始一次新的销售交易 | |
| 简要描述 | 顾客携带所购商品到收银台,收银员计价和收款,并打印票据 | |
| 参与者 | 收银员 | |
| 相关用例 | 处理退货、收款 | |
| 关联人员 | 售货员、顾客、经理 | |
| 前置条件 | 收银员经过确认和认证 | |
| 后置条件 | 存储销售信息,准确计算税金,更新账务和库存信息,记录提成,生成票据,记录支付授权的批准 | |
| 活动流 | 参与者 | 系统 |
| 1.收银员开始一次新的销售交易 2.收银员输入商品条码 3.收银员告知顾客总额,并请顾客付款,顾客付款 4.收银员把票据交给顾客 | 2.1.系统逐条记录出售的商品 2.2.系统显示商品描述、价格和累计额 2.3.系统显示总额和税金 3.1.系统记录完整的销售信息 3.2.系统将销售和支付信息发送到外部的账务系统和库存系统 3.3.系统打印票据 | |
| 异常情形 | ... ... | |
2 开闭原则
public class Apple extends Fruit {
public Apple(Integer id, String name, Double price) {
super(id, name, price);
}
public Double getPromotionPrice() {
return getPrice() * getDiscount();
}
}
public class Client {
public static void main(String[] args) {
Fruit fruit = new Apple(1, "苹果", 20.0);
fruit.setDiscount(0.8);
printInfo(fruit);
}
public static void printInfo(Fruit fruit) {
System.out.println("Id: " + fruit.getId() + "; 名称: " + fruit.getName() + "; 原价格: " + fruit.getPrice());
System.out.println("促销价格: " + fruit.getPromotionPrice());
}
}
public abstract class Fruit {
private Integer id;
private String name;
private Double price;
private Double discount;
public Fruit(Integer id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Double getDiscount() {
return discount;
}
public void setDiscount(Double discount) {
this.discount = discount;
}
public abstract Double getPromotionPrice();
}
public class Banana extends Fruit {
public Banana(Integer id, String name, Double price) {
super(id, name, price);
}
public Double getPromotionPrice() {
return getPrice() * getDiscount();
}
}
3.组合模式
public class DemoLab {
public static void main(String[] args) {
// 构建公司的组织结构
Department rootDepartment = new Department(1001);
Department saleDepartment = new Department(100101);
Department financialDepartment = new Department(100102);
Employee zhangsan = new Employee(1, 10000);
Employee lisi = new Employee(2, 5000);
Employee wangwu = new Employee(3, 3000);
Employee zhaoliu = new Employee(4, 4000);
rootDepartment.addSubNode(saleDepartment);
rootDepartment.addSubNode(financialDepartment);
rootDepartment.addSubNode(zhangsan);
saleDepartment.addSubNode(lisi);
financialDepartment.addSubNode(wangwu);
financialDepartment.addSubNode(zhaoliu);
// 计算财务部的薪资成本
System.out.println("部门:" + financialDepartment.id);
System.out.println("薪资成本:" + financialDepartment.calculateSalary());
}
}
import java.util.ArrayList;
import java.util.List;
public class Department extends HumanResource {
private List subNodes = new ArrayList<>();
public Department(long id) {
super(id);
}
@Override
public double calculateSalary() {
double totalSalary = 0;
for (HumanResource hr : subNodes) {
totalSalary += hr.calculateSalary();
}
this.salary = totalSalary;
return totalSalary;
}
public void addSubNode(HumanResource hr) {
subNodes.add(hr);
}
}
public class Employee extends HumanResource {
public Employee(long id, double salary) {
super(id);
this.salary = salary;
}
@Override
public double calculateSalary() {
return salary;
}
}
public abstract class HumanResource {
protected long id;
protected double salary;
public HumanResource(long id) {
this.id = id;
}
public long getId() {
return id;
}
public abstract double calculateSalary();
}
4 工厂方法
1
请根据下面的要求用工厂方法模式进行设计。
设计一个程序用于读取多种不同格式的图片文件,对每种不同的图片格式都设计一个图片读取器(ImageReader),例如:PNG图片读取器(PngReader);JPG图片读取器(JpgReader)。图片读取器对象通过图片读取器工厂(ImageReaderFactory)创建,ImageReaderFactory是一个抽象类,用于定义创建图片读取器的工厂方法,其具体子类(PngReaderFactory,JpgReaderFactory)用于创建具体的图片读取器对象。
【提示:图片读取器是“产品”,图片读取器工厂是“工厂”,它们均有抽象的和具体的;工厂和产品是对应的,即什么工厂就生产什么产品。】.
public interface ImageReaderFactory {
public ImageReader createImageReader();
}
public class JpgReader implements ImageReader {
public void readImage() {
System.out.println("正在阅读JPG格式的图片......");
}
}
public class JpgReaderFactory implements ImageReaderFactory {
public ImageReader createImageReader() {
System.out.println("JPG图片阅读器工厂生产JPG图片阅读器......完成");
return new JpgReader();
}
}
public class PngReader implements ImageReader {
public void readImage() {
System.out.println("正在阅读PNG格式的图片......");
}
}
public class PngReaderFactory implements ImageReaderFactory {
public ImageReader createImageReader() {
System.out.println("PNG图片阅读器工厂生产PNG图片阅读器......完成");
return new PngReader();
}
}
public class ClientFM {
public static void main(String args[]) {
ImageReader reader;
ImageReaderFactory factory;
factory = new PngReaderFactory();
reader = factory.createImageReader();
reader.readImage();
}
}
5.代码重构
function printOwing(invoice) {
printBanner()
const outstanding = caculateOutstanding(invoice)
recordDueDate(invoice)
printDetails(invoice, outstanding)
}
function printBanner() {
console.log("***********************")
console.log("**** Customer Owes ****")
console.log("***********************")
}
function caculateOutstanding(invoice) {
let result = 0;
for (const o of invoice.orders) {
result += o.amount
}
return result
}
function recordDueDate(invoice) {
const today = Clock.today;
invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30)
}
function printDetails(invoice, outstanding) {
console.log(`name: ${invoice.customer}`)
console.log(`amount: ${outstanding}`)
console.log(`name: ${invoice.dueDate.toLocaleDateString()}`)
}



