TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Factory Design Pattern

Implement the factory design pattern. Use a method to return a derived class.
Factory pattern. With design patterns, we formalize how classes are used together. This makes programs clearer and easier to develop—more organized.
In a factory, objects of a common derived type are returned. So we use a method, a factory method, to return classes that "extend" a common base class.
Example implementation. In this code, we create a hierarchy of classes: the Position abstract class is the base. And Manager, Clerk and Programmer extend that base class.

Factory: This class contains a get() method. It uses a switch statement to return a class based on an id number.

Switch

And: In the factory, derived types are returned. They are implicitly cast to the Position base class.

Main: We invoke the Factory.get() method. Get() is static so we need no Factory instance.

Static
Java program that uses Factory design pattern abstract class Position { public abstract String getTitle(); } class Manager extends Position { public String getTitle() { return "Manager"; } } class Clerk extends Position { public String getTitle() { return "Clerk"; } } class Programmer extends Position { public String getTitle() { return "Programmer"; } } class Factory { public static Position get(int id) { // Return a Position object based on the id parameter. // ... All these classes "extend" Position. switch (id) { case 0: return new Manager(); case 1: case 2: return new Clerk(); case 3: default: return new Programmer(); } } } public class Program { public static void main(String[] args) { for (int i = 0; i <= 3; i++) { // Use Factory to get a Position for each id. Position p = Factory.get(i); // Display the results. System.out.println("Where id = " + i + ", position = " + p.getTitle()); } } } Output Where id = 0, position = Manager Where id = 1, position = Clerk Where id = 2, position = Clerk Where id = 3, position = Programmer
With a factory, we are creating an abstraction of the creation of classes. The inheritance mechanism, where derived types can be treated by their base class, is used.
Consider this. In massively complex programs, creating classes in many ways, throughout the code, may become complicated. With a factory, all this logic is in one place.
With organized, factory-based creation, class instantiation is easier to analyze and understand. It can be changed by editing just one method, not many code locations.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf