July 29, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

A Survey of Common Design Patterns

  • November 18, 2002
  • By James Maioriello
  • Send Email »
  • More Articles »

Template Method

The Template Method is a simple pattern. You have an abstract class that is the base class of a hierarchy, and the behavior that is common to all objects in the hierarchy is implemented in the abstract class. Other details are left to the individual subclasses. The Template pattern is basically a formalism of the idea of defining an algorithm in a class but leaving some of the details to be implemented in subclasses. Another way to think of the Template Method is that it allows you to define a skeleton of an algorithm in an operation and defer some of the steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

The next class diagram shows the Template method simply consists of an abstract class and subclasses. These classes define a set of primitive behaviors that make up a specific piece of functionality in the application. Behaviors common to all objects (TemplateMethod(..)) are implemented in the abstract class. Subclasses are free to provide an alternate implementation (overriding a method), or to provide methods specific to a particular concrete class (such as Step1(...) and Step2(...)).

Visitor

The Visitor pattern uses an external class to act on data in other classes. This is a useful approach to you when you have a polymorphic operation that cannot reside in the class hierarchy. Visitor is also a useful way to extend the behavior of a class hierarchy without the need to alter existing classes or to implement the new behavior in every subclass that requires it.

The following class diagram shows that the Visitor pattern consists of two different types of classes, Elements and Visitors. Elements are the objects your application uses and Visitors are encapsulated behaviors that are required for each concrete Element. Each Element must implement the Accept( ) method, which takes Visitor as an argument. In addition, each Visitor must provide an implementation of the VisitElementX( ) method for each element in the object structure. The client acts to instantiate the element class and the visitor class. The client then passes the visitor object to the element's Accept( ) method, and then calls the elements methods to have it perform operations. The actual action that gets performed is determined by which visitor class was instantiated by the client.

James Maioriello, Ph.D., is a Senior Design Engineer for Formation Systems Inc., a corporation located in Massachusetts dedicated to the development of software systems for product development in the processing industries. His position allows him to use his expertise and skills to design solutions for chemists and other scientists so they can focus their talents on innovation. Dr. Maioriello obtained degrees in Chemistry from Lehigh University and in Software Engineering from Polytechnic University in New York.





Page 5 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel