OO Design Question

K

Kay

Hello,


My question isn't specific to .Net but as .Net is object oriented, I would
be interested if someone could let me know if there is a standard way of
implementing what I describe below in OO or if it is covered by a design
pattern. I hope someone can help me as I have been looking for a solution to
this question for a while.

For example:

If I have a number of Customers and each Customer can have a number of
Orders associated with them, each Order can have a number of Products
associated with it. To implement this in OO, I would have the following
classes :


Customer,
Order and
Product


(1) If I want to retrieve all the Orders for a customer, should I have an
arraylist of Order objects within the Customer class and similarly have an
arraylist of Product objects within the Order Class?


When I instantiate the Customer class, should I get all the Orders
associated with the Customer and fill the arraylist of Order objects? I.e.
at the time of instantiating a Customer getting all the Orders and Products
for the Customer or should I provide a method such as GetAllOrders in the
Customer class that is called when you require the Orders associated with
the Customer.


(2) If I needed to display in a dropdown list, all the Customer Names, is
this another class or how do I implement it i.e. I need a method that
returns a list of Client names, to what class would this belong to?. I asume
it is not good practice to return a dataset or datareader to the code behind
of a web page. This question applies to lists of anything, for e.g. I might
want to display in a dropdown list, all the order numbers associated with a
client.

(3) Another common task might be to sum the total of each order, in what
class would this be implemented.


Your help would be much appreciated.

Thanks in advance,
Kay.
 
G

Guest

Hi Kay,

Although I can't really help you with some of your specific questions I have
some experience of OR (Object/Relational) mapping. There is a good example
of how relational schemas can be translated into object mappings :
http://www.ormapper.net/Examples/?ID=IntroSchema . I hope at least the
examples can point you in the right direction.

BTW We have used this OR Mapper in a recent project and I can highly
recommend it.
 
J

Jako Menkveld

Kay said:
Hello,


My question isn't specific to .Net but as .Net is object oriented, I would
be interested if someone could let me know if there is a standard way of
implementing what I describe below in OO or if it is covered by a design
pattern. I hope someone can help me as I have been looking for a solution
to
this question for a while.

For example:

If I have a number of Customers and each Customer can have a number of
Orders associated with them, each Order can have a number of Products
associated with it. To implement this in OO, I would have the following
classes :


Customer,
Order and
Product

These are the right classes.
(1) If I want to retrieve all the Orders for a customer, should I have an
arraylist of Order objects within the Customer class and similarly have an
arraylist of Product objects within the Order Class?


When I instantiate the Customer class, should I get all the Orders
associated with the Customer and fill the arraylist of Order objects? I.e.
at the time of instantiating a Customer getting all the Orders and
Products
for the Customer or should I provide a method such as GetAllOrders in the
Customer class that is called when you require the Orders associated with
the Customer.

I would not load these at time of instantiating, you never know how many
orders there are, thus this can take a while. You should know when the
order are required (when a user selects this Customer from your drop-down
for instance). I would implement that as follows (this is c# syntax):

public class Customer
{
private ArrayList alOrders = null;

// Now add a property to your class
public ArrayList Orders
{
get
{
if (alOrder == null)
{
alOrders = // LOAD THE ORDERS
}
return alOrders;
}
}
}

This would ensure that when you access the Orders property the first time,
the list will be populated.
(2) If I needed to display in a dropdown list, all the Customer Names, is
this another class or how do I implement it i.e. I need a method that
returns a list of Client names, to what class would this belong to?. I
asume
it is not good practice to return a dataset or datareader to the code
behind
of a web page. This question applies to lists of anything, for e.g. I
might
want to display in a dropdown list, all the order numbers associated with
a
client.

For this you'll need another class, i.e. some sort of Customer Collection.
This class will hold a list of Customer objects (you could use an arraylist
again). To get the list of Customer Names, you can scroll through the list
of customers and return a list of their names.

As for the second part of the question, regarding a list of Orders per
Customer; this can be implemented in the Customer class.

(3) Another common task might be to sum the total of each order, in what
class would this be implemented.

You should add a property or method to your Order class that returns it's
total. Then in the Customer class you can have a method that scrolls
through all the Orders and sums their total.

Your help would be much appreciated.

Thanks in advance,
Kay.


Hope this helps!
 
C

Cor Ligthert

Kay,

I would not do it the way you do.

I always ask. To what does something belong

A Client belongs to the company
Does an order belong to the Client, in my opinion not, it is related to the
Client and belongs to the company. Is the product(row) from the client, no
it is related to the order.

Client
has a collection of references too order objects
Orders
has a reference too a Client
has a collection (class) Ordered Productrows with a reference in it to
the Product object
Product
can have a collection of references too productrows

Just my thought,

Cor
 
E

Elliot Rodriguez

I agree with Cor.

The big benefit to using collections is you can modify your collection
member class (Order, a member of the Orders collection) with minimal impact
on the rest of your object hierarchy. Your Orders collection object will
always consist of Order objects.

You can then take it a step further, if you want to derive different Order
types (say, a CustomerOrder versus a CorporateOrder), and still preserve the
integrity of your collection members (since CustomerOrder and CorporateOrder
derive from base class Order).
 
K

Kay

Thanks everyone for your response.



Cor,



Maybe I have misunderstood what you have said below, I was suggesting 3
classes, Customer, Order and Product.



The customer class has a collection of references to Order objects as you
said below.



The Order class has a collection of references to Product objects and as
you said below has a reference to a Client. I am not sure what is meant by
"Orders has a collection (class) Ordered Productrows with a reference in it
to the Product object", is this different to, Orders has a collection of
references to Product objects?



In my example I was thinking that a Product Class would just contain
information about the product such as name of Product, description etc.

I was thinging of a very simple situation i.e. a database with a Customer
Table no foreign keys, an Order table with a foreign key Customer Id and
Product Id and a Product table with no foreign keys.



Thanks,

Kay.
 
C

Cor Ligthert

Kay,

I did assume that an order could contains more products and with that maybe
special information for that product about the delivery.

The base of that information is than the product object of course.

Said in another way.

An order has an order description and orderrows, what I named productrows.
(What is not everywhere necessary).

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top