Let's Practise
1. Match the patterns
Pattern
Application Based Questions
7. Solve
a. Ayushi covered one-fifth of a 100 m race in
30 seconds. Find the distance covered by Ayushi
b. Bhammini studied 1 hour more than half of the
time from 6 pm, to midnight. How many hours
did she study
c. Janavi's toy car runs at half the speed of
Jenevive's toy car. If the difference between
the speeds of Janavi's and Jenevive's cars is
13 km/hr, find the speed of both of their cars,
d. Somu's
age
is half the
age of his sister. If after
twelve years his sister will become 26 years
old, find the present ages of Somu and his sister.
(Hint: Find his sister's age first.)
b.
d.
2. Write algebraic e
situations.
a. 8 is subtracted
Answers
Factory is a creational design pattern which is used to create different implementation objects of the same type. Let’s consider a simple example below:
You have to design a logger API (not a web service API but a class level method ) which can log to different mediums like:
In memory data structure.
File on disk.
Database ( local or remote ).
Remote storage service like Amazon S3 or Google Cloud Storage.
Say you have planned to implement the following API:
class Logger {
public void log(String message, String loggerMedium) {}
}
where loggerMedium can be MEMORY, FILE, DB, REMOTE_SERVICE .
Till now it’s all fine. But how do you implement the logic in the back end?
In a naive way you can implement like following:
What happens when you add a different storage medium like FLASH_DRIVE? You will write another method & another if else in the class? With more numbers of mediums, the class is going to explode.
Is there any better way to design the above logic?
Another way is to have separate classes for different storage mediums & patch them into the Logger class. Like below:
In this implementation, we have separated out the individual code into their corresponding files, but our Logger class is tightly coupled with the instances of the storage mediums like FileLog, DBLog etc. With more additions of storage medium, more instance will be introduced in the Logger class.
Any other better way?
Since all storage mediums are just doing the same work in a different way, we can define a common contract which all the storage implementations follow & the Logger class just knows that it has to invoke that contract to get that work done. Let’s define that contract using interface:
Now all storage medium classes will implement this interface to conform to the contract:
In the Logger class, now you don’t need to have tightly coupled instances of there classes. Since all of them implement the same contract, you just need to be able to choose the most matching instance depending on the parameter passed to the Logger. This is where Factory pattern comes into picture — depending on the parameter passed, choose an implementation dynamically at the run time. Let’s define a factory class which can help you do so:
LoggerFactory helps to choose the proper storage instance by examining the parameter — loggerMedium passed to the getInstance method. Our Logger class just have to use it & pass the parameter:
The code has become very uniform here, responsibility of creating actual storage instances have been shifted to LoggerFactory, individual storage classes just implement how they are going to log the message to their particular medium & finally the Logger class just concerns about getting the appropriate storage instance through the LoggerFactory & delegates the actual logging to the implementation. In this way, the code is very loosely coupled. You want to add a new storage medium like FLASH_DRIVE, just create a new class which implements LoggingOperation interface & register it to the LoggerFactory with proper parameter. This is how factory pattern can help you dynamically choosing an implementation.