James Andrew Smith
James Andrew Smith

Reputation: 1566

Product Class Design

I have a class called product, my problem is that I handle products in various ways, either through a list of products, the product itself, and inserting a product into the database. Each handles different properties.

For example, displaying the product on a page will consist of name, description, id, price, brand name, category, image but a list of products would just display just name, thumbnail. Each will have its own methods, for example, one would get top 5 products but the other only displays one product.

My question is how would go about creating classes for this, do I create a different class for each product variation, or create a class consisting of every method and properties thus would consist of a very bulky class.

Upvotes: 2

Views: 7133

Answers (4)

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

  • Start with an Abstract class called Product.

  • Have all of your common traits/properties/common methods of products in this class.

  • Derive new product types from this abstract class.

  • make properties/methods in the abstract class virtual, so deriving product types can exhibit different behavior depending on the product type.

  • See if you require the derived product types to explicitly exhibit a specific behavior. declare such methods as abstract in your abstract class. so the derived class is responsible for implementing that behavior.

    abstract class product{

    //member fields //methods

    }

     product1:product{
        //product specific implementation
     }
    
     product2:product{
         //product specific implementation
     }
    

Upvotes: 3

Nitin
Nitin

Reputation: 35

Each scenario calls for different model. If you are persisting a product then it will be ProductDto or PersistableProduct. Or you can qualify that with namespace like Shopping.Dto.Product

If you are having product for view then I would think of ProductViewModel. If you are using ASP.NET MVC, the model directory will contain a class by name Product with only limited properties for page display.

Uncle Bob advocates DTO and Business Objects as different things in his blog.
http://blog.cleancoder.com/uncle-bob/2019/06/16/ObjectsAndDataStructures.html We also need to pay attention to design while choosing the classes.

Upvotes: 1

george.zakaryan
george.zakaryan

Reputation: 980

Design one class with all the properties/methods you will ever need in your application.

Upvotes: -2

IrishChieftain
IrishChieftain

Reputation: 15253

Use a single Product class with all properties and simply display the properties (database fields) you need in a given situation. For example, you could create the product as a shopping cart item:

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));
    }
}

Upvotes: 2

Related Questions