Design Patterns
Pattern is something that occurs in a definite order. Sometimes we use a particular way to tackle a problem and that becomes a Pattern.
In technical terms, the design patterns are
language-independent strategies for solving common object-oriented design
problems. Design patterns represent the best practices and it should be
used in the development to provide common solution to a specific problem.
Why should we use design patterns?
·
It provides some popular solutions to coding
problems.
·
Learning design patterns speeds up your
experience accumulation in OOA/OOD.
·
Design patterns describe how objects communicate
without become entangled in each other’s data models and methods. For an
example:-
·
Design patterns provide a standard terminology
and are specific to particular scenario. For example, a singleton design pattern
signifies use of single object so all developers familiar with single design
pattern will make use of single object and they can tell each other that
program is following a singleton pattern.
Creational Patterns
These are ones that create objects for you,
rather than having you instantiate objects directly. This gives your program more flexibility in deciding
which objects need to be created for a given case.
Creational Patterns can be categorized as:
Factory Pattern
In factory pattern, we create object without
exposing the creation logic to the client and refer to newly created
object using a common interface.
In simple words, if we have a super class and
n sub-classes, and based on data provided, we have to return the object of one of the sub-classes,
we use a factory pattern.
Real Life Example
I just love to eat and I believe many of us like to try different cuisines.
Don’t get confused I am on Track. So when you go to a restaurant waiter comes
and ask “What would you like to have miss? (With a big smile). We do offer Chinese,
continental, Italian” and depending upon your reply “I would like to have Chinese”,
Waiter provides you Chinese menu. Is it not simple to understand?
When to use Factory Pattern?
- · When a class does not know which class of objects it must create.
- · A class specifies its sub-classes to specify which objects to create.
- · In programmer’s language, you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.
Advantages of Factory Pattern
·
Factory class removes the dependency on the
specific product classes generated by the Factory from the rest of the system.
Hence it is based on loose coupling.
·
The system is only dependent on the single
interface type that is generated by the Factory.
·
Loosely coupled or decoupled code is easier to
test or reuse since the code has fewer dependencies on other code.
Disadvantages of Factory Pattern
- If we add any new product (cuisine), we need a new else if statement in GetCuisines method of FactoryDemo class. This violates open/closed design principle.
- We can avoid modifying the Factory class by using sub classing. But sub classing means replacing all the factory class references everywhere through the code.
- We have tight coupling between Factory class and products.
Personal Experience:
When I was in my
training period, I was full of confusion. Why should I use a design pattern? I
mean what’s the need? Thanks to my mentor who introduced me to the concept of design
patterns.
I had to develop
PICBUZZ. My life 1st project that I did sincerely . PICBUZZ is an
Artist photo sharing website. All goes well but at last the day came when I had
to implement search functionality. Use case was to implement a search functionality
on the basis of inputs provided. A user can search a photograph on the basis of
Artist name, Album name and Tag name. So I came to know about factory pattern which
solved my problem(efficiently) in a second. So this is my short journey of Factory Pattern.
Friends I personally believe our experience teaches us many things. So stay
connected to my blogs to know more about design patterns.
Very good effort Madhuri!
ReplyDeleteAnother way to look at design patterns is to visualize them as "Design reusability" just like "code reusability". i.e. just the way we reuse coding solutions already provided by someone for business problems, design patterns let you reuse design solutions.