1

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 ;)

HaMeD
  • 349
  • 1
  • 14

3 Answers3

1

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"

Community
  • 1
  • 1
xmojmr
  • 8,073
  • 5
  • 31
  • 54
  • Tanks for your answer. After your answer, another question comes in mind and its Where should I start ?!! – HaMeD Jun 20 '14 at 08:28
  • "**practice makes perfect**". You can read the design patterns and then write the code (or UML draw-it) where contact-saves, user-saves, some DB access layer saves, something else. And compare it from your point of view which one is nicer, which one is easier to maintain, which one can be safely sealed and don't need any extensions later etc. In my early days I usually wrote 4 prototypes where 3 of them were sooner or later thrown away. If I got stuck I tried another way. **Start coding** – xmojmr Jun 20 '14 at 11:07
0

Contacts are user specific. Thus every user object (class instance) should contain its own contacts object which is a container of contact (other user) objects, comprising in turn of name, address, phone etc.

class User {
    String name;
    String phone;
    String address;
    Contacts contacts;
    ....
}

class Contacts {
    List<User> items;
}

The Contacts class should have the implementation of saving a new contact, which needs to be called from a User method, something like the following.

User u;
Contacts c = u.getContacts();
c.addContact(name, address, phone);

User's permissions should be checked in the User class.

The methods of these classes should interface with the database. For this each class method can open a new connection to a database and execute SQL queries. Example method of User cass:

User getContact(String name) {
    Connection conn = getConnection();
    ....
    PreparedStatement ps = con.prepareStatement("select * from Contacts where name = ?");
    ...

    return userRcd;        
}
Debasis
  • 3,680
  • 1
  • 20
  • 23
0

1) Save new contact must the separate class, which working directly with database 2) Best place to check user permission - in user class of course 3) See the item 1:)

I recommend you get strong knowledge about SOLID principles, it's basics for good design.

brewerof
  • 92
  • 2
  • 7