Design Patterns - SPLessons

Factory Method Pattern

Home > Lesson > Chapter 5
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Factory Method Pattern

Factory Method Pattern

shape Description

Factory method pattern, also referred as Factory Pattern, is the most commonly used design pattern. It is a sub category of creational pattern and is associated with object creation. In factory pattern, object can be created by hiding the logic to the client. So, factory pattern is known as "Virtual Constructor". Factory method pattern makes the design completely customizable. There is a lot of difference between requesting an object and creating an object.

shape Advantages

  • A sub class can create its own objects.
  • It uses loose-coupling and interacts with related interfaces and abstract classes.

shape Conceptual figure

shape Examples

Creating an abstract class Computer [java]public abstract class Computer { public abstract String getRAM(); public abstract String getHD(); public abstract String getprocessor(); public String toString() { return "RAM= "+this.getRAM()+", HD="+this.getHD()+", PROCESSOR="+this.getprocessor(); } }[/java] Creating a class ComputerFactory and extending the features of Computerclass. [java] classpublic class ComputerFactory { public static Computer getComputer(String type, String ram, String hdd,String processor){ if("PC".equalsIgnoreCase(type)) return new PC(ram, hd,processor); else if("Server".equalsIgnoreCase(type)) return new Server(ram, hd,processor); return null; } }[/java] Creating a first sub class that extends the computer class [java] public class PC extends Computer { private String ram; private String hd; private String processor; public PC(String ram, String hdd, String processor)//overrides the class computer method { this.ram=ram; this.hd=hd; this.processor=processor; } public String getRAM()//overrides the class computer method { return this.ram; } public String getHD()//overrides the class computer method { return this.hd; } public String getprocessor()//overrides the class computer method { return this.cpu; } }[/java] Creating a second sub class that extends the computer class [java]public class Server extends Computer { private String ram; private String hd; private String processor; public Server(String ram, String hdd, String cpu)//overrides the class computer method { this.ram=ram; this.hd=hd; this.processor=processor; } public String getRAM()//overrides the class computer method { return this.ram; } public String getHDD()//overrides the class computer method { return this.hd; } public String getprocessor()//overrides the class computer method { return this.processor; } }[/java] Creating a main class for accessing the computer class and its sub classes [java]public class TestFactory { public static void main(String[] args) { Computer pc = ComputerFactory.getComputer("pc","4 GB","250 GB","2.4 GHz");//object is created for Computer class Computer server = ComputerFactory.getComputer("server","20 GB","1 TB","2.9 GHz"); System.out.println("Factory PC Config::"+pc); System.out.println("Factory Server Config::"+server); } }[/java] Factory pattern can be used to code for interface rather than implementation. Factory pattern eliminates the process of instantiation of actual implementation classes from client code, thus making it more robust, less coupled and easy to extend. Factory pattern supports the abstraction between implementation and client classes through inheritance.

shape Output

[java]Factory PC Config::RAM= 4 GB, HD=250 GB, PROCESSOR=2.4 GHz Factory Server Config::RAM= 20 GB, HD=1 TB, PROCESSOR=2.9 GHz[/java]

Summary

shape Key Points

  • Factory Design Patterns are mostly used in spring and struts as frameworks.
  • The factory method is one of the most used and robust design patterns.
  • The advantage of the Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type.