requierment for enhanced for loop in java (J2SE5.0)



Enhanced for loops allow convenient iteration over collections, without the need for an explicitly defined iterator. This reduces the need for boilerplate iteration code and the corresponding opportunities for errors.
as per JSR 121

Running Java without Main method

StaticBlockClass.java
~~~~~~~~~~~~~~~~~~~~~

package com.anil.builder.design.pattern;

public  class StaticBlockClass {

      static{
            System.out.println("i dont have main method");
      }
     
}

TestStaticClient.java
~~~~~~~~~~~~~~~~~~~~~

package com.anil.builder.design.pattern;

public class TestStaticClient {

      public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
            Class.forName("com.anil.builder.design.pattern.StaticBlockClass");
      }
}

Builder DesignPattern

Builder Design pattern is used when there is a requirement for creating complex objects.
if it is a simple object you can make use of factory design pattern.



Car.java
~~~~~~~~~
package com.anil.builder.design.
pattern;

public class Car {

        private String engine;
        private String tyre;
        private String paint;

        public String getEngine() {
                return engine;
        }

        public void setEngine(String engine) {
                this.engine = engine;
        }

        public String getTyre() {
                return tyre;
        }

        public void setTyre(String tyre) {
                this.tyre = tyre;
        }

        public String getPaint() {
                return paint;
        }

        public void setPaint(String paint) {
                this.paint = paint;
        }

}
----------------------------------------------------

CarBuilder.java
~~~~~~~~~~~~~~~

package com.anil.builder.design.pattern;

public interface CarBuilder {
        public String makeEngine();
        public String makeTyre();
        public String makePaint();
        public void makeCar();
        public Car getCar();
}
----------------------------------------------------------

USCarBuilder.java
~~~~~~~~~~~~~~~~~~

package com.anil.builder.design.pattern;

public class USCarBuilder implements CarBuilder {

        private Car car;

        @Override
        public String makeEngine() {
                // TODO Auto-generated method stub
                return "US Engine added ";
        }

        @Override
        public String makeTyre() {
                return "APPOLO tyres added";
        }

        @Override
        public String makePaint() {
                return "Red colour added";
        }

        @Override
        public void makeCar() {
                car=new Car();
                car.setEngine(makeEngine());
                car.setTyre(makeTyre());
                car.setPaint(makePaint());
        }

        @Override
        public Car getCar() {
                // TODO Auto-generated method stub
                return car;
        }

}
--------------------------------------------------------

GermanCarBuilder.java
~~~~~~~~~~~~~~~~~~~~~~

package com.anil.builder.design.pattern;

public class GermanCarBuilder implements CarBuilder {

private Car car;

        @Override
        public String makeEngine() {
                // TODO Auto-generated method stub
                return "German Engine";
        }

        @Override
        public String makeTyre() {
                return "German tyres";
        }

        @Override
        public String makePaint() {
                return "Blue";
        }

        @Override
        public void makeCar() {
                car=new Car();
                car.setEngine(makeEngine());
                car.setTyre(makeTyre());
                car.setPaint(makePaint());
        }

        @Override
        public Car getCar() {
                return car;
        }

}
----------------------------------------------------

Director.java
~~~~~~~~~~~~~
package com.anil.builder.design.pattern;

public class Director {

        private CarBuilder carBuilder;

        public Director(CarBuilder carBuilder) {
                super();
                this.carBuilder = carBuilder;
        }

        public CarBuilder getCarBuilder() {
                return carBuilder;
        }

        public void setCarBuilder(CarBuilder carBuilder) {
                this.carBuilder = carBuilder;
        }

        public void build() {
                carBuilder.makeCar();
        }
}
-----------------------------------------------------------
Test.java
~~~~~~~~~
package com.anil.builder.design.pattern;

public class Test {

        public static void main(String[] args) {
                CarBuilder builder=new USCarBuilder();
                Director director=new Director(builder);
                director.build();
                Car car=builder.getCar();
                System.out.println(" Car Engine : "+car.getEngine());
        }

 Pattern Definition
 Moves the Construction logic of the Object from the Object Class to Builder Class

Improves
  • Object Construction
  • Making new Changes
  • Modular Design

Abstract Factory Design Pattern in Java

Definition : Provides an interface for creating families of related or dependent objects without specifying their concrete classes.




Button.java
~~~~~~~~~
package com.anil.product;

public abstract class Button {
        public abstract void add(String os);
}

WinButton.java
~~~~~~~~~~~~
package com.anil.product;

public class WinButton extends Button {

        @Override
        public void add(String os) {
                System.out.println(" Button Added name  "+os+" for Windows");
        }

}

LinuxButton.java
~~~~~~~~~~~~
package com.anil.product;

public class LinuxButton extends Button {
        @Override
        public void add(String os) {
                System.out.println(" Button Added name  "+os+" for Linux");

        }
}

ComboBox.java
~~~~~~~~~~~~
package com.anil.product;

public abstract class ComboBox {
        public abstract void add(String os);
}

WinComboBox.java
~~~~~~~~~~~~~~~~
package com.anil.product;

public class WinComboBox extends ComboBox {

        @Override
        public void add(String os) {
                System.out.println(" Combo Box Added with name  "+os+" for Windows" );
        }

}

LinuxComboBox.java
~~~~~~~~~~~~~~~~
package com.anil.product;

public class LinuxComboBox extends ComboBox {
        @Override
        public void add(String os) {
                System.out.println(" Combo Box Added name  "+os+" for Linux");
        }

}


OSComponents.java
~~~~~~~~~~~~~~~
package com.anil.product;

public abstract class OSComponents {
        public abstract ComboBox createCombo();
        public abstract Button createButton();
}

WindowsComponents.java
~~~~~~~~~~~~~~~~~~~~~
package com.anil.product;

public class WindowsComponents extends OSComponents {

        @Override
        public ComboBox createCombo() {
                return new WinComboBox();
        }

        @Override
        public Button createButton() {
                return new WinButton();
        }

}

LinuxComponents.java
~~~~~~~~~~~~~~~~~~~~
package com.anil.product;

public class LinuxComponents extends OSComponents {

        @Override
        public ComboBox createCombo() {
                return new LinuxComboBox();
        }

        @Override
        public Button createButton() {
                return new LinuxButton();
        }

}


AbstractOSComponentsFactory.java
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

package com.anil.product;

public abstract class AbstractOSComponentsFactory {
        public abstract ComboBoxInf createCombo();

        public abstract ButtonInf createButton();

        public static AbstractOSComponentsFactory getOSFactory(String s) {
                if (s == "win") {
                        return new WindowsComponentsFactory();
                } else {
                        return new LinuxComponentsFactory();
                }
        }
}

AbstractFactoryClient.java
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package com.anil.product;

public class AbstractFactoryClient {

        public static void main(String os[]) {

                AbstractOSComponentsFactory factory =AbstractOSComponentsFactory.getOSFactory("win");
                factory.createButton().add("windows1");
                factory.createCombo().add("windows2");

        }

}







Benefits

* Isolates concrete classes
* Allows to change product family easily
* Promotes consistency among products
Usage

* When the system needs to be independent of how its products are created composed and represented.
* When the system needs to be configured with one of multiple families of products.
* When a family of products need to be used together and this constraint needs to be enforced.
* When you need to provide a library of products, expose their interfaces not the implementation.