2012-09-24 4 views
0

안녕하세요 저는 제품이라는 클래스가 있습니다. 제 문제는 제품 목록, 제품 자체 및 제품을 데이터베이스에 삽입하는 등 다양한 방법으로 제품을 처리한다는 것입니다. 각각 다른 속성을 처리합니다.제품 클래스 디자인

예를 들어 제품을 페이지에 표시하는 것은 이름, discription, id, 가격, 브랜드 이름, 카테고리, 이미지로 구성되지만 제품 목록은 이름, 미리보기 이미지 만 표시합니다. 각 제품에는 자체적 인 방법이 있습니다. 예를 들어 상위 5 개 제품을 얻고 다른 제품은 하나의 제품 만 표시합니다.

내 질문에 대한이 클래스를 만드는 방법에 갈 것입니다, 각 제품 변형에 대해 다른 클래스를 만들거나 모든 메서드 및 속성을 구성하는 클래스를 만드는 따라서 매우 부피가 큰 클래스로 구성됩니다.

어떤 도움이 필요합니까?

답변

2
  • Product라는 추상 클래스로 시작하십시오.
  • 이 클래스의 일반적인 특성/속성/일반적인 방법을 모두 보유하고 있습니다.
  • 이 추상 클래스에서 새 제품 유형을 파생시킵니다.
  • 가상 클래스의 속성/메소드를 가상으로 만들므로 제품 유형을 파생하면 제품 유형에 따라 다른 동작이 발생할 수 있습니다.
  • 파생 된 제품 유형에 특정 동작을 명시 적으로 표시해야하는 경우 참조하십시오. 추상 클래스에 abstract와 같은 메소드를 선언하십시오. 파생 클래스가 해당 동작을 구현할 책임이 있습니다.

    추상 클래스 제품 {

    // 멤버 필드 는 // 방법

    }

    product1:product{ 
    

    // 제품 별 구현 } 특정

    product2:product{ 
    

    // 제품 구현 }

1

모든 속성을 가진 단일 Product 클래스를 사용하고 주어진 상황에서 필요한 속성 (데이터베이스 필드) 만 표시하면됩니다. 예를 들어 장바구니 품목으로 제품을 만들 수 있습니다.

public class ShoppingCartItem 
{ 
    private int productID; 
    private string productCategory; 
    private string subCategory; 
    private string productName; 
    private string productDescription; 
    private decimal productPrice; 
    private double productWeight; 
    private int units; 

    public int ProductID 
    { 
     get { return productID; } 
    } 
    public string ProductCategory 
    { 
     get { return productCategory; } 
    } 
    public string SubCategory 
    { 
     get { return subCategory; } 
    } 
    public string ProductName 
    { 
     get { return productName; } 
    } 
    public string ProductDescription 
    { 
     get { return productDescription; } 
    } 
    public decimal ProductPrice 
    { 
     get { return productPrice; } 
    } 
    public double ProductWeight 
    { 
     get { return productWeight; } 
     set { productWeight = value; } 
    } 
    public int Units 
    { 
     get { return units; } 
     set { units = value; } 
    } 
    public decimal Total 
    { 
     get { return Units * ProductPrice; } 
    } 
    public ShoppingCartItem(int productID, string farm, string productCategory, 
     string subCategory, string productName, string productDescription, 
      decimal productPrice, double productWeight, int units) 
    { 
     this.productID = productID; 
     this.productCategory = productCategory; 
     this.subCategory = productCategory; 
     this.productName = productName; 
     this.productDescription = productDescription; 
     this.productPrice = productPrice; 
     this.productWeight = productWeight; 
     this.units = units; 
    } 
} 

[Serializable()] 
public class ShoppingCart : CollectionBase 
{ 
    public ShoppingCartItem this[int index] 
    { 
     get { return ((ShoppingCartItem)List[index]); } 
     set { List[index] = value; } 
    } 
    public int Add(ShoppingCartItem value) 
    { 
     return (List.Add(value)); 
    } 
    public int IndexOf(ShoppingCartItem value) 
    { 
     return (List.IndexOf(value)); 
    } 
    public void Insert(int index, ShoppingCartItem value) 
    { 
     List.Insert(index, value); 
    } 
    public void Remove(ShoppingCartItem value) 
    { 
     List.Remove(value); 
    } 
    public bool Contains(ShoppingCartItem value) 
    { 
     return (List.Contains(value)); 
    } 
} 
-1

응용 프로그램에서 필요로하는 모든 속성/방법으로 하나의 클래스를 디자인하십시오.