2013-10-17 48 views
0

我有兩個類,第一個類名爲Product,基本上保存從數據庫加載的所有可用產品及其屬性。另一類叫做OrderProduct,延伸Product並且有另外兩個屬性:數量和日期。 OrderProduct用作保存當前訂單信息的臨時對象。在訂單結束時,訂單對象被保存到數據庫中。我的問題:我是否應該遵循我目前的結構或按照這種方式進行設計,以使OrderProduct不會擴展Product類,並且只會持有id來訪問特定產品並具有以下兩個額外屬性:數量和日期?或者有更好的方法如何做到這一點?如何以正確的方式存儲域對象

產品:

package domainLayer; 

public class Product { 

    private int id; 
    private String name; 
    private String description; 
    private String type; 
    private int price; 
    private String status; 

    public Product(int id, String name, int price, String type, String description, String status) { 
     this.id = id; 
     this.name = name; 
     this.description = description; 
     this.price = price; 
     this.status = status; 
    } 

    public Product(Product product){ 
     this.id = product.getId(); 
     this.name = product.getName(); 
     this.description = product.getDescription(); 
     this.price = product.getPrice(); 
     this.status = product.getStatus(); 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setPrice(int price) { 
     this.price = price; 
    } 

    public int getPrice() { 
     return price; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    public String getStatus() { 
     return status; 
    } 
} 

OrderProduct:

package domainLayer; 

import java.util.Date; 

public class OrderProduct extends Product { 

    private int quantity; 
    private Date date; 

    public OrderProduct(int id, String name, int price, String type, String description, String status, int quantity, Date date) { 
     super(id, name, price, type, description, status); 
     this.quantity = quantity; 
     this.date = date; 
    } 

    public OrderProduct(Product product, int quantity, Date date){ 
     super(product); 
     this.quantity = quantity; 
     this.date = date; 
    } 

    public void setQuantity(int quantity) { 
     this.quantity = quantity; 
    } 

    public int getQuantity() { 
     return quantity; 
    } 

    public void addQuantity(int quantity){ 
     this.quantity += quantity; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

    public Date getDate() { 
     return date; 
    } 
} 

回答

0

我不會那麼做的。 OrderProduct不是真正的子類,因此它不應該是Product的子類。

取而代之的是用產品,數量,日期等獲取產品的OrderProduct。

相關問題