Sunday, 28 April 2019

Attribute or Association


When writing code for a class there are two main things, variables and methods. Variables correspond to the data present in each object. This data is of two types - one the details about the object itself and other the details about this objects relation with other objects. 

Details about the object itself are its attributes and details about its relation with other objects are associations. Both get encoded in the class variables.

An example : Student class has its attributes : name, age, roll number, height, weight. Then it is also associated with a Grade - so we need to code a variable named grade which points to a Grade class. Grade class can have its own attributes : number, head count, class teacher, section name. Grade class can have the association back to all students.

Student and Grade class diagram :


The way its code will be written will be something like following :

class Student {
  private String name;
  private String age;
  // ignoring other attributes for simplicity

  // this represents association
  private Grade grade;

  // rest of code will be getters / setters 
}

class Grade {
  private int number;
  private int headCount;
  // ignoring other attributes for simplicity

  // this represents association
  private List<Student> students;

  // rest of code will be getters / setters 
}



One interesting question is when should an attribute become association or vice versa ? Here it is clear that Student object does not have a Grade as its attribute, but what if one represents grade as just a grade number and say we dont need any more details about the Grade in the system, is grade number then an attribute or an association ? Clearly depending on the system we are implementing and how much data is localized in a class vs how much data is spread across the system, one needs to extract out an attribute to an association. Another example here is : age, height and weight attributes of the Student - what if we had a HealthPersona class, would these still be attributes of student or should one put them in HealthPersona ? Depends what kind of system one is building and if these properties are in wide use across the system then this data should be taken out shared properly across the system and Student will have an association with HealthPersona.



Saturday, 6 April 2019

Let's code a Library Management System - Part 2

This is in continuation to part 1 , we have already seen how to come up think about use cases and create a class diagram. Now in this blog I will cover activity diagram and the actual c++ code.

Activity diagrams :

Here we think about some use cases and how the control flows in it ( Check here for meaning of various symbols used in diagram). Here main use cases are : Borrow an item, Return an item.

Borrow an item : In our library how will anyone borrow an item ? First a member shares his id or name by a library card to librarian, then will request for an item by saying id or barcode, Librarian will check for any fines or status of account then librarian will check on the system if the item is not issued and not reserved and if the number of items which can be issued are not max yet. Then item issue details will be updated in the system and member will be given the item. If its not available but there is no reservation on the item, then librarian tells member about the option to reserve, in which case the reservation status for that item will be updated and member will be notified in future when the book is available.



Return an item : Member gives the item to librarian. Librarian checks the barcode or id on the item. Librarian fetches the item issue details and checks for fine. If any fine collects it. Then updates the item issue status to available. Checks if there is any reservation on the item, then system notifies that member about the book being available.




Same way one can comeup with the reserve an item activity.

Now time to look into actual code, this is a work in progress and  will be done gradually and improved upon :