Ronald Djunaedi

About Ronald Djunaedi

Ronald is a senior Java Developer, specialization in J2EE Programming and its technologies and frameworks. Been a trainer for a while, specialization in training subjects: Java Fundamentals, OOP and J2EE.

Factory Design Pattern Case Study

I had a job to check our project code quality. And have to report it back to my team leader for any obstacle that i found in the project. I found a lot of leaks and i think would be good to be discussed on the blog. Not to mock the author, but to learn and improve ourselves together.

Like this code, this is the part that i found in our code.

 public ContactInfoBean(final Reseller resellerInfo) {

        switch(resellerInfo.getType()) {

            case PROGRAM_CONTACT:

                readExecutiveInfo(resellerInfo);

                break;

            case FILE_CONTACT:

                readOperationalInfo(resellerInfo);

                break;

            default:

                break;

        }

    }

The code works fine, and do its job pretty well. But some problem will appear by using this code-style. This class will grow tailing the biz changes, as usual, the bigger one class, the “merrier” to maintain it is. And most likely this class, will be having more than one purpose, can be called low-cohesion.

Better OOP Approach

Well the better approach for the case above would be using the Factory Design Pattern. We can let the factory of READER to generate every single instance according to their type. It would be easier to grow the instance type, since we just need to create a new class and do a little modification in the Factory class. The caller class, wont grow and will stand still at its current shape.

public interface InfoReader {

 public void readInfo();

}
public class ExecutiveReader implements InfoReader {

 public void readInfo() {

  // override

 }

}
public class OperationalReader implements InfoReader {

 public void readInfo() {

  // override

 }

}

And The Factory

public class InfoReaderFactory {

 private static final int PROGRAM_CONTACT = 1;

 private static final int FILE_CONTACT = 2;

 public static InfoReader getInstance(Reseller resellerInfo) {

  InfoReader instance = null;

  switch (resellerInfo.getType()) {

  case PROGRAM_CONTACT:

   instance = new ExecutiveReader();

   break;

  case FILE_CONTACT:

   instance = new OperationalReader();

   break;

  default:

   throw new IllegalArgumentException('Unknown Reseller');

  }

  return instance;

 }

}

And now The Caller

InfoReader reader = InfoReaderFactory.getInstance(resellerInfo);

reader.readInfo();

The Benefits

With the Factory Design Pattern to handle this case, we can achieve some benefits,

  • Specifying a class for one task, means, easier to maintain since one class is for one purpose only (modularity/High Cohesion). i.e: Operational Reader is only to read data for Operational only, no other purpose. Just in case, one day in the future we need another Reader (say: NonOperationalReader). We just need create a new Class that extends (or implements) the InfoReader class and then we can override our own readInfo() function. This Caller class will have no impact. We just need to do some modification in the Factory code.
public class InfoReaderFactory {

 private static final int PROGRAM_CONTACT = 1;

 private static final int FILE_CONTACT = 2;

 private static final int NEW_READER = 3;

 public static InfoReader getInstance(ResellerInfo resellerInfo) {

  InfoReader instance = null;

  switch (resellerInfo.getType()) {

  case PROGRAM_CONTACT:

   instance = new ExecutiveReader();

   break;

  case FILE_CONTACT:

   instance = new OperationalReader();

   break;

  case NEW_READER:

   instance = new NonOperationalReader();

   break;

  default:

   throw new IllegalArgumentException('Unknown Reseller');

  }

  return instance;

 }

}
  • Higher Reusability of Parent’s Component (Inheritance): Since we have parent class (InfoReader), we can put common functions and thingies inside this InfoReader class, and later all of the derivative classes (ExecutiveReader and OperationalReader) can reuse the common components from InfoReader . Avoid code redundancy and can minimize coding time. Eventhough this one depends on how you do the code and cant be guaranteed.

But, It’s Run Perfectly, Should We Change It?

Obviously the answer is big NO. This is only the case study and for your further experience and knowledge. OOP is good, do it anywhere it’s applicable. But the most important thing is, if it’s running, dont change it. It would be ridiculous if you ruin the entire working code just to pursue some OOP approach. Dont be naive also, no one can achieve the perfect code. The most important is we know what is the better approach.

Reference: Case Study: Factory Design Pattern from our JCG partner Ronald Djunaedi at the Naming Exception blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Factory Design Pattern Case Study"

  1. Netanel says:

    I think the example is nice but the getInstance method shouldn’t be static so a unit test will be able to mock the factory and return specific mock of InfoReader with static method we can’t.

Leave a Reply


7 + = eleven



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close