• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Associations

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
carl marais
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic