667514 members! Sign up to stay informed.

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

Posted by: Pratheesh TU on August 29, 2008 DIGG
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.

Threaded replies

·  Intelligent Lazy [IL] Pattern by Pratheesh TU on Fri Aug 29 08:16:43 EDT 2008
  ·  What is the difference between IL and Factory pattern by Gary Zhu on Mon Sep 08 05:20:31 EDT 2008
    ·  Re: What is the difference between IL and Factory pattern by Pratheesh TU on Thu Sep 25 09:08:22 EDT 2008
      ·  Re: What is the difference between IL and Factory pattern by Kamil Tabak on Thu Oct 09 18:00:23 EDT 2008
  ·  pattern? by D Liang on Wed Sep 10 05:42:02 EDT 2008
  ·  Wrong responsibility by Patrick Holthuizen on Sat Oct 18 02:33:04 EDT 2008
  ·  Wait by Guillaume Bourassa on Fri Jan 23 01:20:26 EST 2009
  ·  Re: Intelligent Lazy [IL] Pattern by Rohit Verma on Mon Feb 02 06:41:12 EST 2009
    ·  Re: Intelligent Lazy [IL] Pattern by Sergey Gus on Tue Feb 03 08:08:34 EST 2009
  ·  Re: Intelligent Lazy [IL] Pattern by Bhaskar Reddy on Sat Feb 28 22:34:39 EST 2009
  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

Posted by: Gary Zhu on September 08, 2008 in response to Message #267590
what is the difference between IL and Factory pattern?

  Message #268199 Post reply Post reply Post reply Go to top Go to top Go to top

pattern?

Posted by: D Liang on September 10, 2008 in response to Message #267590
Is it the new pattern you think up?
I don't think it's a 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

Posted by: Pratheesh TU on September 25, 2008 in response to Message #268081
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

Posted by: Kamil Tabak on October 09, 2008 in response to Message #269832
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

Posted by: Patrick Holthuizen on October 18, 2008 in response to Message #267590
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

Posted by: Guillaume Bourassa on January 23, 2009 in response to Message #267590
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

Posted by: Rohit Verma on February 02, 2009 in response to Message #267590
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

Posted by: Sergey Gus on February 03, 2009 in response to Message #303788
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

Posted by: Bhaskar Reddy on February 28, 2009 in response to Message #267590
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.

J2EE PatternsJ2EE PatternsJ2EE Patterns
Patterns

We are proud to provide this patterns/strategies repository to the community. Feel free to post any useful design tips you know!

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
Patterns

EntityBeansAsDomainObjects

This essential pattern describes how to model your entity beans.

The Aggregate Entity pattern

How to make an entity bean a facade to a set of dependent objects.

EJB Unit Testing Strategies

Every guru should use unit testing.

Other Patterns sitesOther Patterns sitesOther Patterns sites

Portland Patterns Repository

The original reference site for patterns. Frequented by the gang of 4 and their mentors (Kent Beck, Ward Cunningham).

Sun Java Center Patterns

A catalogue of J2EE design patterns from Suns Consulting Division.

IBM Patterns for e-Business

A catalogue of high level business, architectural and topological patterns for large scale systems.

J2EE Blueprints Patterns

Design Patterns from the J2EE Blueprints, Suns authoritative guide to J2EE development.


News | Blogs | Discussions | Tech talks | Patterns | Reviews | White Papers | Downloads | Articles | Media kit | About
Java Solutions
All Content Copyright ©2007 TheServerSide Privacy Policy
Site Map