Obect instatiation is a critical part of any application. If a class is creating an instance of other then it has to know the way to get the instance. The instatiation can be decoupled using the factory class. The factory class is responsible for returning an instance to the requester class. Usually it is the constructor that returns the instance but it can be anyother method that does the job in case of factory class.
Due to decoupling, the factory class can be used to select the instance required by the requester class. The factory class depending upon the argument passed to it decides which class' instance is to be returned.
e.g.
Due to decoupling, the factory class can be used to select the instance required by the requester class. The factory class depending upon the argument passed to it decides which class' instance is to be returned.
e.g.
class AddressFactory {
Object getInstance(arg) {
if(arg==1){
return (new usaddress);
} else {
return (new indiaaddress);
}
}
}
class usaddress implements address{
}
class indiaaddress implements address{
}
interface address {
}
class requester {
AddressFactory addrfactory = new AddressFactory();
address addr = addrfactory.getInstance(1); //getting usaddress
}
0 Comments On This Entry
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
My Blog Links
Recent Entries
-
-
-
SCJP Notes - Part 6on Mar 27 2008 08:19 AM
-
SCJP Notes - Part 5on Mar 27 2008 08:05 AM
-
Recent Comments
Search My Blog
10 user(s) viewing
10 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



Leave Comment









|