Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.


Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to comment

  • Be the first to like this


  1. 1. OOP Design Patterns
  2. 2. OOP Design Patterns • Contents • Patterns classification • Observer • Singleton • Builder • Chain of responsibility • Proxy • Decorator • Template method • Service Locator • Conclusion
  3. 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. 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
  5. 5. Observer diagram
  6. 6. 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()
  7. 7. UML diagram
  8. 8. Singleton code example
  9. 9. 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, Reader … • Javax.servlet.HttoServeltRequestWrapper
  10. 10. UML diagram
  11. 11. 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
  12. 12. UML Diagram
  13. 13. Chain of responsibility
  14. 14. 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.
  15. 15. UML Diagram
  16. 16. Builder • Concept • Builder use for dealing with constructors that require too many parameters. • Using
  17. 17. 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()
  18. 18. • 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()
  19. 19. UML diagram
  20. 20. 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() • (Returns singleton object per protocol)
  21. 21. UML diagram
  22. 22. 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.
  23. 23. UML Diagram
  24. 24. 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;
  25. 25. Thank you for attention
  26. 26. The end
  27. 27. 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 WEBSITE: THE END