今天嘗試使用visio畫了一個序列圖。java
話很少說,先打開visio,新建一個UML序列。git
先新建幾個對象生命線socket
加入幾個激活測試
然後是消息,實線表示請求,虛線表示響應。this
固然若是不影響理解,能夠不用每一個請求都配上一個響應。3d
此UML例子是在《UML精粹:標準對象建模語言簡明指南》中拿過來的,爲了更好的理解這個UML,下面使用java來實現此UML。code
ps,源碼上傳到碼雲了。https://gitee.com/xixiw777/sequenceDiagramhttps://gitee.com/xixiw777/sequenceDiagram對象
package com.simon.uml.sequenceDiagram; /** * Created by IntelliJ IDEA. * * @author: Simon * @date: 2019-05-09 * @time: 14:50 * @description:產品 */ public class Product { private String name; private double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public double getPrice(int number) { double orderLinePrice = price * number; System.out.println("product name:" + getName() + "\tprice:" + getPrice() + "\tnumber:" + number + "\torder line price:" + orderLinePrice ); return orderLinePrice; } public void setPrice(double price) { this.price = price; } }
package com.simon.uml.sequenceDiagram; /** * Created by IntelliJ IDEA. * * @author: Simon * @date: 2019-05-09 * @time: 14:51 * @description:客戶 */ public class Customer { private double discount; public double getDiscountedValue(Order order) { return discount * order.getBaseValue(); } public double getDiscount() { return discount; } public void setDiscount(double discount) { this.discount = discount; } }
package com.simon.uml.sequenceDiagram; import java.util.List; /** * Created by IntelliJ IDEA. * * @author: Simon * @date: 2019-05-09 * @time: 14:50 * @description:訂單 */ public class Order { private double baseValue; private Customer customer; private List<OrderLine> orderLineList; public double calculatePrice() { double sumOrderLinePrice = 0; for (OrderLine orderLine : orderLineList) { double orderLinePrice = orderLine.calculatePrice(); sumOrderLinePrice += orderLinePrice; } this.setBaseValue(sumOrderLinePrice); System.out.println("order base price:" + sumOrderLinePrice); double discountedValue = getCustomer().getDiscountedValue(this); System.out.println("order discounted price:" + discountedValue); return discountedValue; } public double getBaseValue() { return baseValue; } public void setBaseValue(double baseValue) { this.baseValue = baseValue; } public List<OrderLine> getOrderLineList() { return orderLineList; } public void setOrderLineList(List<OrderLine> orderLineList) { this.orderLineList = orderLineList; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
order lineblog
package com.simon.uml.sequenceDiagram; /** * Created by IntelliJ IDEA. * * @author: Simon * @date: 2019-05-09 * @time: 14:50 * @description:訂單明細 */ public class OrderLine { private int number; private Product product; public OrderLine(int number, Product product) { this.number = number; this.product = product; } public double calculatePrice() { return product.getPrice(number); } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } }
order的測試用例ip
package com.simon.uml.sequenceDiagram; import org.junit.Assert; import java.util.ArrayList; import java.util.List; /** * Created by IntelliJ IDEA. * * @author: Simon * @date: 2019-05-09 * @time: 15:23 * @description: */ public class OrderTest { @org.junit.Test public void calculatePrice() { //這個商品是鞋子,123塊錢一雙 Product productShoes = new Product(); productShoes.setName("shoes"); productShoes.setPrice(123); //這個商品是襪子,8塊錢一雙 Product productSocket = new Product(); productSocket.setName("socket"); productSocket.setPrice(8); //這是一個老客戶,打七折 Customer customer = new Customer(); customer.setDiscount(0.7); //這個客戶下了一個單 Order order = new Order(); order.setCustomer(customer); //買了一雙鞋子,兩雙襪子 OrderLine orderLineShoes = new OrderLine(1, productShoes); OrderLine orderLineSocket = new OrderLine(2, productSocket); List<OrderLine> orderLineList = new ArrayList<OrderLine>(); orderLineList.add(orderLineShoes); orderLineList.add(orderLineSocket); order.setOrderLineList(orderLineList); //算一算總共多少錢 double priceActual = order.calculatePrice(); Assert.assertEquals("97.3", String.valueOf(priceActual)); } }
product name:shoes price:123.0 number:1 order line price:123.0 product name:socket price:8.0 number:2 order line price:16.0 order base price:139.0 order discounted price:97.3