Friday, February 14, 2014

Spring AOP - JointPoint and PointCut


This is the easiest and best definition of JointPoint and PointCut by Craig Walls , the author of Spring in Action.

When you go out to a restaurant, you look at a menu and see several options to choose from. You can order one or more of any of the items on the menu. But until you actually order them, they are just "opportunities to dine". Once you place the order and the waiter brings it to your table, it's a meal.
Join points are the options on the menu and pointcuts are the items you select. A joinpoint is an opportunity within code for you to apply an aspect...just an opportunity. Once you take that opportunity and select one or more joinpoints and apply an aspect to them, you've got a pointcut.

Saturday, February 8, 2014

Difference between Association, Composition and Aggregation in Java, UML

When we have only one relationship between objects, that is called Association. Aggregation and Composition both are specialized form of Association.

Composition is again specialize form of Aggregation.
           
            Association is a relationship where all object have their own life-cycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own life-cycle. Both can create and delete independently.

            Aggregation is a specialize form of Association where all object have their own life-cycle but there is ownership and child object cannot belongs to another parent object. Let’s take an example of Department and teacher. A single teacher cannot belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.

            Composition  is again specialize form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have their life-cycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room cannot belongs to two different house if we delete the house room will automatically delete. Let’s take another example relationship between Questions and options. Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will automatically delete.

Difference between Association, Aggregation and Composition





Here is the list of differences between Composition and Aggregation in point format, for quick review.The key difference between them comes from the point that in case of Composition, One object is OWNER of other object, while in case of aggregation, one object is just a USER or another object.

1) If A and B two classes are related to each other such that, B ceased to exist, when A is destroyed, then association between two object is known as CompositionExample is Car and Engine. While if A and B are associated with each other, such that B can exist without being associated with A, then this association in known as Aggregation.

2) In case of Composition A owns B e.g. Person is owner of his HandMind and Heart, while  in case of Aggregation, A uses B e.g. Organization uses People as employee.

3) In UML diagram Association is denoted by normal arrow head, while Composition is represented by filled diamond arrow head, and Aggregation is represented by empty diamond arrow head, As shown in below and attached diagram in third paragraph.

Association  A---->B
Composition  A-----B
Aggregation  A-----<>B

4) Aggregation is a lighter form of Composition, where sub-part object can meaningfully exits without main objects.

5) In Java, you can use final keyword to represent Composition. Since in Composition, Owner object expect part object to be available and functions, by making it final, your provide guarantee that, when Owner will be created, this part object will exist. This is actually a Java idiom to represent strong form of association i.e. composition between two objects.

6) Another interesting word, which comes handy to understand difference between Composition and Aggregation in software design is "part-of" and "has". If one object is part-of another object e.g. Engine is part of Car, then association or relationship between them is Composition. On the other hand if one object just has another object e.g. Car has driver than it's Aggregation.

That's all on difference between Association, Composition and Aggregation in UML, Java and Object oriented design. Since object oriented analysis is more about defining relationship between object, it's important to know what kind of relationship exists between them, composition and aggregation is a two way of representing relationship between two objects.

"Islands of Isolation" in garbage collection?

you may already know that when an object is not referenced by other objects, it's eligiable for garbage collection. But do you know the following two statemenats are also true?

  • "If an object obj1 is garbage collected, but another object obj2 contains a reference to it, then obj2 is also eligible for garbage collection"
  • "If object obj2 can access object obj1 that is eligible for garbage collection, then obj2 is also eligible for garbage collection"
This is called "Island of Isolation". An "island of isolation" describes one or more objects have NO references to them from active parts of an application.
In When is an object eligible for garbage collection?, we talked about: any object, that are not accessible from root set of references, is eligible for garbage collection. If object obj1 is eligible for garbage collection meaning it is not reachable by any objects from root set of references. Then the garbage collection algorithm tries to find any objects that have ONLY refernce to object obj1 (in this case object obj2) which also become eligible for garbage collection.
If obj2 was accessible from root then the object obj1 would never be eligible for garbage collection in the first place. Therefore, it must be that the object obj2 cannot be referenced from the active part of the program, and so object obj2 is eligible for garbage collection.
Here is an example,

class Person {
   public Person firend;
   public static void main(String[] args) {
     Person obj1 = new Person();
     Person obj2 = new Person();
     obj2.firend = obj1;

     obj1 = null;  //Line A
     obj2 = null;  //Line B

     .....
   }
}

After Line A executes, The object obj2 still has a reference to the object obj1 and the objectobj2 is still referenced by the variable obj2. Therefore, the object obj1 can not be eligable for garbage collection. After Line B exectues, there are no more references to the object obj2. There still is a reference to object obj1 inside the object obj2. Now, the object obj1 and obj2has no reference from root set of references. Therefore, both of objects are eligible for garbage collection.