views:

43

answers:

4

Hi Experts,

I know that my this question may sound pathetic to you but as a beginner in .NET, it means a lot to me.

I just want to know that how can I utilize the concept of Abstract class, virtual class etc. in my shopping cart website. I have read the tutorial out there on internet and I saw some examples too, but those examples are so general that they dosen't fit into real world scenerio like I am searching for a shopping website. Same questions again and again comes to my mind that why to made a class only to give the declaration of methods and property.

I understand that most of you are Gurus of .NET and have ample knowlesge in it but if you could just help me out in thinking the logic behind this i'll be very greatfull.

If possible, please recommend me nice books for asp.net design patterns, from which I can learn design patterns.

Thanks in advance

+2  A: 

Your one stop resource and guide is Head First - OOAD.

alt text

this. __curious_geek
Is this specific to asp.net or .NET ?
Rahul
@Rahul - you can't learn design patterns just for one language
Graphain
+1  A: 

If you can't see why to use them then don't for now. Never use a design pattern just for the sake of it.

As for their purpose however, imagine that you want to allow different types of products, but you never have something that is just a "Product" - it's always something specific like a "Book" or "Car". In that case you can put the common properties in an abstract Product class like this:

public abstract class Product
{
    /* Abstract Price allows Car/Book to apply their own category discounts */
    public abstract decimal Price { get; }

    public string Title { get; }

    public void AddReview(int userId, string reviewText)
    {
        /* Add review */
    }

    public abstract List<Product> Recommendations(int userId);
}

which your Book and Car classes can then extend.

Graphain
+1  A: 

Here is a good design patterns book with examples in C#.

C# 3.0 Design Patterns

alt text

Wallace Breza
A: 

Its not ture that for every desing you have to use Abstarct class or define virtual methods.

Basically virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class, and Abstarct keyword enables you to create classes and class members solely for the purpose of inheritance—to define features of derived, non-abstract classes

In your case you can have an abstract class called CartItem which works as a base class for all the types of items you are supposed to support in you cart. All types of items will inherit the CartItem class. You can’t directely create the instance of CartItem class but you can use this as a reference to achieve the Polymorphism.

You can define some concrete methods/properties like ItemId, ItemName, Price etc in the CartItem class which are common to all the types of items and you can also define some of the methods a virtual for which you have a default implementation but the child classes can override the implementation.

Prakash Kalakoti