Right Money Management App For Your Financial Goals
Gang of Four in Java
1. GoF
Gang of Four (Design Patterns) in Java
Mina Tafreshi
Software Engineer @ Bol.com
2. What is a design pattern?
● Design pattern is a general repeatable solution to common problems in
software design.
● A design pattern is not a finished design.
● These patterns are mostly “evolved” rather than “discovered”.
[Note]: Code implementation is your responsibility!
3. Gang of Four Design Patterns
● Name?
Element of Reusable
Object-Oriented Software.
● By whom?
Erich Gamma, Richard Helm,
Ralph Johnson and John Vlissides
● When?
1994
4. GoF Design Patterns categories
1. Creational design patterns:
It deal with object creation. They look at ways to solve design issues arising out of creation of objects.
2. Structural design patterns:
Ease the design by identifying a simple way to realize relationships between entities.
3. Behavioral design patterns:
Identify common communication patterns between objects and realize these patterns.
5. Singleton Design Pattern Ensures only one instance of an object is created.
Factory Design Pattern Creates objects without specifying the exact class
to create.
Abstract Factory Design
Pattern
Allows for the creation of objects without
specifying their concrete type.
Builder Design Pattern Separate the construction of a complex object
from its representation so that the same
construction process can create different
representations.
Prototype Design Pattern Creates a new object from an existing object.
6. Adapter Design Pattern Convert the interface of a class into another
interface the clients expect.
Bridge Design Pattern Decouple an abstraction from its implementation.
Composite Design Pattern Compose objects into tree structures to represent
part-whole hierarchies.
Decorator Design Pattern Attach additional responsibilities to an object
dynamically.
Facade Design Pattern Facade defines a higher-level interface that makes
the subsystem easier to use.
Flyweight Design Pattern Use sharing to support large numbers of
fine-grained objects efficiently.
Proxy Design Pattern Provide a surrogate or placeholder for another
object to control access to it.
7. Command Design Pattern Encapsulate a request as an object.
Iterator Design Pattern Accesses the elements of an object sequentially
without exposing its underlying representation
Mediator Design Pattern Allows loose coupling between classes by being the
only class that has detailed knowledge of their
methods.
Memento Design Pattern Provides the ability to restore an object to its previous
state.
Observer Design Pattern publish/subscribe pattern which allows a number of
observer objects to see an event.
State Design Pattern Allow an object to alter its behaviour when its internal
state changes.
Strategy Design Pattern Define a family of algorithms, encapsulate each one,
and make them interchangeable.
Template Method Design Pattern lets subclasses redefine certain steps of an algorithm
without changing the algorithm's structure.
Delegation Pattern where an object expresses certain behavior to the
outside but in reality delegates responsibility for
implementing that behaviour to an associated object.
8. Singleton Design Pattern:
Ensures only one instance of an object is created
When:
A class in your program should have just a single instance available to all clients.
Why:
The most common reason for this is to control access to some shared resource.
Examples in Java core libraries:
● java.lang.Runtime#getRuntime()
● java.awt.Desktop#getDesktop()
● java.lang.System#getSecurityManager()
9. Factory Design Pattern:
Creates objects without specifying the exact class to create
When:
You don’t know beforehand the exact types and dependencies of the objects your code should work with.
Why:
The most common reason for this is to control access to some shared resource.
Examples in Java core libraries:
● java.util.Calendar#getInstance()
● java.util.ResourceBundle#getBundle()
● java.text.NumberFormat#getInstance()
● java.nio.charset.Charset#forName()
● java.net.URLStreamHandlerFactory#createURLStreamHandler(String)
● javax.xml.bind.JAXBContext#createMarshaller()
10. Builder Design Pattern:
Separate the construction of a complex object from its representation
When:
You want your code to be able to create different representations of some objects.
Why:
It lets you build objects step by step, using only those steps that you really need.
Examples in Java core libraries:
● java.lang.StringBuilder#append() (unsynchronized)
● java.lang.StringBuffer#append() (synchronized)
● java.nio.ByteBuffer#put()
( in CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)
● javax.swing.GroupLayout.Group#addComponent()
● All implementations java.lang.Appendable
11. Prototype Design Pattern:
Creates a new object from an existing object
When:
Your code shouldn’t depend on the concrete classes of objects that you need to copy
Why:
you want to reduce the number of subclasses that only differ in the way they initialize their respective objects
Examples in Java core libraries:
The Prototype pattern is available in Java out of the box with
a Cloneable interface.
Any class can implement this interface to become cloneable.
● java.lang.Object#clone()
(class should implement the java.lang.Cloneable interface)