0% found this document useful (0 votes)
106 views6 pages

Factory Design Pattern (2) - Other Design Patterns Necessitate The Creation of New Classes

The document discusses the factory design pattern, including its benefits, categories, and application. It provides examples of when the factory pattern is useful, such as when object dependencies are unknown or when new subclasses need to be added. The factory pattern avoids tight coupling between creators and products and allows introducing new product types without breaking client code. While it can make code more complex, it also improves flexibility and reusability.

Uploaded by

Tsion Mengistu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views6 pages

Factory Design Pattern (2) - Other Design Patterns Necessitate The Creation of New Classes

The document discusses the factory design pattern, including its benefits, categories, and application. It provides examples of when the factory pattern is useful, such as when object dependencies are unknown or when new subclasses need to be added. The factory pattern avoids tight coupling between creators and products and allows introducing new product types without breaking client code. While it can make code more complex, it also improves flexibility and reusability.

Uploaded by

Tsion Mengistu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction

Object-oriented software is difficult to design, and reusable object-oriented software is even


more difficult. You must locate relevant items, factor them into appropriate classes at the
appropriate granularity, design class interfaces and inheritance hierarchies, and establish critical
interactions between them. Your solution should be tailored to the problem at hand while still
being generic enough to handle future issues and needs. You should also try to avoid or at least
limit redesign. Experienced object-oriented designers will tell you that getting a reusable and
adaptable design "right" the first time is tough, if not impossible.
What are the benefits of using design patterns? By presenting solutions that have already been
proven, design patterns can considerably speed up development. Design patterns also improve
the readability of the code for other programmers and the design for future maintenance or
adjustment. Developers can communicate using well-known words and software interactions
thanks to design patterns. Another important characteristic of design patterns is that they evolve
and improve with time, making them more sophisticated than haphazard designs. Creational,
structural, and behavioral design patterns are the three basic categories of design patterns. Class
instantiation or object generation is the focus of creational design patterns. Structural design
patterns are organizing different classes and objects to form larger structures and provide new
functionality. Behavioral patterns are about identifying common communication patterns
between objects and realize these patterns. This seminar will talk about a specific creational
design pattern method, factory design pattern.
Literature Review
Many research articles and books have been written that examine the factory design pattern's
concept, use, benefits, and drawbacks. A few research papers and books were examined for this
seminar paper in order to thoroughly understand the industrial design pattern from many aspects.
The summaries for each can be found below.
Even if the factory design pattern makes the design more complicated, it also makes it more
adjustable, according to the writers Arbana Kadriu and Meral Memeti in their study article The
Factory Design Pattern [2]. Other design patterns necessitate the creation of new classes,
whereas the factory design pattern just necessitates the creation of new actions.

The author Dawid Rafa Ireno writes in his work Dynamic Factory - New Possibilities For
Factory Design Pattern [3] that design pattern knowledge has not yet been entirely clarified as
technology advances. He decided to propose a new factory design named "Dynamic Factory"
based on his research and the necessity for a dynamical compilation of the pieces before
execution. The dynamic factory, often known as JIT (Just-In-Time), is used mostly in class and
method level compilation types, especially when a developer is unsure of the object kinds or
functionalities that will be restricted.
In the Increasing the reusability at the cost of understandability [5] research paper, the author
discusses that based on previous results that show that the understandability decreases when
one uses the factory method design pattern. The author concludes that the purpose of the Factory
Method design pattern in AOP is slightly changed compared to OOP. Instead of creating
factories,it only passes a reference to the necessary aspect.
The last publication is R.Subburaj, Gladman Jekese, and Chiedza Hwata's Impact of Object-
Oriented Design Patterns in Software Development [7] research study. Design patterns,
according to the authors, allow architectural level reuse by offering "blueprints" for defining,
composing, and evaluating important components in a software system.

Application

In modern computer languages, the factory pattern is perhaps the most widely utilized design
pattern. Of course, when we make a remark like this, the question that comes to mind is why?
The goal of any company is to develop loosely linked, highly scalable solutions in as little time
as feasible. To achieve this loose coupling, software engineers must conceal as many features of
the class's functions as feasible. To achieve great scalability, the program's elements should be
loosely connected, so that if we wish to add something new, the changes to the other parts of the
program should be minimal.
After studying the core notions of the factory design pattern, the following question to address is
when should we apply the factory design pattern. There are three main scenarios wherein the
factory design is relevant, according to two books, Dive into design patterns [6] and First Design
Patterns: a Brain-Friendly Guide [1].
 When the object dependencies are not known beforehand, in this case, the factory
separates the construction code from the code that uses the product. For example,
add a new creator subclass to add a new subclass and override the factory method
on it.
 When a framework or library is provided to users, there is a way to extend the
components. For example, a UI framework provides square-shaped buttons. If
round buttons are needed, the user creates a new subclass, RoundButtons, and tell
the main framework to use the RoundButtons subclass instead of the
SquareButtons default class.
 When the user reuses the existing objects instead of rebuilding them each time,
for the program, this means faster time to market and saving system resources.

Implementation
The factory method pattern is also known as the virtual constructor pattern. This factory pattern is part of the core
Java libraries. For example, java.util.Calender.getInstance() and
java.nio.charset.Charset.forName().
The general structure of the Factory Design Pattern is shown below,
 Product declares the interface, which is common to all objects produced by the creator
and its subclasses.
 Products show the different implementations of the interface.
 The Creator class declares the factory method that returns new product objects.
 Concrete Creators override the base factory method, so it returns a different type of
Product.
After reviewing the general factory design pattern, lets assume we're developing a logistics
management application to track and handle all of the packages. Because the company is new
and solely provides land transportation using trucks, the code only has one class, the Truck class.
After receiving numerous requests for ship transportation over the course of a year, the company
decided to include the Ship service in its application as well. Great news for the company, but
what about the application? Of course not! The application is highly coupled from the Truck
class; therefore, merely adding a new class in the program will not work. Moreover, if later, the
company decides to add another transportation method, for example, aerial one by planes. The
code will be a total mess. So, the question is, what the best solution for the application is? Of
course, the factory design pattern! In the definition, we mentioned that this pattern intends to
define an interface for creating an object but lets subclasses decide which class to instantiate [5].
The same logic will be applied in our example as well. So, we create an interface called
Transport, and we create two subclasses (Truck and Ship) that implement the interface
Transport.
The figure below shows the uml diagram of the logistic management system
Below, there is the code for this example.
//Interface
public interface Transport {
public void deliver();
}
//Subclasses that implement the interface
public class Ship implements Transport {
public void deliver()
{
System.out.println("The package will be delivered by Ship!");
}
}
public class Truck implements Transport {
public void deliver()
{
System.out.println("The package will be delivered by Truck!");
}
}
//Factory Class
public class TransportFactory {
public static Transport getTransport(String criteria)
{
if ( criteria.equals("sea") )
return new Ship();
else if ( criteria.equals("land") )
return new Truck();
return null;
}
}
public class FactoryDeliveryDemo {
public static void main(String[] args) {
// create a sea delivery
Transport transport = TransportFactory.getTransport("sea");
transport.deliver();
// create a land delivery
transport = TransportFactory.getTransport("land");
transport.deliver();
}}

Pros and cons

 You avoid tight coupling between the creator and the concrete products.
  Single Responsibility Principle. You can move the product creation code into one
place in the program, making the code easier to support.
  Open/Closed Principle. You can introduce new types of products into the
program without breaking existing client code.

Cons

  The code may become more complicated since you need to introduce a lot of
new subclasses to implement the pattern. The best case scenario is when you’re
introducing the pattern into an existing hierarchy of creator classes.

Conclusions
Factory Design is a commonly used Design Pattern that is included in the Java frameworks. The Factory Design
Pattern allows developers to prevent code coupling between the creator and the products. Another big advantage of
the factory design pattern is that the product generation code can be moved to a single location in the application,
making it easier to maintain and support. Another fantastic aspect is that, while the factory design pattern makes the
design more sophisticated, it also makes it more configurable, as noted in The Factory Design Pattern research
report [2]. Furthermore, as demonstrated in the first example, adding new goods to the program is significantly
easier and does not require breaking existing code. However, as described in the Dive into Design Patterns book [6],
the code may become more complicated since the pattern requires numerous subclasses to implement. As a result,
it's possible that the optimum scenario is to include the pattern into an existing hierarchy. Knowing when and how to
employ the Factory Design Pattern is critical.
References
[1] Eric Freeman, Elisabeth Robson First Design Patterns: a Brain-Friendly Guide. - O’Reilly, Edition:
10th Anniversary (2014) 110-164
[2] Arbana Kadriu, Meral Mehmeti The Factory Design Pattern - Research Paper (2017)
[3] Rafał Ireno The Dynamic Factory - New Possibilities For Factory Design Pattern - Conference
Paper (2014)
[4] Erich Gamma Design Patterns: Elements of Reusable Object-Oriented Software - Addison Wesley
(1998)
[5] Linkping University Factory method - Increasing the reusability at the cost of understandability -
Research Paper (2018)
[6] Alexander Shvets Dive into design patterns - Refactoring.Guru (2019)
[7] R.Subburaj Professor, Gladman Jekese, Chiedza Hwata Impact of Object-Oriented Design
Patterns in Software Development - Research Paper (2015)

You might also like