0

this is a small scenario of a big system. i want to model this scenario in class diagram and then implement it in code

this is a scenario in book store.there is customer and books customer can buy books,search books by name,and read books

class diagram implemented in following ways

1.put all operations to the customer class because customer is the one who perform these actions but buyBooks(),searchBook(),readBooks() methods are not responsible for changing the state of customer class (these methods are not working on data inside of customer class ) there for it violate basic oop principle encapsulation.

customer can buy,search,read 1 or more books therefore association marked as shown in image.

i have found lots of class diagrams draw this way when referring the INTERNET

enter image description here

2. put all operations to the customer class same as above. and mark each associations seperately, with three separate lines

associations

  • customer can buy one or more books
  • customer can search one or more books
  • customer can read one or more books

enter image description here

3.in here put all operations to the service class because those methods are not responsible for changing state of neither customer,book class

and then mark association.

enter image description here - among theses which one is correct class diagram ?
- is it acceptable to draw service classes in uml class diagrams ?

1
  • You describe your implementation, but not your requirements. But the latter is what drives the design. Commented May 10, 2018 at 6:10

1 Answer 1

3

Either the first or the last diagram can work in my opinion (once corrected), but I lean towards the first implementation. The second one is just plain wrong.

I would use the first because a "Customer" implies a person whose exclusive purpose in the system is to interact with your Book class, especially in light of the fact that your multiplicity values indicate that a customer must associate with at least one book class. If a customer did more things than interact with books, it might be useful to abstract the things that a customer does with respect to books into a service, but I don't see the need here.

Now, your class diagram syntax is off. The arrows should be open (like >) rather than solid. Also, your multiplicity value on the Book class seems wrong: a book instance will presumably have zero customers associated with it if it hasn't been bought yet, and one customer associated with it if it has. If so, your multiplicity value on the book end should be 0..1.

Finally, your second diagram is wrong because associations are by class rather than by operation; you don't put an association for each operation, but one per class.

Sign up to request clarification or add additional context in comments.

17 Comments

I'm not even convinced that it needs any associations at all. From the requirements I cannot determine if there is a need to create a permanent relation between the customer and the book. If all the customer needs is to be able to read/buy/search, then the book(s) could be passed as parameters. That could be depicted as a dependency.
Open arrow? Generalization? No. Also I wonder how you can give a design suggestion without knowing the requirements.
@ThomasKilian Open arrow as in two lines, such as would be similar to this: >, meaning a unidirectional association. Generalization is of course a white triangle. As for what you wonder about, the first diagram models the specifications given in the OP's second paragraph, doesn't it?
Ahem, yes :-[ THIS arrow (a picture would have said more; please put the above in the answer). But the requirements are not given. There are 3 bullets under2, but that's not where I expect the requirements. They need to be at the beginning.
I agree with @geertbellekens, the description provided so far by OP do not indicate any book association, it only might be a dependency. Also as Thomas Kilian wrote, to answer this question we should better understand the requirements. All three diagrams might be correct depending on the requirements. It might also be that neither is correct. Anyway I would bet a nickel neither accurately represents OPs needs, at least not without significant corrections (and using proper is only first of many).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.