3. Patterns classification
• Creational
• In software engineering, creational design patterns are design
patterns that deal with object creation mechanisms, trying to create
objects in a manner suitable to the situation.
• Structural
• In software engineering, structural design patterns are design
patterns that ease the design by identifying a simple way to realize
relationships between entities.
• Behavioral
• In software engineering, behavioral design patterns are design
patterns that identify common communication patterns between
objects and realize these patterns. By doing so, these patterns
increase flexibility in carrying out this communication.
4. Observer
• Usage
• In this pattern, there are many observers (objects) which are
observing a particular subject (object).
• Computer example
• java.util.Observable
(rarely used in real world though)
• javax.servlet.http
.HttpSessionBindingListener
7. Singleton
• Usage
• A particular class should have only one instance. We will use only
that instance whenever we are in need.
• Computer example
• In a software system sometimes we may decide to use only one
file system. Usually we may use it for the centralized management
of resources. Connection Manager.
java.lang.Runtime#getRuntime()
java.awt.Desktop#getDesktop()
java.lang.System#getSecurityManager()
11. Decorator
• Concept
• Attach additional responsibilities to an object dynamically. Decorators
provide a flexible alternative to subclassing for extending functionality
• Using
• The pattern is useful when a creational algorithm of a complex object
is independent of the assembly of the parts of the object. The
construction process is also capable of building a different
representation of that object under consideration.
• All subclasses of java.io.inputStream, Reader …
• Javax.servlet.HttoServeltRequestWrapper
14. Proxy
• Concept
• We want to use a class which can perform as an interface to
something else.
• General type of proxy
• Remote proxies. They will hide that actual object which is in a
different address space.
• Protection proxy. It limits permission to
functional.
• Virtual proxy create lazy loading of object
• Smart reference proxy add additional
functional
18. Chain of responsibility
• Concept
• Avoid coupling the sender of a request to its receiver by giving
more than one object a chance to handle the request. Chain the
receiving objects and pass the request along the chain until an
object handles it.
• Using
• Chain of responsibility pattern is used to achieve loose coupling in
software design where a request from client is passed to a chain of
objects to process them.
23. Prototype
• Concept
• Specify the kinds of objects to create using a prototypical instance,
and create new objects by copying this prototype.
• Using
• Java.lang.Object#clone()
24. • Concept
• Define the skeleton of an algorithm in an operation, deferring some
steps to subclasses. The template method lets subclasses redefine
certain steps of an algorithm without changing the algorithm’s
structure.
• Using
• java.util.Comparator#compare(), executed by among others
Collections#sort().
• javax.servlet.http.HttpServlet, the service() and all doXXX() methods
take HttpServletRequest and HttpServletResponse and the
implementor has to process them (and not to get hold of them as
instance variables!).
• javax.servlet.Filter#doFilter()
27. Factory method
• Concept
• Define an interface for creating an object, but let subclasses decide
which class to instantiate. The factory method lets a class defer
instantiation to subclasses.
• Using
• java.util.Calendar#getInstance()
• java.util.ResourceBundle#getBundle()
• java.text.NumberFormat#getInstance()
• java.nio.charset.Charset#forName()
• java.net.URLStreamHandlerFactory#createURLStreamHandler(String)
(Returns singleton object per protocol)
29. Service Locator
• Is it antipattern?
• Context
• Service lookup and creation involves complex interfaces and network
operations.
• Using
• EJB clients need to use the JNDI API to look up EJBHome objects by
using the enterprise bean's registered JNDI name.
• JMS clients need to use JNDI API to look up JMS components by
using the JNDI names registered for JMS components, such as
connection factories, queues, and topics.
31. Conclusions
• OOP Patterns can make your life and code better.
• All of them need a lot of practice.
• Be careful with using patterns.
• Avoid antipatterns;