Discussions

J2EE patterns: Intelligent Lazy [IL] Pattern

  1. Intelligent Lazy [IL] Pattern (9 messages)

    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 Messages (9)

  2. what is the difference between IL and Factory pattern?
  3. 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
  4. I can't think of any practical usage of IL.
  5. pattern?[ Go to top ]

    Is it the new pattern you think up? I don't think it's a pattern!
  6. Wrong responsibility[ Go to top ]

    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.
  7. Wait[ Go to top ]

    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(); }
  8. Re: Intelligent Lazy [IL] Pattern[ Go to top ]

    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.
  9. Re: Intelligent Lazy [IL] Pattern[ Go to top ]

    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
  10. Re: Intelligent Lazy [IL] Pattern[ Go to top ]

    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.