|
Sponsored Links
Resources
Enterprise Java Research Library
Get Java white papers, product information, case studies and webcasts
|
J2EE patterns
J2EE patterns
J2EE patterns
|
Messages: 9
Messages: 9
Messages: 9
Printer friendly
Printer friendly
Printer friendly
Post reply
Post reply
Post reply
XML
XML
XML
|
 |
Intelligent Lazy [IL] Pattern
IL Pattern asks the creator to wait until it gets all or required information about the creating object. Let's assume 'CarProducer' is the creator object and 'Car' is the creating(produced) object.
Here the CarProducer waits until it gets Doors, breaks, steering, Tyres, etc...So the producer makes sure that all the necessary objects are avilable to create the Car.
Let's write a java class for the same. start with Car Producer class public class CarProducer { public CarProducer() { }
public Car createCar(Break breaks,Tyres tyres,Glass glass) { boolean breaksAvailable=false; Car car=null; if(breaks!=null) { breaksAvailable=true; } if(tyres!=null) { tyresAvailable=true; } if(glass!=null) { glassAvailable=true; }
//checks if all parts are available if(breaksAvailable==true && tyresAvailable==true && glassAvailable==true) { car=new Car(breaks, tyres, glass); } else if (breaksAvailable==true && tyresAvailable==true ) { car=new Car(breaks, tyres, null); //still i will make a car with out front glasses :-)) } else { throw NoTyresAndBreaksException; //Car without Tyre :-)) }
return car; } }
Now let's right Car class;
class Car { Break breaks; Tyres tyres; Glass glass;
public Car(Break breaks,Tyres tyres,Glass glass) { this.breaks=breaks; this.tyres=tyres; this.glass=glass; } }
Here the Car producer creates a Car only when it gets breaks, tyres and optionally glass. If no breaks and tyres available, it will not create the Car object. Now let's assume that we are storing the Car object to a database. 1.We can restrict insering an incomplete object to a database. 2.If the Car object is incomplete, it will not pass to the next level of components who will use this car object. 3. We can make sure that the object created with required attributes,etc....
So it creates an object intelligently and too lazy !!!
Welcoming your suggetions.
Pratheesh TU, Sasken Communications.
|
|
Message #268081
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
What is the difference between IL and Factory pattern
what is the difference between IL and Factory pattern?
|
|
Message #269832
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Re: What is the difference between IL and Factory pattern
Factory Pattern creates objects whenever a request comes for the creation.It will not check the all the required attrbutes available or not.In the case of IL pattern , it waits untils all the required properties set to create that particular object. Then only it will instantiate the real object. Hope this helps you to understand the IL pattern.
Regards Pratheesh TU
|
|
Message #270991
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Re: What is the difference between IL and Factory pattern
I can't think of any practical usage of IL.
|
|
Message #271361
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Wrong responsibility
The code of your CarProducer should be in the constructor of the Car class. You do not need the CarProducer. If you really want a factory you can use a standard factory additionally.
|
|
Message #303089
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Wait
Your explanation is incorrect: you class does not actually "waits" until it gets all the required data. It simply refuses to create it if there is some missing parts, which is not at all the same thing.
And your verification code is overly complicated, it could have been:
if (breaks == null || tyres == null{ throw new NoTyresAndBreaksException(); }
|
|
Message #303788
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Re: Intelligent Lazy [IL] Pattern
This is nothing but buider pattern. As some one else is also pointed out , you "pattern" does not really waits, it just raise an exception and expects client to take care of that.
|
|
Message #303893
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Re: Intelligent Lazy [IL] Pattern
To "Rohit Verma" I completely agree it is the Builder pattern. I would have rewritten
public CarProducer() { private Break breaks; private Tyres tyres; private Glass glass;
public CarProducer breaks(Breaks breaks){
this.breaks = breaks; return this; }
public CarProducer breaks(Tyres tyres){
this.tyres = tyres; return this; }
public CarProducer glass(Glass glass){
this.glass = glass; return this; }
public Car createCar(){
if (breaks == null || tyres == null{ throw new NoTyresAndBreaksException(); }
return new Car(breaks, tyres, class); } }
class Car { private final Break breaks; private final Tyres tyres; private final Glass glass;
public Car(Break breaks,Tyres tyres,Glass glass){
this.breaks=breaks; this.tyres=tyres; this.glass=glass; } }
So if client wants to create a car: ... Car car = new CarProducer().breaks(breaks).tyres(tyres).glass(glass).createCar(); ...
This pattern is described in "Effective Java 2nd. Edition" Joshua Bloch
|
|
Message #305516
Post reply
Post reply
Post reply
Go to top
Go to top
Go to top
|
 |
Re: Intelligent Lazy [IL] Pattern
How these patterns are better than using a restricted constructor?. We can solve this problem by not providing the default constructor, and have only one single constructor with mandate fields as arguments. This is the simple solution.
|
|
 |
EJB Design Patterns PDFEJB Design Patterns PDFEJB Design Patterns PDF |
 |
 |

EJB Design Patterns is now available for free download in PDF format. The book contains a catalog of twenty advanced EJB patterns and provides strategies for mapping application requirements to patterns-driven design, J2EE development best practices, and more. EJB Design Patterns was the #2 book at Java One 2002, and held the #1 Java book position on Amazon.com for weeks since the book was released in March. Download your PDF here.
|
Useful patterns around the webUseful patterns around the webUseful patterns around the web |
 |
 |
This
essential pattern describes how to model your entity beans.
How to make an entity bean a facade to a set of dependent objects.
Every guru should use unit testing.
|
Other Patterns sitesOther Patterns sitesOther Patterns sites |
 |
 |
The original reference
site for patterns. Frequented by the gang of 4 and their mentors (Kent Beck, Ward Cunningham).
A catalogue of J2EE design patterns from Suns Consulting Division.
A catalogue of
high level business, architectural and topological patterns for large scale systems.
Design Patterns from the J2EE Blueprints, Suns authoritative guide to J2EE development.
|
|