template pattern

21
Template design Pattern

Upload: vithushan-vinayagamoorthy

Post on 15-Apr-2017

105 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Template pattern

Template design

Pattern

Page 2: Template pattern

Types of Pattern

Creational: address problems of creating an object in a flexible way. Separate creation, from operation/use.

Structural: address problems of using O-O constructs like inheritance to organize classes and objects

Behavioral: address problems of assigning responsibilities to classes. Suggest both static relationships and patterns of communication(use cases)

Page 3: Template pattern

What is behavioral pattern? 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.

- Wikipedia -

Page 4: Template pattern

Some Examples of template pattern Chain of responsibility Command Interpreter Iterator Mediator Memento Null Object Observer State Strategy Template method Visitor

Page 5: Template pattern

Template method pattern

The Template Method defines a skeleton of an algorithm in an operation, and defers some steps to subclasses.

Page 6: Template pattern

When Would We Use This Pattern?

When behavior of an algorithm can vary, we let subclasses implement the behavior through overriding

We want to avoid code duplication, implementing variations of the algorithm in subclasses

we want to control the point that sub classing is allowed.

Page 7: Template pattern

Structure of template pattern

Page 8: Template pattern

Real time example

Page 9: Template pattern

Similarities &Dissimilarities

Similarities All these coffee preparation method is same Final step is coffee is prepared

Page 10: Template pattern

Dissimilarities The coffee powder is different Each method has different in depend the client

taste

Page 11: Template pattern

Class diagram

Page 12: Template pattern
Page 13: Template pattern
Page 14: Template pattern
Page 15: Template pattern
Page 16: Template pattern

Advantages of Using The Template Method Design Pattern

1. No code duplication between the classes

2. Inheritance and Not CompositionWhenever a design pattern uses inheritance as a key element instead of composition, you need to consider the reason. To understand the reason, you need to fully understand the principle of favoring composition over inheritance as a general principle in good OOP. The principle's established because of certain advantages of composition over inheritance, especially the composition advantage of not breaking encapsulation. However, Gamma, Helm, Johnson and Vlissides (GoF) note that inheritance also has certain advantages. One such advantage is that when using subclasses where some but not all operations are overridden, modifying the reused implementation is easier. Because the Template Method design pattern does exactly that—uses some but not all operations that can be overridden to achieve flexibility—it incorporates a key advantage of inheritance over composition.

3. By taking advantage of polymorphism the super class automatically calls the methods of the correct subclasses.

Page 17: Template pattern

Disadvantages of Template design pattern Communicates intent poorly – The template method pattern is often used as part of the effective

API in some mini-framework where the framework user is expected to subclass the template class. My experience has been that it is difficult to communicate that usage intent to users of the framework. Often the template class has some non-private methods that are exposed for use by the framework but are not intended to be used by the framework user, some that are intended to be overridden, and some that are both. Also, you may need to say whether the super’s version of the method can, should, or must be called. Communicating all that clearly is impossible in an API of any complexity.

Difficult to compose functionality – When inheritance is used as the way to add new functionality, it becomes impossible to add functionality in more than one axis at the same time without defining more and more classes. So, say you have a ConnectionPool and you start by having an OracleConnectionPool and DB2ConnectionPool and so on. But then you also need the ability to create XAConnections sometimes. So you then need to create OracleXAConnectionPool and DB2XAConnectionPool and so on. The per-DB pools share some functionality and the XA pools share functionality but can’t inherit from both. This typically leads to a lot of duplication and an explosion in implementations.

Difficult to comprehend program flow – In my experience it takes very few levels of template methods and inheritance to make debugging or understand the sequence of method calls difficult (as few as 2 or 3). When template methods are really pushed (lots of abstract methods at multiple levels), it can become painful to debug this kind of a system.

Difficult to maintain – Having maintained a couple chunks of code that made extensive use of the template method, it can be challenging. This kind of system can rapidly become fragile. Changes at any one level can disturb operation above or below that level in the template methods. There is often a feeling of unpredictability when adding new functionality as it difficult to predict how behavior will change in all cases. You often also tend to build finer and finer tweaks by splitting the algorithmic parts of the template class and inserting more layers, thus exacerbating the problem.

Page 18: Template pattern

Template method Used in java API

All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.

All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.

javax.servlet.http.HttpServlet, all the doXXX() methods by default sends a HTTP 405 ‘Method Not Allowed’ error to the response. You’re free to implement none or any of them.

Page 19: Template pattern

Reference

http://snehaprashant.blogspot.com/2008/09/template-method-pattern.html

Google.com Slideshare.com Ramj2ee.blogspot.in http://www.javacodegeeks.com/2013/03/template-

method-design-pattern-in-java.html

Page 20: Template pattern
Page 21: Template pattern

THANK YO