Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a class diagram that is like this:

< Order > (1) CAN HAVE (M) < products >

But therefore Order has the following:

Order_Id
Customer_Id
Order_date_day
Order_date_month
Order_date_yeah

But I do not know how it would handle the Products? Because, I couldn't have ProductID because that would mean that each item in this class would have to have a separate instance for each product (E.g. someone ordered 100 products, but only placed 1 order).

Could I have an Product object in class Order? If so, how do I represent that in UML?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

The one-to-many relationship between Order and Product translates as a list of Products. For example in Java Order would have a member of type List<Product>.

But I recommend having an OrderDetail class instead because you will soon find that you have to have and amount or quantity of the product, and it doesn't belong in either Order or Product.

EDIT:

In UML Order would have a "composition" which is a line with a black diamond shape on the Order side and a ">" (not a triangle) on the OrderDetail side.

If you decide to use Product instead of OrderDetail then you should have an "agregation" which has a white diamond shape.

The difference is that a composition means OrderDetail has no meaning outside an Order whereas agregation means that Products can exist by themselves outside an Order.

share|improve this answer
    
Hey thanks. BUT how do you represent List<Product> in UML? –  Phorce Dec 3 '12 at 18:24
    
@Phorce I added an explanation in the answer. –  user61852 Dec 3 '12 at 18:42
    
So it's not an association, more of a "Composition" ok that makes sense now, thanks :)! –  Phorce Dec 3 '12 at 18:51
    
@Phorce Added something more the answer. –  user61852 Dec 3 '12 at 19:10
add comment

Normally, you have two classes in your diagram: Order and Product.

As an order usually means that the customer has ordered 1 or more (1..*) products, you model this by creating an association from the class Order to the class Product.

This coincides with adding a collection of Product instances to your Order instances, i.e. most code generators will give you a Order.getProducts() method or something similar. In fact, you can add such an association explictly as an attribute to the Order class. Some tools hide this fact, others allow you to see this distinction clearly (f.ex. Papyrus).

share|improve this answer
add comment

I do not know how it would handle the Products

Assuming that you are modeling this to generate RDBMS tables, you would generally use 2 separate classes. The 'child' class would have the Product attributes plus the Product_Id acting as a 'foreign key'. The other class is your Order class.

The diagram in the answer of @Esther Fan - MSFT in UML-arrows-pointers-explanation shows more than what you want in fact, however, on a class level its what you want. Just replace the word "Order_Item" by the word "Product" in the class diagram and ignore the other classes not related to your case.

The MSDN link provided within that answer may also be useful for you.

An important business rule you need to consider is whether an Order occurence is meaningful without Products or not. The diagram in the link above assumes a composition association between Order and Product (Black diamond), this may or may not be your case, so please check.

It is also common to have a class for a requesting entity and a product reference entity shown but you did not ask for those. As an example, have a look at: Simple Class Diagram.

I couldn't have ProductID because that would mean that each item in this class would have to have a separate instance for each product (E.g. someone ordered 100 products, but only placed 1 order).

The above situation is not a problem. Each of the products would use the FK column (Order_Id). The value in that column would be the same for the 100 products.

Could I have an Product object in class Order?

Yes, but this will not replace the 1-M pattern existing here. See a UML representation sample here: Inner-Internal Class.

Note that in cases, you need programming data structures for representing collections of data in your code that is different from the data structure you use to represent the same information in the database if you use RDBMS from an OO application.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.