wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Associations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Associations" Watch "Associations" New topic
Author

Associations

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

The hardest part of OO for me is when I need to translate
the UML model to Java code. There are some easy ones, of course:
1) has-A: Translate it directly into a member field of the class.
e.g. Person has a firstname.
2) is-A: Translate using interfaces or inheritance.
3) arbitrary associations: ?
e.g. Product A was sold to Mr Foo at Barnes&Noble store #123.
I have a big problem with solving scenario #3.
The association that could be derived is:
"b&n store" <-----"sale"----> Person
Mr Foo is an instance of "Person".
Product A is a member field in "sale".
store#123 is a member field of "b&n store".
How do you code the class "sale" ?
Thanks
Pho
[This message has been edited by Pho Tek (edited November 27, 2001).]
[This message has been edited by Pho Tek (edited November 27, 2001).]
[This message has been edited by Pho Tek (edited November 27, 2001).]


Regards,

Pho
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
3) arbitrary associations: ?
e.g. Product A was sold to Mr Foo at Barnes&Noble store #123.
I have a big problem with solving scenario #3.
The association that could be derived is:
"b&n store" <-----"sale"----> Person
Mr Foo is an instance of "Person".
Product A is a member field in "sale".
store#123 is a member field of "b&n store".
How do you code the class "sale" ?

I think I would have said
Barnes&Noble store #123 sold Product A to Mr Foo.

I would make sell() a method in the B&N store class.

Does Mr Foo sell products to B&N stores?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
carl marais
Greenhorn

Joined: Aug 18, 2001
Posts: 18
The normal approach to this problem is to use an Association Class. Both seller and purchaser are given access to this class.
Below is a defination from JGuru posted by John Moore.
An association class is used to model an association as a class. Association classes often occur in many-to-one and many-to-many associations where the association itself has attributes.
As an example, consider a many-to-many association between classes Person and Company. The association could have properties; e.g., salary, jobClassification, startDate, etc. In this case, the association is more correctly modeled as an association class with attributes rather than trying to fold the attributes into one of the classes in the association.
An association class is rendered by a dashed line from the association to the class rectangle. Each link in the association is an object of the association class. An association class is essentially a class attached to an association; the association itself is modeled as a class.
Here are some pointers to consider when modeling with association classes:
You can�t attach the same class to more than one association; an association class is the association.
The name of the association is usually omitted since it is considered to be the same as that of the attached class.
Distinguish between the use of an association class as a modeling technique and the implementation of the association class. There can be several ways to implement an association class.

Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
Originally posted by Pho Tek:
I have a big problem with solving scenario #3.
The association that could be derived is:
"b&n store" <-----"sale"----> Person
Mr Foo is an instance of "Person".
Product A is a member field in "sale".
store#123 is a member field of "b&n store".
How do you code the class "sale" ?

Are you certain you don't mean store#123 is an INSTANCE of "b&n store" ?
Well assuming you have unique codes and/or reference #s for your stores, customers and sale transactions then this is all you need for your DB table, your Sale class could have methods like
public void getSale(String saleRef)
public Person getCustomer(Sale sale)
public Store getStore(Sale sale)
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Colin,

...this is all you need for your DB table, your Sale class could have methods like:
public void getSale(String saleRef)
public Person getCustomer(Sale sale)
public Store getStore(Sale sale)

The interface for "Sale" above roughly means that the "Sale" class has as part of its "members": "Store" and "Customer" instances or references to them. Is it always so straightforward? I don't think so.
Pho

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Associations
 
Similar Threads
GC
Pattern for assigning taxonomy metadata
Want my exceptions to be caught by errorPage JSP ?
Any java class to hold time duration ?
Input in Java