Showing posts with label design. Show all posts
Showing posts with label design. Show all posts

Tuesday, December 27, 2011

Extend Thread vs implement Runnable

There are two ways to create your own thread type: subclass java.lang.Thread class, or implementing java.lang.Runnable and pass it to Thread constructor or java.util.concurrent.ThreadFactory. What is the difference, and which one is better?
  1. The practical reason is, a Java class can have only one superclass. So if your thread class extends java.lang.Thread, it cannot inherit from any other classes. This limits how you can reuse your application logic.
  2. From a design point of view, there should be a clean separation between how a task is identified and defined, between how it is executed. The former is the responsibility of a Runnalbe impl, and the latter is job of the Thread class.
  3. A Runnable instance can be passed to other libraries that accept task submission, e.g., java.util.concurrent.Executors. A Thread subclass inherits all the overhead of thread management and is hard to reuse.
  4. Their instances also have different lifecycle. Once a thread is started and completed its work, it's subject to garbage collection. An instance of Runnalbe task can be resubmitted or retried multiple times, though usually new tasks are instantiated for each submission to ease state management.

Sunday, June 12, 2011

Various relations in software design - Association, Aggregation, Composition, Abstraction, Generalization, Realization, Dependency

We will just discuss about some of the terms used in software engineering, regarding relationship among the objects, and how they are represented in class diagram.

Association

Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.

Example: A Student and a Faculty are having an association. The relationship between a car and a driver is representative of this relationship. The car will have a driver who will be associated with the car for a period of time.

Aggregation

Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. Aggregation is also called a “Has-a” relationship.

 
A FileReader object that has been created using a File object represents a mutual dependency where the two objects combine to create a useful mechanism for reading characters from a file. The UML symbols for expressing this relationship as shown in following figure which involve a line connecting the two classes with a diamond at the object that represents the greater whole.


Composition

Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.

Example: A class contains students. A student cannot exist without a class. There exists composition between class and students. Other example of this is a customer object and its related address object; the address object cannot exist without a customer object. This relationship is shown with a darkened diamond at the object, which represents the greater whole, as shown in the following figure:

Difference between aggregation and composition

Composition is more restrictive. When there is a composition between two objects, the composed object cannot exist without the other object. This restriction is not there in aggregation. Though one object can contain the other object, there is no condition that the composed object must exist. The existence of the composed object is entirely optional. In both aggregation and composition, direction is must. The direction specifies, which object contains the other object.
Example: A Library contains students and books. Relationship between library and student is aggregation. Relationship between library and book is composition. A student can exist without a library and therefore it is aggregation. A book cannot exist without a library and therefore its a composition. For easy understanding I am picking this example. Don’t go deeper into example and justify relationships!

Abstraction

Abstraction is specifying the framework and hiding the implementation level information. Concreteness will be built on top of the abstraction. It gives you a blueprint to follow to while implementing the details. Abstraction reduces the complexity by hiding low level details.
Example: A wire frame model of a car.

Generalization

Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure and behaviour are used from the specializtion to the generalized class. At a very broader level you can understand this as inheritance. Why I take the term inheritance is, you can relate this term very well. Generalization is also called a “Is-a” relationship.

Example: Consider there exists a class named Person. A student is a person. A faculty is a person. Therefore here the relationship between student and person, similarly faculty and person is generalization.

Realization

Realization is a relationship between the blueprint class and the object containing its respective implementation level details. This object is said to realize the blueprint class. In other words, you can understand this as the relationship between the interface and the implementing class.

Example: A particular model of a car ‘GTB Fiorano’ that implements the blueprint of a car realizes the abstraction.

Dependency

Change in structure or behaviour of a class affects the other related class, then there is a dependency between those two classes. It need not be the same vice-versa. When one class contains the other class it this happens.

Example: Relationship between shape and circle is dependency. The same relationship would exist for a stock transfer object that needed to use a stock item object to get the information on the stock item being transferred. The stock transfer object would have a dependency relationship with the stock item object; the stock transfer object would read the transfer information and could then discard the stock item object and continue processing.

Friday, September 17, 2010

Making library of books

To have library we can have 2 components for sure:
1. Books
2. Person borrowing that books

So we need a person class
class Person
{
String name;
public Person (String n)
{
name = n;
}

public String details ()
{
return name;
}

public String toString()
{
return "name <"+name+">";
}
}
 
Person must take membership from library...so something has to be added to above
class Membership
{
public boolean canLoan(int onLoan)
{
return false;
}
}

class JuniorMember extends Membership
{
public boolean canLoan(int onLoan)
{
return onLoan<1;
}
public String toString()
{
return "Junior";
}
}

class StandardMember extends Membership
{
public boolean canLoan(int onLoan)
{
return onLoan<6;
}
public String toString()
{
return "Standard";
}
}

class OAPMember extends Membership
{
public boolean canLoan(int onLoan)
{
Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DAY_OF_WEEK);
//if (day==Calendar.SATURDAY || day==Calendar.SUNDAY)
if (day == Calendar.THURSDAY)
return false;
return true;
}
public String toString()
{
return "OAP";
}
}
We have to add how many books person borrowed...so we have to add following function:
 So now person class become:
public class Person
{
String name;
Membership mem;
int books;
public Person (String n, Membership member)
{
name = n;
mem = member;
}

public boolean borrow(Book b)
{
if (b.canLoan() && mem.canLoan(books))
{
books++;
return true;
}
return false;
}

public String details ()
{
return name+":"+mem;
}

public String toString()
{
return "name <"+name+"> "+mem;
}
}

 

Extending books to loanable and reference books

class LoanBook extends Book
{
public LoanBook (String t, String a, int c1, int c2, float p)
{
super(t,a,c1,c2,p);
}

public boolean canLoan()
{
return true;
}

public String details ()
{
return "Loan: "+super.details();
}

public String toString()
{
return "loan: "+super.toString();
}
}

class ReferenceBook extends Book
{
public ReferenceBook (String t, String a, int c1, int c2, float p)
{
super(t,a,c1,c2,p);
}

public boolean canLoan()
{
return false;
}

public String details ()
{
return "Reference: "+super.details();
}

public String toString()
{
return "Ref: "+super.toString();
}
}



class Loan
{
Book book;
Person person;
Date date = new Date();

public Loan (Book b, Person p)
{
book = b;
person = p;
}

public String details ()
{
return book.details()+" loaned on "+date+" to "+person.details();
}

public String toString()
{
return "book "+book+" -> person"+person+" @date "+date;
}
}

public class Library
{
public static void main(String[] args)
{
Book b1 = new LoanBook ("Java I/O Programming","E.R.Harrold",123,45,18.99F);
Book b2 = new LoanBook ("Java in a Nutshell","D.Flanagan",123,25,12.99F);
Book b3 = new ReferenceBook ("The Java CLass Libraries","P.Chan & R.Lee",2123,10,35.00F);

System.out.println("Books");
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println("");

System.out.println("People:");
Person james = new Person ("James Gosling");
System.out.println(james);
System.out.println("");

Loan l1 = makeLoan(james, b1);
Loan l2 = makeLoan(james, b2);
Loan l3 = makeLoan(james, b3);
}

private static Loan makeLoan(Person p, Book b)
{
Loan l=null;
if (b.canLoan())
{
l = new Loan(b,p);
System.out.println(l.details());
}
else
System.out.println("Cannot loan "+b.details()+" to "+p.details());
System.out.println("");
return l;
}


}

Book class 1

class Book
{
String title;
String author;
float price;
int cat, subCat;

public Book (String t, String a, int c1, int c2, float p)
{
title = t;
author = a;
price = p;
cat = c1;
subCat = c2;
}

public String details ()
{
return title+", "+author+" Category "+cat+"."+subCat+" value $"+price;
}

public String toString()
{
return "Title <"+title+"> Author <"+author+"> cat:"+cat+" subCat:"+subCat+" price:"+price;
}
}

Chitika