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.

No comments:

Post a Comment