OOP Design
Patterns
OOP Design Patterns
• Contents
• Patterns classification
• Observer
• Singleton
• Builder
• Chain of responsibility
• Proxy
• Decorator
• Template method
• Service Locator
• Conclusion
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.
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
Observer diagram
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()
UML diagram
Singleton code example
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
UML diagram
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
UML Diagram
Chain of responsibility
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.
UML Diagram
Builder
• Concept
• Builder use for dealing with constructors that require too many
parameters.
• Using
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()
• 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()
UML diagram
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)
UML diagram
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.
UML Diagram
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;
Thank you for attention
The end
USA HQ
Toll Free: 866-687-3588
Tel: +1-512-516-8880
Ukraine HQ
Tel: +380-32-240-9090
Bulgaria
Tel: +359-2-902-3760
Germany
Tel: +49-69-2602-5857
Netherlands
Tel: +31-20-262-33-23
Poland
Tel: +48-71-382-2800
UK
Tel: +44-207-544-8414
EMAIL
info@softserveinc.com
WEBSITE:
www.softserveinc.com
THE END

PresentationPatterns_v2

  • 1.
  • 2.
    OOP Design Patterns •Contents • Patterns classification • Observer • Singleton • Builder • Chain of responsibility • Proxy • Decorator • Template method • Service Locator • Conclusion
  • 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 • Inthis 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
  • 5.
  • 7.
    Singleton • Usage • Aparticular 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()
  • 8.
  • 9.
  • 11.
    Decorator • Concept • Attachadditional 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
  • 12.
  • 14.
    Proxy • Concept • Wewant 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
  • 15.
  • 17.
  • 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.
  • 19.
  • 21.
    Builder • Concept • Builderuse for dealing with constructors that require too many parameters. • Using
  • 23.
    Prototype • Concept • Specifythe kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. • Using • Java.lang.Object#clone()
  • 24.
    • Concept • Definethe 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()
  • 25.
  • 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)
  • 28.
  • 29.
    Service Locator • Isit 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.
  • 30.
  • 31.
    Conclusions • OOP Patternscan make your life and code better. • All of them need a lot of practice. • Be careful with using patterns. • Avoid antipatterns;
  • 32.
    Thank you forattention
  • 33.
  • 34.
    USA HQ Toll Free:866-687-3588 Tel: +1-512-516-8880 Ukraine HQ Tel: +380-32-240-9090 Bulgaria Tel: +359-2-902-3760 Germany Tel: +49-69-2602-5857 Netherlands Tel: +31-20-262-33-23 Poland Tel: +48-71-382-2800 UK Tel: +44-207-544-8414 EMAIL info@softserveinc.com WEBSITE: www.softserveinc.com THE END