Search code examples
oopdesign-patternsumlmodeling

how to recognize object's responsibility?


I'm new in OOP and I just started learning it. Its too complicated to determine the functionality of classes. Let's take an example:

We have an Address-book and an user want to add a new contact to it. In this scenario we have 2 classes:

  • User: that determine the user that logged in.
  • Contact: A contact object that consists of Name, Address, Phone Number, etc

And the questions:

  • Who have to save a new contact?User class or Contact Class
  • If we try to check the user's permission before doing anything where is the best place for it?
  • Is it OK that these classes have a access to database?(Is it better to create 3rd class for doing query stuffs?)

Thanks for any good idea ;)


Solution

  • Usable distribution of "responsibility" is an OOP design and architecture decision with no single simple correct answer. For discussion refer to Stack Overflow question What is the single most influential book every programmer should read?

    You'll learn the pros/cons by coding (using someone's design or creating your own design which does not work well).

    However there are some useful/frequent distributions of responsibility already known as http://en.wikipedia.org/wiki/Software_design_pattern

    In my opinion the only fixed fact is that each class/function/structure should have its responsibility clearly defined/documented - since the very first lines of code - and "do one thing and do it well"