本文介绍了OOAD设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子: tblCustomer tblProduct

tblCustomer:
    Id: Integer, auto-increament
    Name: Varchar(30)
    ....

tblProduct
    Id: Integer, auto-increament
    Name: Varchar(50)
    customerId: Integer
    ....

两个类:客户产品

public class Product
{
    private int id;
    private int name;
    /* Other stuffs */
}

public class Customer
{
    private int id;
    private String name;
    private String phoneNumber;

    /* get-set and others stuffs */

    public static boolean add(Customer cus) {
        /* This is for insert a customer to tblCustomer */
    }

    public boolean addProduct(Product pd) {
        /* This is for insert a product to tblProduct with current customer Id */
    }
}

当客户注册账户时,它会致电:

When customer register account, it call:

Customer cus = new Customer(/* ... */);
Customer.add(cus);

当顾客购买产品时:

Product pd = new Product(/* ... */);
currentCustomer.addProduct(pd);

但我的老师说OOAD(甚至是OOP)不正确,因为 Customer.addProduct tblProduct 表上运行,是不是?这个案例的优秀设计是什么?

But my teacher said it not correct in OOAD (and even OOP) because Customer.addProduct is operate on tblProduct table, is it right? What is good design for this case?

**更新:**
当客户购买产品时,产品还没有预先定义,商店将把它交付给客户,所以两个相同的产品很少发生,所以 tblCustomerProduct 需要吗?

** Update: **Product haven't a pre-defined yet, when a customer buy a product, the store will make it and delivery to customer, so two same products is rare happen, so is tblCustomerProduct need?

推荐答案

添加一个DAO层,它将包含方法的逻辑部分 save delete 更新等。

Add a DAO tier that will contain the logical part of the methods save, delete, update, etc.

以下是我通常做的事情:

Here is how I usually do:


  • basepackage.domain :包含您的所有实体(仅限数据,无逻辑部分 - 在您的情况下产品客户

  • basepackage.dao :包含所有DAO,仅用于访问数据,基本上每个实体一个,每个实体包含 findAll等方法( ):列表< E> findOne(K id):E save(E e):void 等。

  • basepackage.service :包含您的所有服务,应用程序的逻辑部分。这些服务是唯一调用DAO的服务。

  • basepackage.presentation (或 basepackage.web for webapp):包含HMI / web services / ...实现。

  • basepackage.domain: contains all your entities (data only, no logical part - in your case Product and Customer)
  • basepackage.dao: contains all your DAOs, only used to access the data, basically one per entity, each one containing methods such as findAll() : List<E>, findOne(K id) : E, save(E e) : void, etc.
  • basepackage.service: contains all your services, the logical part of the app. The services are the only ones that are calling the DAOs.
  • basepackage.presentation (or basepackage.web for a webapp): contains the HMI/web services/... implementation.

这篇关于OOAD设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 10:31