java - UML 2.0 序列图深度

标签 java uml sequence diagram sequence-diagram

我正在尝试完成我的中期作业,即为一个简单的 java 程序创建一个序列图。然而,我遇到了一个问题,我无法决定将哪个元素放入图表中以及忽略哪个元素。如果可以帮助回答问题,我将发布 JAVA 代码。

public class TestPOS {
public static void main(String[] args) {
    // POST 객체를 준비
    Store store = new Store();
    ProductCatalog catalog = new ProductCatalog();
    catalog.addSpec(1, new ProductSpec(1, "pencil", 1000));
    catalog.addSpec(2, new ProductSpec(2, "eraser", 500));
    catalog.addSpec(3, new ProductSpec(3, "fountain pen", 50000));
    POST post = new POST(store, catalog);

    // 첫 번째 판매
    post.enterItem(1, 12);
    post.enterItem(2, 4);
    post.enterItem(3, 1);

    post.makePayment();

    post.endSale();

    // 두 번째 판매
    post.enterItem(1, 2);
    post.enterItem(2, 1);

    post.makePayment();

    post.endSale();

    // 출력을 보여주어 이해를 돕기위한 코드
    for (Sale sale : store.completedSales) {
        System.out.println(sale.getDate());
        sale.printLineItems();
        System.out.println("total = " + sale.getTotal());
    }
  }
}

这是调用 EnterItem()、makePayment() 和 endSale() 的主要部分。作业是为上述三个函数创建序列图。我将在下面发布每个类(class)。

-----------------------------------------------------------
import java.util.Date;
public class POST {
    private Store store;
    private ProductCatalog catalog;
    private Sale sale = null;

    public POST(Store store, ProductCatalog catalog) {
        this.store = store;
        this.catalog = catalog;
    }

    public void enterItem(int upc, int qty) {
        if (sale == null) {
            Date date = new Date(System.currentTimeMillis());
            sale = new Sale(date);
        }
        ProductSpec s = catalog.spec(upc);
        sale.makeLineItem(s, qty);
    }

    public void makePayment() {
        if (sale != null) sale.makePayment();
    }

    public void endSale() {
        store.addCompleteSale(sale);
        sale = null;
    }
}
-----------------------------------------------------------
import java.util.ArrayList;
public class Store {
    protected ArrayList<Sale> completedSales = null;

    public Store() {
        completedSales = new ArrayList<Sale>();
    }

    public void addCompleteSale(Sale sale) {
        completedSales.add(sale);
    }
}
-----------------------------------------------------------
import java.util.ArrayList;
import java.util.Date;
public class Sale {
    private Date date;
    private ArrayList<SalesLineItem> lineItem = null;
    private Payment payment = null;

    public Sale(Date date)
    {
        this.date = date;
        lineItem = new ArrayList<SalesLineItem>();
    }

    public void makeLineItem(ProductSpec s, int qty) {
        SalesLineItem item = new SalesLineItem(s, qty);
        lineItem.add(item);
    }

    public int getTotal() {
        int total = 0;  
        for (SalesLineItem item : lineItem) {
            total += item.getSubTotal();
        }
        return total;
    }

    public void makePayment() {
        int total = this.getTotal();
        payment = new Payment(total);   
    }

    // 출력을 보여주어 이해를 돕기위한 메소드, 클래스 다이어그램에 반영하지 않음
    public Date getDate() {
        return date;
    }

    // 출력을 보여주어 이해를 돕기위한 메소드, 클래스 다이어그램에 반영하지 않음
    public void printLineItems() {
        for (SalesLineItem item : lineItem) {
            System.out.println("upc : " + item.getItemUpc() +", name : " + item.getItemName() + ", price : "
                    + item.getItemPrice() + ", quantity : " + item.getQuantity());
        }
    }
}
-----------------------------------------------------------
public class Payment {
    private int amount;

    public Payment(int amount) {
        this.amount = amount;
    }

    // 출력을 보여주어 이해를 돕기위한 메소드, 클래스 다이어그램에 반영하지 않음
    public int getAmount() {
        return amount;
    }
}
-----------------------------------------------------------
public class SalesLineItem  {
    private int quantity;
    private ProductSpec spec;

    public SalesLineItem(ProductSpec spec, int quantity) {
        this.spec = spec;
        this.quantity = quantity;
    }

    public int getSubTotal() {
        int price = spec.getPrice();
        return price * quantity;
    }

    // 출력을 보여주어 이해를 돕기위한 메소드, 클래스 다이어그램에 반영하지 않음
    public int getItemUpc() {
        return spec.getUpc();
    }

    // 출력을 보여주어 이해를 돕기위한 메소드, 클래스 다이어그램에 반영하지 않음
    public String getItemName() {
        return spec.getName();
    }

    // 출력을 보여주어 이해를 돕기위한 메소드, 클래스 다이어그램에 반영하지 않음
    public int getItemPrice() {
        return spec.getPrice();
    }

    // 출력을 보여주어 이해를 돕기위한 메소드, 클래스 다이어그램에 반영하지 않음
    public int getQuantity() {
        return quantity;
    }
}
-----------------------------------------------------------
import java.util.HashMap;
public  class ProductCatalog  {
    private HashMap<Integer, ProductSpec> specTable = new HashMap<Integer, ProductSpec>();

    public void addSpec(int upc, ProductSpec spec) {
        specTable.put(upc, spec);
    }

    public ProductSpec spec(int upc) {
        return specTable.get(upc);
    }
}
-----------------------------------------------------------
public class ProductSpec  {
    private int upc;
    private String name;
    private int price;

    public ProductSpec(int upc, String name, int price) {
        this.upc = upc;
        this.name = name;
        this.price = price;
    }

    public int getPrice() {
        return price;
    }

    // 출력을 보여주어 이해를 돕기위한 메소드, 클래스 다이어그램에 반영하지 않음
    public int getUpc() {
        return upc;
    }

    // 출력을 보여주어 이해를 돕기위한 메소드, 클래스 다이어그램에 반영하지 않음
    public String getName() {
        return name;
    }
}

不要介意代码中的韩语,它们只是为了一些描述。现在下图是我画的图,不管正确与否。

enterItem sequence diagram

主要问题是我不知道是否应该绘制终止过程中使用的每个元素、交互和实例。我知道我听起来有点杂乱无章,但请有人帮助我解决我的问题。

最佳答案

我没有与你的代码交叉检查绘图是否正确。请注意,创建 ProductSpec 直接返回到 POST。这可能是您上下文中的合法缩写,但它可能不反射(reflect)代码。

无论如何,你在 SD 中放入的内容应该澄清特定的上下文,而不是模糊它。因此,将所有内容放入一个 SD 中绝不是一个好主意。相反,创建一个粗略的概述来突出显示主要流程。在 POS 系统中,这可能首先要做两件事:整个内容的初始化和目录中文章的输入。意味着您将在此处创建两个 SD。您也不应该试图“以图形方式编程”并大量使用片段。将它们留给重要的路径决策等,而不是为您在代码中找到的每个 ifcase 保留它们。就 SD 而言,少用往往比多用要好。

注意:我不知道你的老师在这里的意图是什么。这可能与我的陈述相矛盾,他只是想让你创建一个(无用的)壁纸。

关于java - UML 2.0 序列图深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46628708/

相关文章:

java - EJB 与 JPA Controller ?

java - 如何用java递归调用方法?

java - 如何使用特殊字符在字符串中公开

uml - 逆向工程 : representation of a C# Dictionary?

mysql - 具有两个外键的表之间的关系

php - 识别选中的复选框序列然后保存到 mysql 数据库

用于限制序列的方便的 FLAG

java - 是否推荐使用 StringUtils.EMPTY?

UML 图实际上很漂亮吗?

python - 使用Python的itertools生成二维表索引