<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Smbuch</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Smbuch"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Smbuch"/>
	<updated>2026-05-09T17:54:23Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68371</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w19 is</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68371"/>
		<updated>2012-10-26T22:50:38Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: /* Problems Solved by this pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Template Method Pattern'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
A template is a previously set format that other applications use as a starting point, instead of redefining the format each time&amp;lt;ref&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;/ref&amp;gt;. As you can imagine, [http://en.wikipedia.org/wiki/Software_engineering Software Engineering] contains many such patterns in order to develop software and applications. One such pattern is the Template Method Pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;/ref&amp;gt;. The Template Method Pattern is a [http://en.wikipedia.org/wiki/Behavioral_pattern behavioral design pattern] that defines an algorithm skeleton in a base class, with abstract definitions that derived classes can override to provide concrete behavior. The skeleton is called the template method and the subclasses or derived classes implement the algorithm, sometimes with minor modifications. &lt;br /&gt;
&lt;br /&gt;
In object-oriented design, the base class defines the template by stating the steps of the algorithm using abstract methods that its derived classes implement in a more concrete manner. In this manner, the template method pattern stores the algorithm in one place while other classes implement it, which is good object-oriented(OO) design. Also, the designer can decide which of the algorithm steps are to be standard and which of these are to be customizable&amp;lt;ref&amp;gt;http://sourcemaking.com/design_patterns/template_method&amp;lt;/ref&amp;gt;. These standard steps are implemented concretely in the derived classes using the abstract base class's definitions and the customizable steps are either implemented in the derived classes, or not at all. These customizable steps serve as placeholders, or ''hooks''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Hook_method#Hook_methods&amp;lt;/ref&amp;gt;, that the derived classes can supply or not, as they choose. So basically it is used when programmers want to fix the order of operations used in a method, but allow some amount of flexibility such that the subclasses can implement some of these operations in their own manner&amp;lt;ref&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The template method pattern thus, is fundamentally a code reuse technique. It is used whenever programmers want to localize common behavior in subclasses and place it in a super class to avoid code duplication. Hence it is an example of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring].&lt;br /&gt;
&lt;br /&gt;
==Problems Solved by this pattern==&lt;br /&gt;
Template Method pattern is a type of a behavioral design pattern which defines a skeleton for an algorithm which cannot be implemented in that class due to calls to methods that are abstract but can be implemented in the child classes. The template method cannot be overridden in the child classes as it is defined as a final method in the parent class.&lt;br /&gt;
&lt;br /&gt;
Suppose we have a portal where we allow users to book tickets to travel from source to destination. The problem we face here is that, we want all users to use a standard way of booking tickets and travelling. They could travel by bus, train or flight but we want them to follow a certain number of steps in the same order before they can see the confirmation page.  We do not want one user to pay first and then select a date and time and a flight and the other user to do this the other way round. However, the mode of transport for the travel may be different for every user and the source and destination may also be different for each of them. &lt;br /&gt;
&lt;br /&gt;
This is the ideal situation in which we would use the Template Method pattern. The parent class will have a template method defined with an algorithm skeleton that says that the steps to be followed will be select the mode of transport, then select the source, then destination and then the date of travel and finally mode of payment. &lt;br /&gt;
&lt;br /&gt;
However, the methods defining the selection of these functionalities can be implemented in different ways depending on what the subclass is. These methods in the parent class are kept abstract but the template method in the parent is made final. The template method calls the functions in a certain order.&lt;br /&gt;
&lt;br /&gt;
Let us show this with a concrete code.&lt;br /&gt;
&lt;br /&gt;
:public class Portal&lt;br /&gt;
::{&lt;br /&gt;
:::public final void TicketBooking()&lt;br /&gt;
:::{&lt;br /&gt;
      TransportMode();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      SourceAndDate();&lt;br /&gt;
      else&lt;br /&gt;
      DestinationAndDate();&lt;br /&gt;
      Payment();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
:abstract public MethodA();&lt;br /&gt;
:abstract public MethodB();&lt;br /&gt;
:abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  TransportMode() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  SourceAndDate() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  DestinationAndDate() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  Payment() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Each of the sub classes inheriting from this class can then provide a concrete definition to these abstract methods in a way they but the method template is final and must be used in the same way.&lt;br /&gt;
&lt;br /&gt;
==Structure and Protocol==&lt;br /&gt;
&lt;br /&gt;
==Roles and Responsibilities==&lt;br /&gt;
&lt;br /&gt;
==Benefits and Liabilities==&lt;br /&gt;
&lt;br /&gt;
==Comparison with other design patterns==&lt;br /&gt;
===Comparison with Adapter Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Adapter Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural patterns, the focus is on relationships between objects and how they together form larger components whereas in Behavioral patterns, the focus is on communication between objects and what each object does &amp;lt;ref name='StructuralBehavioral'&amp;gt;Design Patterns: Structural and Behavioral http://twu.seanho.com/09spr/cmpt166/lectures/35-patterns.pdf &amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! Enables the use of two or more unrelated interfaces, together, as one unit. This can be done by converting one (or more) of the interfaces to adapt to the type of interface that is required by the client. thus, adapter pattern acts as a Wrapper, thus, also called, Wrapper Pattern. &amp;lt;ref name=''&amp;gt;JAVA DESIGN PATTERNS http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm &amp;lt;/ref&amp;gt;&lt;br /&gt;
! This pattern is used to define and maintain an algorithm directed towards how you want the flow of execution to be. This template method is defined in the parent class and made final, so that all children have to follow the same flow.&lt;br /&gt;
! Purpose of Structural pattern is to act as a wrapper to make different interfaces ready for the client interface as against that of a Behavioral pattern is to define a common algorithm defining method as final in the parents class to act as a template for all the child classes to decide the flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! public class SquarePeg {&lt;br /&gt;
     public void insert(String str) {&lt;br /&gt;
       System.out.println(&amp;quot;SquarePeg insert(): &amp;quot; + str);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class RoundPeg {&lt;br /&gt;
     public void insertIntoHole(String msg) {&lt;br /&gt;
       System.out.println(&amp;quot;RoundPeg insertIntoHole(): &amp;quot; + msg);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
 public class PegAdapter extends SquarePeg {&lt;br /&gt;
     private RoundPeg roundPeg;&lt;br /&gt;
     public PegAdapter(RoundPeg peg)  {this.roundPeg = peg;}&lt;br /&gt;
     public void insert(String str)  {roundPeg.insertIntoHole(str);}&lt;br /&gt;
   }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Strategy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Behavioral patterns, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow fo control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! interface myInterface {&lt;br /&gt;
  int findSum(int x, inty, int z);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class A implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Proxy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural pattern, the focus is on creating ease of design by identifying a simple way to realize relationships between entities. In Behavioral patterns, on the other hand, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow of control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control. Purpose of the Proxy pattern is use a less complex object in place of the actual object until such a time that the actual object can be created. The less complex object is known as the proxy for the actual object.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! inteface myInterface {&lt;br /&gt;
public abstract void display();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classA implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadImage() {&lt;br /&gt;
System.out.println(&amp;quot;Loading in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
system.out.println(&amp;quot;Displaying in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classB implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadProxyImage(final Sting FILENAME) {&lt;br /&gt;
filename = FILENAME;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
if(image == null) {&lt;br /&gt;
   image = new classA(filename);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
    image.display();&lt;br /&gt;
}&lt;br /&gt;
} &lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Behavioral design patterns, generally identify the communication between different objects and classes and how this can be made most efficiently. The behavioral design pattern, Template Method pattern, is one of the most important patterns in certain situations like in cases where we need a static algorithm defined for some purpose. Whenever we do not want the child classes to make a decision about the flow of the algorithm or we do not want the child class interfering with the parents decision, it would be ideal to use this design pattern. It also gives you a very easy way to maintain your code and make changes to it in the future as everything needs to be done only in one place. In conclusion, Template Method pattern can be explained as easily as defining a skeleton of an algorithm in an operation, deferring some steps to sub classes.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68368</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w19 is</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68368"/>
		<updated>2012-10-26T22:49:19Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: /* Problems Solved by this pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Template Method Pattern'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
A template is a previously set format that other applications use as a starting point, instead of redefining the format each time&amp;lt;ref&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;/ref&amp;gt;. As you can imagine, [http://en.wikipedia.org/wiki/Software_engineering Software Engineering] contains many such patterns in order to develop software and applications. One such pattern is the Template Method Pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;/ref&amp;gt;. The Template Method Pattern is a [http://en.wikipedia.org/wiki/Behavioral_pattern behavioral design pattern] that defines an algorithm skeleton in a base class, with abstract definitions that derived classes can override to provide concrete behavior. The skeleton is called the template method and the subclasses or derived classes implement the algorithm, sometimes with minor modifications. &lt;br /&gt;
&lt;br /&gt;
In object-oriented design, the base class defines the template by stating the steps of the algorithm using abstract methods that its derived classes implement in a more concrete manner. In this manner, the template method pattern stores the algorithm in one place while other classes implement it, which is good object-oriented(OO) design. Also, the designer can decide which of the algorithm steps are to be standard and which of these are to be customizable&amp;lt;ref&amp;gt;http://sourcemaking.com/design_patterns/template_method&amp;lt;/ref&amp;gt;. These standard steps are implemented concretely in the derived classes using the abstract base class's definitions and the customizable steps are either implemented in the derived classes, or not at all. These customizable steps serve as placeholders, or ''hooks''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Hook_method#Hook_methods&amp;lt;/ref&amp;gt;, that the derived classes can supply or not, as they choose. So basically it is used when programmers want to fix the order of operations used in a method, but allow some amount of flexibility such that the subclasses can implement some of these operations in their own manner&amp;lt;ref&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The template method pattern thus, is fundamentally a code reuse technique. It is used whenever programmers want to localize common behavior in subclasses and place it in a super class to avoid code duplication. Hence it is an example of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring].&lt;br /&gt;
&lt;br /&gt;
==Problems Solved by this pattern==&lt;br /&gt;
Template Method pattern is a type of a behavioral design pattern which defines a skeleton for an algorithm which cannot be implemented in that class due to calls to methods that are abstract but can be implemented in the child classes. The template method cannot be overridden in the child classes as it is defined as a final method in the parent class.&lt;br /&gt;
&lt;br /&gt;
Suppose we have a portal where we allow users to book tickets to travel from source to destination. The problem we face here is that, we want all users to use a standard way of booking tickets and travelling. They could travel by bus, train or flight but we want them to follow a certain number of steps in the same order before they can see the confirmation page.  We do not want one user to pay first and then select a date and time and a flight and the other user to do this the other way round. However, the mode of transport for the travel may be different for every user and the source and destination may also be different for each of them. &lt;br /&gt;
&lt;br /&gt;
This is the ideal situation in which we would use the Template Method pattern. The parent class will have a template method defined with an algorithm skeleton that says that the steps to be followed will be select the mode of transport, then select the source, then destination and then the date of travel and finally mode of payment. &lt;br /&gt;
&lt;br /&gt;
However, the methods defining the selection of these functionalities can be implemented in different ways depending on what the subclass is. These methods in the parent class are kept abstract but the template method in the parent is made final. The template method calls the functions in a certain order.&lt;br /&gt;
&lt;br /&gt;
Let us show this with a concrete code.&lt;br /&gt;
&lt;br /&gt;
:public class Portal&lt;br /&gt;
::{&lt;br /&gt;
:::public final void TicketBooking()&lt;br /&gt;
:::{&lt;br /&gt;
      TransportMode();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      SourceAndDate();&lt;br /&gt;
      else&lt;br /&gt;
      DestinationAndDate();&lt;br /&gt;
      Payment();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
:abstract public MethodA();&lt;br /&gt;
:abstract public MethodB();&lt;br /&gt;
:abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  TransportMode() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  SourceAndDate() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  DestinationAndDate() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  Payment() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Structure and Protocol==&lt;br /&gt;
&lt;br /&gt;
==Roles and Responsibilities==&lt;br /&gt;
&lt;br /&gt;
==Benefits and Liabilities==&lt;br /&gt;
&lt;br /&gt;
==Comparison with other design patterns==&lt;br /&gt;
===Comparison with Adapter Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Adapter Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural patterns, the focus is on relationships between objects and how they together form larger components whereas in Behavioral patterns, the focus is on communication between objects and what each object does &amp;lt;ref name='StructuralBehavioral'&amp;gt;Design Patterns: Structural and Behavioral http://twu.seanho.com/09spr/cmpt166/lectures/35-patterns.pdf &amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! Enables the use of two or more unrelated interfaces, together, as one unit. This can be done by converting one (or more) of the interfaces to adapt to the type of interface that is required by the client. thus, adapter pattern acts as a Wrapper, thus, also called, Wrapper Pattern. &amp;lt;ref name=''&amp;gt;JAVA DESIGN PATTERNS http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm &amp;lt;/ref&amp;gt;&lt;br /&gt;
! This pattern is used to define and maintain an algorithm directed towards how you want the flow of execution to be. This template method is defined in the parent class and made final, so that all children have to follow the same flow.&lt;br /&gt;
! Purpose of Structural pattern is to act as a wrapper to make different interfaces ready for the client interface as against that of a Behavioral pattern is to define a common algorithm defining method as final in the parents class to act as a template for all the child classes to decide the flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! public class SquarePeg {&lt;br /&gt;
     public void insert(String str) {&lt;br /&gt;
       System.out.println(&amp;quot;SquarePeg insert(): &amp;quot; + str);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class RoundPeg {&lt;br /&gt;
     public void insertIntoHole(String msg) {&lt;br /&gt;
       System.out.println(&amp;quot;RoundPeg insertIntoHole(): &amp;quot; + msg);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
 public class PegAdapter extends SquarePeg {&lt;br /&gt;
     private RoundPeg roundPeg;&lt;br /&gt;
     public PegAdapter(RoundPeg peg)  {this.roundPeg = peg;}&lt;br /&gt;
     public void insert(String str)  {roundPeg.insertIntoHole(str);}&lt;br /&gt;
   }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Strategy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Behavioral patterns, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow fo control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! interface myInterface {&lt;br /&gt;
  int findSum(int x, inty, int z);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class A implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Proxy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural pattern, the focus is on creating ease of design by identifying a simple way to realize relationships between entities. In Behavioral patterns, on the other hand, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow of control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control. Purpose of the Proxy pattern is use a less complex object in place of the actual object until such a time that the actual object can be created. The less complex object is known as the proxy for the actual object.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! inteface myInterface {&lt;br /&gt;
public abstract void display();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classA implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadImage() {&lt;br /&gt;
System.out.println(&amp;quot;Loading in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
system.out.println(&amp;quot;Displaying in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classB implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadProxyImage(final Sting FILENAME) {&lt;br /&gt;
filename = FILENAME;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
if(image == null) {&lt;br /&gt;
   image = new classA(filename);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
    image.display();&lt;br /&gt;
}&lt;br /&gt;
} &lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Behavioral design patterns, generally identify the communication between different objects and classes and how this can be made most efficiently. The behavioral design pattern, Template Method pattern, is one of the most important patterns in certain situations like in cases where we need a static algorithm defined for some purpose. Whenever we do not want the child classes to make a decision about the flow of the algorithm or we do not want the child class interfering with the parents decision, it would be ideal to use this design pattern. It also gives you a very easy way to maintain your code and make changes to it in the future as everything needs to be done only in one place. In conclusion, Template Method pattern can be explained as easily as defining a skeleton of an algorithm in an operation, deferring some steps to sub classes.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68358</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w19 is</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68358"/>
		<updated>2012-10-26T22:47:31Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: /* Problems Solved by this pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Template Method Pattern'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
A template is a previously set format that other applications use as a starting point, instead of redefining the format each time&amp;lt;ref&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;/ref&amp;gt;. As you can imagine, [http://en.wikipedia.org/wiki/Software_engineering Software Engineering] contains many such patterns in order to develop software and applications. One such pattern is the Template Method Pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;/ref&amp;gt;. The Template Method Pattern is a [http://en.wikipedia.org/wiki/Behavioral_pattern behavioral design pattern] that defines an algorithm skeleton in a base class, with abstract definitions that derived classes can override to provide concrete behavior. The skeleton is called the template method and the subclasses or derived classes implement the algorithm, sometimes with minor modifications. &lt;br /&gt;
&lt;br /&gt;
In object-oriented design, the base class defines the template by stating the steps of the algorithm using abstract methods that its derived classes implement in a more concrete manner. In this manner, the template method pattern stores the algorithm in one place while other classes implement it, which is good object-oriented(OO) design. Also, the designer can decide which of the algorithm steps are to be standard and which of these are to be customizable&amp;lt;ref&amp;gt;http://sourcemaking.com/design_patterns/template_method&amp;lt;/ref&amp;gt;. These standard steps are implemented concretely in the derived classes using the abstract base class's definitions and the customizable steps are either implemented in the derived classes, or not at all. These customizable steps serve as placeholders, or ''hooks''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Hook_method#Hook_methods&amp;lt;/ref&amp;gt;, that the derived classes can supply or not, as they choose. So basically it is used when programmers want to fix the order of operations used in a method, but allow some amount of flexibility such that the subclasses can implement some of these operations in their own manner&amp;lt;ref&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The template method pattern thus, is fundamentally a code reuse technique. It is used whenever programmers want to localize common behavior in subclasses and place it in a super class to avoid code duplication. Hence it is an example of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring].&lt;br /&gt;
&lt;br /&gt;
==Problems Solved by this pattern==&lt;br /&gt;
Template Method pattern is a type of a behavioral design pattern which defines a skeleton for an algorithm which cannot be implemented in that class due to calls to methods that are abstract but can be implemented in the child classes. The template method cannot be overridden in the child classes as it is defined as a final method in the parent class.&lt;br /&gt;
&lt;br /&gt;
Suppose we have a portal where we allow users to book tickets to travel from source to destination. The problem we face here is that, we want all users to use a standard way of booking tickets and travelling. They could travel by bus, train or flight but we want them to follow a certain number of steps in the same order before they can see the confirmation page.  We do not want one user to pay first and then select a date and time and a flight and the other user to do this the other way round. However, the mode of transport for the travel may be different for every user and the source and destination may also be different for each of them. &lt;br /&gt;
&lt;br /&gt;
This is the ideal situation in which we would use the Template Method pattern. The parent class will have a template method defined with an algorithm skeleton that says that the steps to be followed will be select the mode of transport, then select the source, then destination and then the date of travel and finally mode of payment. &lt;br /&gt;
&lt;br /&gt;
However, the methods defining the selection of these functionalities can be implemented in different ways depending on what the subclass is. These methods in the parent class are kept abstract but the template method in the parent is made final. The template method calls the functions in a certain order.&lt;br /&gt;
&lt;br /&gt;
Let us show this with a concrete code.&lt;br /&gt;
&lt;br /&gt;
public class Portal {&lt;br /&gt;
public final void TicketBooking()&lt;br /&gt;
{&lt;br /&gt;
      TransportMode();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      SourceAndDate();&lt;br /&gt;
      else&lt;br /&gt;
      DestinationAndDate();&lt;br /&gt;
      Payment();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  TransportMode() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  SourceAndDate() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  DestinationAndDate() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  Payment() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Structure and Protocol==&lt;br /&gt;
&lt;br /&gt;
==Roles and Responsibilities==&lt;br /&gt;
&lt;br /&gt;
==Benefits and Liabilities==&lt;br /&gt;
&lt;br /&gt;
==Comparison with other design patterns==&lt;br /&gt;
===Comparison with Adapter Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Adapter Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural patterns, the focus is on relationships between objects and how they together form larger components whereas in Behavioral patterns, the focus is on communication between objects and what each object does &amp;lt;ref name='StructuralBehavioral'&amp;gt;Design Patterns: Structural and Behavioral http://twu.seanho.com/09spr/cmpt166/lectures/35-patterns.pdf &amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! Enables the use of two or more unrelated interfaces, together, as one unit. This can be done by converting one (or more) of the interfaces to adapt to the type of interface that is required by the client. thus, adapter pattern acts as a Wrapper, thus, also called, Wrapper Pattern. &amp;lt;ref name=''&amp;gt;JAVA DESIGN PATTERNS http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm &amp;lt;/ref&amp;gt;&lt;br /&gt;
! This pattern is used to define and maintain an algorithm directed towards how you want the flow of execution to be. This template method is defined in the parent class and made final, so that all children have to follow the same flow.&lt;br /&gt;
! Purpose of Structural pattern is to act as a wrapper to make different interfaces ready for the client interface as against that of a Behavioral pattern is to define a common algorithm defining method as final in the parents class to act as a template for all the child classes to decide the flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! public class SquarePeg {&lt;br /&gt;
     public void insert(String str) {&lt;br /&gt;
       System.out.println(&amp;quot;SquarePeg insert(): &amp;quot; + str);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class RoundPeg {&lt;br /&gt;
     public void insertIntoHole(String msg) {&lt;br /&gt;
       System.out.println(&amp;quot;RoundPeg insertIntoHole(): &amp;quot; + msg);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
 public class PegAdapter extends SquarePeg {&lt;br /&gt;
     private RoundPeg roundPeg;&lt;br /&gt;
     public PegAdapter(RoundPeg peg)  {this.roundPeg = peg;}&lt;br /&gt;
     public void insert(String str)  {roundPeg.insertIntoHole(str);}&lt;br /&gt;
   }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Strategy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Behavioral patterns, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow fo control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! interface myInterface {&lt;br /&gt;
  int findSum(int x, inty, int z);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class A implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Proxy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural pattern, the focus is on creating ease of design by identifying a simple way to realize relationships between entities. In Behavioral patterns, on the other hand, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow of control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control. Purpose of the Proxy pattern is use a less complex object in place of the actual object until such a time that the actual object can be created. The less complex object is known as the proxy for the actual object.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! inteface myInterface {&lt;br /&gt;
public abstract void display();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classA implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadImage() {&lt;br /&gt;
System.out.println(&amp;quot;Loading in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
system.out.println(&amp;quot;Displaying in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classB implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadProxyImage(final Sting FILENAME) {&lt;br /&gt;
filename = FILENAME;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
if(image == null) {&lt;br /&gt;
   image = new classA(filename);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
    image.display();&lt;br /&gt;
}&lt;br /&gt;
} &lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Behavioral design patterns, generally identify the communication between different objects and classes and how this can be made most efficiently. The behavioral design pattern, Template Method pattern, is one of the most important patterns in certain situations like in cases where we need a static algorithm defined for some purpose. Whenever we do not want the child classes to make a decision about the flow of the algorithm or we do not want the child class interfering with the parents decision, it would be ideal to use this design pattern. It also gives you a very easy way to maintain your code and make changes to it in the future as everything needs to be done only in one place. In conclusion, Template Method pattern can be explained as easily as defining a skeleton of an algorithm in an operation, deferring some steps to sub classes.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68349</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w19 is</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68349"/>
		<updated>2012-10-26T22:42:11Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: /* Problems Solved by this pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Template Method Pattern'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
A template is a previously set format that other applications use as a starting point, instead of redefining the format each time&amp;lt;ref&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;/ref&amp;gt;. As you can imagine, [http://en.wikipedia.org/wiki/Software_engineering Software Engineering] contains many such patterns in order to develop software and applications. One such pattern is the Template Method Pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;/ref&amp;gt;. The Template Method Pattern is a [http://en.wikipedia.org/wiki/Behavioral_pattern behavioral design pattern] that defines an algorithm skeleton in a base class, with abstract definitions that derived classes can override to provide concrete behavior. The skeleton is called the template method and the subclasses or derived classes implement the algorithm, sometimes with minor modifications. &lt;br /&gt;
&lt;br /&gt;
In object-oriented design, the base class defines the template by stating the steps of the algorithm using abstract methods that its derived classes implement in a more concrete manner. In this manner, the template method pattern stores the algorithm in one place while other classes implement it, which is good object-oriented(OO) design. Also, the designer can decide which of the algorithm steps are to be standard and which of these are to be customizable&amp;lt;ref&amp;gt;http://sourcemaking.com/design_patterns/template_method&amp;lt;/ref&amp;gt;. These standard steps are implemented concretely in the derived classes using the abstract base class's definitions and the customizable steps are either implemented in the derived classes, or not at all. These customizable steps serve as placeholders, or ''hooks''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Hook_method#Hook_methods&amp;lt;/ref&amp;gt;, that the derived classes can supply or not, as they choose. So basically it is used when programmers want to fix the order of operations used in a method, but allow some amount of flexibility such that the subclasses can implement some of these operations in their own manner&amp;lt;ref&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The template method pattern thus, is fundamentally a code reuse technique. It is used whenever programmers want to localize common behavior in subclasses and place it in a super class to avoid code duplication. Hence it is an example of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring].&lt;br /&gt;
&lt;br /&gt;
==Problems Solved by this pattern==&lt;br /&gt;
Template Method pattern is a type of a behavioral design pattern which defines a skeleton for an algorithm which cannot be implemented in that class due to calls to methods that are abstract but can be implemented in the child classes. The template method cannot be overridden in the child classes as it is defined as a final method in the parent class.&lt;br /&gt;
&lt;br /&gt;
Suppose we have a portal where we allow users to book tickets to travel from source to destination. The problem we face here is that, we want all users to use a standard way of booking tickets and travelling. They could travel by bus, train or flight but we want them to follow a certain number of steps in the same order before they can see the confirmation page.  We do not want one user to pay first and then select a date and time and a flight and the other user to do this the other way round. However, the mode of transport for the travel may be different for every user and the source and destination may also be different for each of them. &lt;br /&gt;
&lt;br /&gt;
This is the ideal situation in which we would use the Template Method pattern. The parent class will have a template method defined with an algorithm skeleton that says that the steps to be followed will be select the mode of transport, then select the source, then destination and then the date of travel and finally mode of payment. &lt;br /&gt;
&lt;br /&gt;
However, the methods defining the selection of these functionalities can be implemented in different ways depending on what the subclass is. These methods in the parent class are kept abstract but the template method in the parent is made final. The template method calls the functions in a certain order.&lt;br /&gt;
&lt;br /&gt;
Let us show this with a concrete code.&lt;br /&gt;
&lt;br /&gt;
==Structure and Protocol==&lt;br /&gt;
&lt;br /&gt;
==Roles and Responsibilities==&lt;br /&gt;
&lt;br /&gt;
==Benefits and Liabilities==&lt;br /&gt;
&lt;br /&gt;
==Comparison with other design patterns==&lt;br /&gt;
===Comparison with Adapter Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Adapter Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural patterns, the focus is on relationships between objects and how they together form larger components whereas in Behavioral patterns, the focus is on communication between objects and what each object does &amp;lt;ref name='StructuralBehavioral'&amp;gt;Design Patterns: Structural and Behavioral http://twu.seanho.com/09spr/cmpt166/lectures/35-patterns.pdf &amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! Enables the use of two or more unrelated interfaces, together, as one unit. This can be done by converting one (or more) of the interfaces to adapt to the type of interface that is required by the client. thus, adapter pattern acts as a Wrapper, thus, also called, Wrapper Pattern. &amp;lt;ref name=''&amp;gt;JAVA DESIGN PATTERNS http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm &amp;lt;/ref&amp;gt;&lt;br /&gt;
! This pattern is used to define and maintain an algorithm directed towards how you want the flow of execution to be. This template method is defined in the parent class and made final, so that all children have to follow the same flow.&lt;br /&gt;
! Purpose of Structural pattern is to act as a wrapper to make different interfaces ready for the client interface as against that of a Behavioral pattern is to define a common algorithm defining method as final in the parents class to act as a template for all the child classes to decide the flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! public class SquarePeg {&lt;br /&gt;
     public void insert(String str) {&lt;br /&gt;
       System.out.println(&amp;quot;SquarePeg insert(): &amp;quot; + str);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class RoundPeg {&lt;br /&gt;
     public void insertIntoHole(String msg) {&lt;br /&gt;
       System.out.println(&amp;quot;RoundPeg insertIntoHole(): &amp;quot; + msg);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
 public class PegAdapter extends SquarePeg {&lt;br /&gt;
     private RoundPeg roundPeg;&lt;br /&gt;
     public PegAdapter(RoundPeg peg)  {this.roundPeg = peg;}&lt;br /&gt;
     public void insert(String str)  {roundPeg.insertIntoHole(str);}&lt;br /&gt;
   }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Strategy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Behavioral patterns, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow fo control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! interface myInterface {&lt;br /&gt;
  int findSum(int x, inty, int z);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class A implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Proxy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural pattern, the focus is on creating ease of design by identifying a simple way to realize relationships between entities. In Behavioral patterns, on the other hand, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow of control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control. Purpose of the Proxy pattern is use a less complex object in place of the actual object until such a time that the actual object can be created. The less complex object is known as the proxy for the actual object.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! inteface myInterface {&lt;br /&gt;
public abstract void display();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classA implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadImage() {&lt;br /&gt;
System.out.println(&amp;quot;Loading in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
system.out.println(&amp;quot;Displaying in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classB implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadProxyImage(final Sting FILENAME) {&lt;br /&gt;
filename = FILENAME;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
if(image == null) {&lt;br /&gt;
   image = new classA(filename);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
    image.display();&lt;br /&gt;
}&lt;br /&gt;
} &lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Behavioral design patterns, generally identify the communication between different objects and classes and how this can be made most efficiently. The behavioral design pattern, Template Method pattern, is one of the most important patterns in certain situations like in cases where we need a static algorithm defined for some purpose. Whenever we do not want the child classes to make a decision about the flow of the algorithm or we do not want the child class interfering with the parents decision, it would be ideal to use this design pattern. It also gives you a very easy way to maintain your code and make changes to it in the future as everything needs to be done only in one place. In conclusion, Template Method pattern can be explained as easily as defining a skeleton of an algorithm in an operation, deferring some steps to sub classes.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68321</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w19 is</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68321"/>
		<updated>2012-10-26T22:12:41Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Template Method Pattern'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
A template is a previously set format that other applications use as a starting point, instead of redefining the format each time&amp;lt;ref&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;/ref&amp;gt;. As you can imagine, [http://en.wikipedia.org/wiki/Software_engineering Software Engineering] contains many such patterns in order to develop software and applications. One such pattern is the Template Method Pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;/ref&amp;gt;. The Template Method Pattern is a [http://en.wikipedia.org/wiki/Behavioral_pattern behavioral design pattern] that defines an algorithm skeleton in a base class, with abstract definitions that derived classes can override to provide concrete behavior. The skeleton is called the template method and the subclasses or derived classes implement the algorithm, sometimes with minor modifications. &lt;br /&gt;
&lt;br /&gt;
In object-oriented design, the base class defines the template by stating the steps of the algorithm using abstract methods that its derived classes implement in a more concrete manner. In this manner, the template method pattern stores the algorithm in one place while other classes implement it, which is good object-oriented(OO) design. Also, the designer can decide which of the algorithm steps are to be standard and which of these are to be customizable&amp;lt;ref&amp;gt;http://sourcemaking.com/design_patterns/template_method&amp;lt;/ref&amp;gt;. These standard steps are implemented concretely in the derived classes using the abstract base class's definitions and the customizable steps are either implemented in the derived classes, or not at all. These customizable steps serve as placeholders, or ''hooks''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Hook_method#Hook_methods&amp;lt;/ref&amp;gt;, that the derived classes can supply or not, as they choose. So basically it is used when programmers want to fix the order of operations used in a method, but allow some amount of flexibility such that the subclasses can implement some of these operations in their own manner&amp;lt;ref&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The template method pattern thus, is fundamentally a code reuse technique. It is used whenever programmers want to localize common behavior in subclasses and place it in a super class to avoid code duplication. Hence it is an example of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring].&lt;br /&gt;
&lt;br /&gt;
==Problems Solved by this pattern==&lt;br /&gt;
&lt;br /&gt;
==Structure and Protocol==&lt;br /&gt;
&lt;br /&gt;
==Roles and Responsibilities==&lt;br /&gt;
&lt;br /&gt;
==Benefits and Liabilities==&lt;br /&gt;
&lt;br /&gt;
==Comparison with other design patterns==&lt;br /&gt;
===Comparison with Adapter Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Adapter Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural patterns, the focus is on relationships between objects and how they together form larger components whereas in Behavioral patterns, the focus is on communication between objects and what each object does &amp;lt;ref name='StructuralBehavioral'&amp;gt;Design Patterns: Structural and Behavioral http://twu.seanho.com/09spr/cmpt166/lectures/35-patterns.pdf &amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! Enables the use of two or more unrelated interfaces, together, as one unit. This can be done by converting one (or more) of the interfaces to adapt to the type of interface that is required by the client. thus, adapter pattern acts as a Wrapper, thus, also called, Wrapper Pattern. &amp;lt;ref name=''&amp;gt;JAVA DESIGN PATTERNS http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm &amp;lt;/ref&amp;gt;&lt;br /&gt;
! This pattern is used to define and maintain an algorithm directed towards how you want the flow of execution to be. This template method is defined in the parent class and made final, so that all children have to follow the same flow.&lt;br /&gt;
! Purpose of Structural pattern is to act as a wrapper to make different interfaces ready for the client interface as against that of a Behavioral pattern is to define a common algorithm defining method as final in the parents class to act as a template for all the child classes to decide the flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! public class SquarePeg {&lt;br /&gt;
     public void insert(String str) {&lt;br /&gt;
       System.out.println(&amp;quot;SquarePeg insert(): &amp;quot; + str);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class RoundPeg {&lt;br /&gt;
     public void insertIntoHole(String msg) {&lt;br /&gt;
       System.out.println(&amp;quot;RoundPeg insertIntoHole(): &amp;quot; + msg);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
 public class PegAdapter extends SquarePeg {&lt;br /&gt;
     private RoundPeg roundPeg;&lt;br /&gt;
     public PegAdapter(RoundPeg peg)  {this.roundPeg = peg;}&lt;br /&gt;
     public void insert(String str)  {roundPeg.insertIntoHole(str);}&lt;br /&gt;
   }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Strategy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Behavioral patterns, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow fo control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! interface myInterface {&lt;br /&gt;
  int findSum(int x, inty, int z);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class A implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Proxy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural pattern, the focus is on creating ease of design by identifying a simple way to realize relationships between entities. In Behavioral patterns, on the other hand, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow of control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control. Purpose of the Proxy pattern is use a less complex object in place of the actual object until such a time that the actual object can be created. The less complex object is known as the proxy for the actual object.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! inteface myInterface {&lt;br /&gt;
public abstract void display();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classA implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadImage() {&lt;br /&gt;
System.out.println(&amp;quot;Loading in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
system.out.println(&amp;quot;Displaying in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classB implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadProxyImage(final Sting FILENAME) {&lt;br /&gt;
filename = FILENAME;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
if(image == null) {&lt;br /&gt;
   image = new classA(filename);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
    image.display();&lt;br /&gt;
}&lt;br /&gt;
} &lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Behavioral design patterns, generally identify the communication between different objects and classes and how this can be made most efficiently. The behavioral design pattern, Template Method pattern, is one of the most important patterns in certain situations like in cases where we need a static algorithm defined for some purpose. Whenever we do not want the child classes to make a decision about the flow of the algorithm or we do not want the child class interfering with the parents decision, it would be ideal to use this design pattern. It also gives you a very easy way to maintain your code and make changes to it in the future as everything needs to be done only in one place. In conclusion, Template Method pattern can be explained as easily as defining a skeleton of an algorithm in an operation, deferring some steps to sub classes.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68312</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w19 is</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68312"/>
		<updated>2012-10-26T21:55:18Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Template Method Pattern'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
A template is a previously set format that other applications use as a starting point, instead of redefining the format each time&amp;lt;ref&amp;gt;http://www.oodesign.com/template-method-pattern.html&amp;lt;/ref&amp;gt;. As you can imagine, [http://en.wikipedia.org/wiki/Software_engineering Software Engineering] contains many such patterns in order to develop software and applications. One such pattern is the Template Method Pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Template_method_pattern&amp;lt;/ref&amp;gt;. The Template Method Pattern is a [http://en.wikipedia.org/wiki/Behavioral_pattern behavioral design pattern] that defines an algorithm skeleton in a base class, with abstract definitions that derived classes can override to provide concrete behavior. The skeleton is called the template method and the subclasses or derived classes implement the algorithm, sometimes with minor modifications. &lt;br /&gt;
&lt;br /&gt;
In object-oriented design, the base class defines the template by stating the steps of the algorithm using abstract methods that its derived classes implement in a more concrete manner. In this manner, the template method pattern stores the algorithm in one place while other classes implement it, which is good object-oriented(OO) design. Also, the designer can decide which of the algorithm steps are to be standard and which of these are to be customizable&amp;lt;ref&amp;gt;http://sourcemaking.com/design_patterns/template_method&amp;lt;/ref&amp;gt;. These standard steps are implemented concretely in the derived classes using the abstract base class's definitions and the customizable steps are either implemented in the derived classes, or not at all. These customizable steps serve as placeholders, or ''hooks''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Hook_method#Hook_methods&amp;lt;/ref&amp;gt;, that the derived classes can supply or not, as they choose. So basically it is used when programmers want to fix the order of operations used in a method, but allow some amount of flexibility such that the subclasses can implement some of these operations in their own manner&amp;lt;ref&amp;gt;http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The template method pattern thus, is fundamentally a code reuse technique. It is used whenever programmers want to localize common behavior in subclasses and place it in a super class to avoid code duplication. Hence it is an example of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring].&lt;br /&gt;
&lt;br /&gt;
==Problems Solved by this pattern==&lt;br /&gt;
&lt;br /&gt;
==Structure and Protocol==&lt;br /&gt;
&lt;br /&gt;
==Roles and Responsibilities==&lt;br /&gt;
&lt;br /&gt;
==Benefits and Liabilities==&lt;br /&gt;
&lt;br /&gt;
==Comparison with other design patterns==&lt;br /&gt;
===Comparison with Adapter Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Adapter Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural patterns, the focus is on relationships between objects and how they together form larger components whereas in Behavioral patterns, the focus is on communication between objects and what each object does &amp;lt;ref name='StructuralBehavioral'&amp;gt;Design Patterns: Structural and Behavioral http://twu.seanho.com/09spr/cmpt166/lectures/35-patterns.pdf &amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! Enables the use of two or more unrelated interfaces, together, as one unit. This can be done by converting one (or more) of the interfaces to adapt to the type of interface that is required by the client. thus, adapter pattern acts as a Wrapper, thus, also called, Wrapper Pattern. &amp;lt;ref name=''&amp;gt;JAVA DESIGN PATTERNS http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm &amp;lt;/ref&amp;gt;&lt;br /&gt;
! This pattern is used to define and maintain an algorithm directed towards how you want the flow of execution to be. This template method is defined in the parent class and made final, so that all children have to follow the same flow.&lt;br /&gt;
! Purpose of Structural pattern is to act as a wrapper to make different interfaces ready for the client interface as against that of a Behavioral pattern is to define a common algorithm defining method as final in the parents class to act as a template for all the child classes to decide the flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! public class SquarePeg {&lt;br /&gt;
     public void insert(String str) {&lt;br /&gt;
       System.out.println(&amp;quot;SquarePeg insert(): &amp;quot; + str);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class RoundPeg {&lt;br /&gt;
     public void insertIntoHole(String msg) {&lt;br /&gt;
       System.out.println(&amp;quot;RoundPeg insertIntoHole(): &amp;quot; + msg);&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
 public class PegAdapter extends SquarePeg {&lt;br /&gt;
     private RoundPeg roundPeg;&lt;br /&gt;
     public PegAdapter(RoundPeg peg)  {this.roundPeg = peg;}&lt;br /&gt;
     public void insert(String str)  {roundPeg.insertIntoHole(str);}&lt;br /&gt;
   }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Strategy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Behavioral patterns, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow fo control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! interface myInterface {&lt;br /&gt;
  int findSum(int x, inty, int z);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class A implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Proxy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural pattern, the focus is on creating ease of design by identifying a simple way to realize relationships between entities. In Behavioral patterns, on the other hand, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow of control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control. Purpose of the Proxy pattern is use a less complex object in place of the actual object until such a time that the actual object can be created. The less complex object is known as the proxy for the actual object.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! inteface myInterface {&lt;br /&gt;
public abstract void display();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classA implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadImage() {&lt;br /&gt;
System.out.println(&amp;quot;Loading in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
system.out.println(&amp;quot;Displaying in classA&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class classB implements myInterface {&lt;br /&gt;
&lt;br /&gt;
private void loadProxyImage(final Sting FILENAME) {&lt;br /&gt;
filename = FILENAME;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void display() {&lt;br /&gt;
if(image == null) {&lt;br /&gt;
   image = new classA(filename);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
    image.display();&lt;br /&gt;
}&lt;br /&gt;
} &lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68082</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w19 is</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w19_is&amp;diff=68082"/>
		<updated>2012-10-24T05:18:30Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: Created page with &amp;quot;&amp;lt;big&amp;gt;'''Template Method Pattern'''&amp;lt;/big&amp;gt;  __TOC__  ==Introduction==  ==Problems Solved by this pattern==  ==Structure and Protocol==  ==Roles and Responsibilities==  ==Benefits a...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Template Method Pattern'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
==Problems Solved by this pattern==&lt;br /&gt;
&lt;br /&gt;
==Structure and Protocol==&lt;br /&gt;
&lt;br /&gt;
==Roles and Responsibilities==&lt;br /&gt;
&lt;br /&gt;
==Benefits and Liabilities==&lt;br /&gt;
&lt;br /&gt;
==Comparison with other design patterns==&lt;br /&gt;
===Comparison with Adapter Design Pattern===&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Adapter Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Structural Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Structural patterns, the focus is on relationships between objects and how they together form larger components whereas in Behavioral patterns, the focus is on communication between objects and what each object does &amp;lt;ref name='StructuralBehavioral'&amp;gt;Design Patterns: Structural and Behavioral http://twu.seanho.com/09spr/cmpt166/lectures/35-patterns.pdf &amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! Enables the use of two or more unrelated interfaces, together, as one unit. This can be done by converting one (or more) of the interfaces to adapt to the type of interface that is required by the client. thus, adapter pattern acts as a Wrapper, thus, also called, Wrapper Pattern. &amp;lt;ref name=''&amp;gt;JAVA DESIGN PATTERNS http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm &amp;lt;/ref&amp;gt;&lt;br /&gt;
! This pattern is used to define and maintain an algorithm directed towards how you want the flow of execution to be. This template method is defined in the parent class and made final, so that all children have to follow the same flow.&lt;br /&gt;
! Purpose of Structural pattern is to act as a wrapper to make different interfaces ready for the client interface as against that of a Behavioral pattern is to define a common algorithm defining method as final in the parents class to act as a template for all the child classes to decide the flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! &lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Comparison with Strategy Design Pattern===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Characteristics&lt;br /&gt;
! Strategy Design Pattern&lt;br /&gt;
! Template Method Design Pattern&lt;br /&gt;
! Explanation&lt;br /&gt;
|-&lt;br /&gt;
! Design Pattern Category&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! Behavioral Pattern&lt;br /&gt;
! In Behavioral patterns, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. &amp;lt;ref name='Behavioral'&amp;gt;Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern &amp;lt;/ref&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
! Purpose of Use&lt;br /&gt;
! The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement.&lt;br /&gt;
! This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow fo control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. &lt;br /&gt;
! Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control.&lt;br /&gt;
|-&lt;br /&gt;
! Example&lt;br /&gt;
! interface myInterface {&lt;br /&gt;
  int findSum(int x, inty, int z);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class A implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B implements myInterface {&lt;br /&gt;
  public int findSum(int x, int y, int z) {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
! public class Response&lt;br /&gt;
public final void import_response()&lt;br /&gt;
{&lt;br /&gt;
    template_method() { &lt;br /&gt;
      MethodA();&lt;br /&gt;
      if(int i &amp;lt; 4)&lt;br /&gt;
      MethodB();&lt;br /&gt;
      else&lt;br /&gt;
      MethodC();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
abstract public MethodA();&lt;br /&gt;
abstract public MethodB();&lt;br /&gt;
abstract public MethodC();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Feedback_response extends Response&lt;br /&gt;
{&lt;br /&gt;
  MethodA() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodB() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  MethodC() {&lt;br /&gt;
  //some definition&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
! In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.&lt;br /&gt;
&lt;br /&gt;
In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=67582</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=67582"/>
		<updated>2012-10-18T22:12:50Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w58 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w60 ac]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w62 rb]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w29 st]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch 2w30 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w17 pt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w31 up]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w9 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w19 is]]&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w45_is&amp;diff=66586</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w45 is</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w45_is&amp;diff=66586"/>
		<updated>2012-10-03T22:34:54Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Automated Refactoring'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Refactoring is a technique for restructuring existing code. It is defined as altering the code's internal structure without changing its external behavior. It is done as a series of small transformations which together produce significant restructuring. Since each transformation is small, it's less likely to go wrong. The system is also kept fully working after each small transformation, reducing the chances that a system can get seriously broken during the restructuring.&amp;lt;ref name=&amp;quot;refone&amp;quot;&amp;gt; Refactoring Home Page http://refactoring.com/ &amp;lt;/ref&amp;gt; When people make small changes to the code without fully understanding its design, the code loses its structure, which eventually has a cumulative effect. If the developer cannot understand the code design, then it becomes more difficult for him to preserve it. Timely refactoring helps the code to retain its shape. &amp;lt;ref name=&amp;quot;reftwo&amp;quot;&amp;gt; Why should you Refactor http://sourcemaking.com/refactoring/why-should-you-refactor &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Automated refactoring refers to refactoring which is under the developers control. What we mean by this is that, the system is not responsible for deciding what refactoring is required, rather this is the job of the developer. Automated refactoring is a feature of most widely used IDE's today, which helps expedite the task of refactoring for the developers. &amp;lt;ref name=&amp;quot;refthree&amp;quot;&amp;gt; Refactoring for everyone http://www.ibm.com/developerworks/library/os-ecref/ &amp;lt;/ref&amp;gt;  &lt;br /&gt;
In this article, we are aiming at describing the various techniques for automated refactoring. This article gives a clear understanding of these techniques which are illustrated with examples. We have also given a comparison of the refactoring functionality provided by various frequently used IDE's. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Common Refactoring Techniques==&lt;br /&gt;
===Extraction Refactoring=== &lt;br /&gt;
There are several refactoring techniques related to extraction: Extract method, Extract local variables, Extract constants. The first method &amp;quot;Extract method will create a new method from code you've selected. Extract Local Variable refactoring takes an expression that is being used repeatedly and assigns it to a local variable. Extract constant refactoring is to select a static, constant expression, which the refactoring will convert to a static final constant. This is useful for removing hard-coded numbers and strings from your code&lt;br /&gt;
&lt;br /&gt;
[[1) Extract method:]]&lt;br /&gt;
&lt;br /&gt;
'''Before refactoring'''                &lt;br /&gt;
:public void abc() {&lt;br /&gt;
::int x= 9;&lt;br /&gt;
::int y= 100;&lt;br /&gt;
::int z=x+y;&lt;br /&gt;
::int s=x +z;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
'''After refactoring'''&lt;br /&gt;
:public void abc() {&lt;br /&gt;
::int x=9;&lt;br /&gt;
::int y=50;&lt;br /&gt;
::int z= add(x,y);&lt;br /&gt;
::int s= add(x,z);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:private int add(int x, int y) {&lt;br /&gt;
::return x+y;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
[[2) Extract local variable:]]&lt;br /&gt;
This refactoring technique takes out an expression and then assigns it to a variable.&lt;br /&gt;
This variable is then used in place of expression everywhere in the code where the&lt;br /&gt;
expression is present. It has several advantages. First of all it gives meaningful&lt;br /&gt;
name to an expression which helps in understanding what an expression does. It&lt;br /&gt;
helps in debugging of code and it makes code efficient.&lt;br /&gt;
&lt;br /&gt;
'''Before refactoring'''&lt;br /&gt;
&lt;br /&gt;
:void xyz() {&lt;br /&gt;
::int i=12;&lt;br /&gt;
::int j= 6;&lt;br /&gt;
::int k=15;&lt;br /&gt;
::int p=10;&lt;br /&gt;
::p+=i+j+k;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
In order to perform refactoring in eclipse or any other tool first we select the&lt;br /&gt;
expression (here i+j+k) or any other tool, it asks for name of new variable name.&lt;br /&gt;
Suppose we give it q.&lt;br /&gt;
&lt;br /&gt;
'''After refactoring'''&lt;br /&gt;
&lt;br /&gt;
:void xyz() {&lt;br /&gt;
::int i=12;&lt;br /&gt;
::int j= 6;&lt;br /&gt;
::int k=15;&lt;br /&gt;
::int p=10;&lt;br /&gt;
::p+=q;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
[[3) Extract constant:]]&lt;br /&gt;
&lt;br /&gt;
'''Before refactoring'''&lt;br /&gt;
:void abc() {&lt;br /&gt;
::int a=100;&lt;br /&gt;
::int b=100;&lt;br /&gt;
::int z= a+b;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
Now if we want to extract the constant 100 we will select it and enter a name for it&lt;br /&gt;
say for example ‘num’. Now instead of using 100 we will use the variable ’num’&lt;br /&gt;
&lt;br /&gt;
'''After refactoring'''&lt;br /&gt;
&lt;br /&gt;
:void abc() {&lt;br /&gt;
::int a=num;&lt;br /&gt;
::int b=num;&lt;br /&gt;
::int c=num+num;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
===Inline Refactoring=== &lt;br /&gt;
This type refactoring is the reverse of local variable extraction, in which we remove a local variable and use the value of the variable(typically an expression) in the place where the variable is used. This can be marginally more efficient than using a temporary variable and, by making the code terser, makes it either easier to read or more cryptic, depending on your point of view&amp;lt;ref name=&amp;quot;reffour&amp;quot;&amp;gt; refactoring for everyone http://www.ibm.com/developerworks/library/os-ecref/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put in easy words, Inline refactoring is doing what an external method call does but without making the&lt;br /&gt;
actual call to an external method. This is as good as writing what a method does inline instead of calling&lt;br /&gt;
a method defined outside. Let us illustrate this with an example.&lt;br /&gt;
&lt;br /&gt;
'''Before Inline Refactoring'''&lt;br /&gt;
&lt;br /&gt;
:void funtion1(String msg) {&lt;br /&gt;
::Console.write(msg);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:void function2() {&lt;br /&gt;
::function1(“someString”);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
'''After Inline refactoring'''&lt;br /&gt;
&lt;br /&gt;
:void funtion2() {&lt;br /&gt;
::Console.write(“someString”);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
In this way we are shortening the code but can print statically only one value (in this case someString), each time.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;reffive&amp;quot;&amp;gt; Refactorings-Inline Method: http://www.skorkin.com/2011/02/refactorings-inline-method/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Encapsulating Fields===&lt;br /&gt;
Generally its not a good practice to expose the class attributes through public interface. Thus, Encapsulating such fields will make such fields as private or protected as appropriate and generate getters and setters for the associated field.&lt;br /&gt;
&lt;br /&gt;
Consider the following piece of code. This class has an attribute which is public and can be accessed by any object directly.&lt;br /&gt;
&lt;br /&gt;
'''Before Encapsulating Fields'''&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::public String attr1;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main(String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::Obj.attr1 = “var”;&lt;br /&gt;
:::System.out.println(“Value of attr1 is” + obj.attr1);&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
On making attr1 private, we need to add getter and setter method to access it outside the class. This can be done using automated refactoring technique called Encapsulating a field technique. Now the object obj can access the attribute attr1 only through the defined getter and setter method.&lt;br /&gt;
&lt;br /&gt;
'''After Encapsulating Fields'''&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::private String attr1;&lt;br /&gt;
&lt;br /&gt;
::public String getAttr1() {&lt;br /&gt;
:::return attr1;&lt;br /&gt;
::}&lt;br /&gt;
&lt;br /&gt;
::public setAttr1(String newAttr1Value) {&lt;br /&gt;
:::attr1 = newAttr1Value;&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main (String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::obj.setAttr1(“var”);&lt;br /&gt;
:::System.out .println(“Value of Attr1 is” + obj.getAttr1());&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
===Changing Method Signature===&lt;br /&gt;
Here we change the change the parameters, visibility, and return type of a method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Before Refactoring'''&lt;br /&gt;
Suppose our class Class1 is defined as below and it has a function funtion1 as shown:&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::public void funtion1(int a) {&lt;br /&gt;
::://do something&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
The method call to function1 will be as follows:&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main(String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::Obj.function1(5);&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''After Refactoring'''&lt;br /&gt;
On change the number of arguments in function1 to 2 instead of 1 the function call should also change. This is done by a concept called Automated refactoring technique for changing method signature.&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::public void funtion1(int a, double b) {&lt;br /&gt;
::://do something&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main(String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::Obj.function1(5, 0.0);&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
When performing the refactoring a default value of 0.0 will be passed to the function as a parameter.&lt;br /&gt;
&lt;br /&gt;
== Other Refactorings ==&lt;br /&gt;
&lt;br /&gt;
There are a multitude of refactorings.  Those that are included with RubyMine&amp;lt;ref name=&amp;quot;refsix&amp;quot;&amp;gt;RubyMine :: Ruby and Ruby on Rails IDE with smart code completion, syntax highlighting and refactoring http://www.jetbrains.com/ruby/features/ruby_ide.html#Refactorings&amp;lt;/ref&amp;gt; include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;[[Rename refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This refactoring technique allows renaming symbols. There&lt;br /&gt;
are several rename refactoring available. These are rename package, rename class,&lt;br /&gt;
rename method, rename variable etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Before refactoring'''&lt;br /&gt;
:class abc() {&lt;br /&gt;
::puts “hello”&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:def message() {&lt;br /&gt;
::a= new abc()&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
In order to rename the class ‘abc’ select it, choose rename option from the tool and give a new name.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''After refactoring'''&lt;br /&gt;
&lt;br /&gt;
:class xyz() {&lt;br /&gt;
::puts “hello”&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:def message() {&lt;br /&gt;
::a= new xyz()&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;[[Extract Module]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This refactoring technique creates a module from the methods of a ruby class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Before refactoring'''&lt;br /&gt;
&lt;br /&gt;
:class abc() {&lt;br /&gt;
::def total_cost(item1, item2) {&lt;br /&gt;
:::return(item1 +item2)&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''After refactoring:''' in the abc.rb file we will have&lt;br /&gt;
&lt;br /&gt;
:class abc()&lt;br /&gt;
::include total_cost&lt;br /&gt;
:end&lt;br /&gt;
&lt;br /&gt;
in total_cost.rb file we will have&lt;br /&gt;
&lt;br /&gt;
:module total_cost&lt;br /&gt;
&lt;br /&gt;
::def total_cost(item1, item2) {&lt;br /&gt;
:::return(item1 +item2)&lt;br /&gt;
::}&lt;br /&gt;
&amp;lt;li&amp;gt;[[Extract Superclass]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It extract certain methods from a class in a superclass.&lt;br /&gt;
For example if a class B contains two methods abc and xyz. We can do refactoring and move method xyz into superclass A.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Before refactoring'''&lt;br /&gt;
&lt;br /&gt;
:class B&lt;br /&gt;
::def abc&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
&lt;br /&gt;
::def xyz&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
:end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''After refactoring'''&lt;br /&gt;
&lt;br /&gt;
:class B&amp;lt;A&lt;br /&gt;
::def abc&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
&lt;br /&gt;
A.rb file&lt;br /&gt;
&lt;br /&gt;
::def xyz&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
&amp;lt;li&amp;gt;[[Introduce Variable]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is when an expression is replaced by a variable and the variable is used in place of the expression each time. A change to the expression will mean a change only at one place.&lt;br /&gt;
Eg. &lt;br /&gt;
:puts “My favorite team is #{get_team()}”&lt;br /&gt;
But if, ::myTeam = get_team();&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then, :puts “My favorite team is #{myTeam}”&lt;br /&gt;
&amp;lt;li&amp;gt;[[Introduce Constant]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is when a new constant is defined and initialized and that constant is used instead of a value.&lt;br /&gt;
Eg. :a = b +3; CONSTANT = 3; a = b + CONSTANT;&lt;br /&gt;
&amp;lt;li&amp;gt;[[Introduce Field]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is when a new field is defined and initialized and then that field is used instead of&lt;br /&gt;
the value.&lt;br /&gt;
Eg. :a = b + 3; @field = 3; a = b + @field;&lt;br /&gt;
&amp;lt;li&amp;gt;[[Introduce Parameter]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This type simply adds a new parameter to a function and updates the method calls according to that.&lt;br /&gt;
Eg.&lt;br /&gt;
:Method signature changes from def function1() to def function1(pass)&lt;br /&gt;
:Method call will change from a=function1() to a=funtion1(something)&lt;br /&gt;
&amp;lt;li&amp;gt;[[Inline Variable]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This works similar to Inline refactoring explained above. &lt;br /&gt;
Eg. Instead of writing a variable’s value to console, we will write a constant to the console, which is basically hard coding the value written to the console.&lt;br /&gt;
&amp;lt;li&amp;gt;[[Pull Up Members]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Refers to moving a field x from a class A to its superclass. Thus, x will be moved out of all of A’s siblings and will have just one copy in A’s superclass.&lt;br /&gt;
Eg. If ‘type’ is an attribute of class Leopard and class Cheetah, then moving ‘type’ to super class of Leopard and Cheetah, that is, class Cat will result into one copy of ‘type’ being in class Cat and ‘type’ will get removed from both Cheetah and Leopard.&lt;br /&gt;
&amp;lt;li&amp;gt;[[Push Down Members]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
These work opposite of pull up members: If ‘type’ is part of class Cat then it will be moved individually to class Cheetah and class Leopard and now no existence of ‘type’ will be found in class Cat.&lt;br /&gt;
&amp;lt;li&amp;gt;[[Override Method]]&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Refactoring support in popular IDE(s)==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! IDE&lt;br /&gt;
! Language(s)&lt;br /&gt;
! Refactoring Support&lt;br /&gt;
! Automated Refactoring?&lt;br /&gt;
! Number of Refactoring support&lt;br /&gt;
! Support for Pattern&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
! Borland Delphi&lt;br /&gt;
! Object Oriented Pascal&lt;br /&gt;
! Rename refactoring, Refactor driven “Find References”, Introduce Variable refactoring, Introduce Field refactoring, Inline Variable refactoring, Change Parameters refactoring, Safe Delete refactoring, Push Members Up / Down refactoring, Pull Members Up refactoring, Extract Superclass refactoring, Extract Interface refactoring, Move Members refactoring, Declare variable refactoring, Declare field refactoring, Extract method refactoring, Find unit/import namespace refactoring, Refactor driven “Find in Files”, Extract to resource string refactoring&amp;lt;ref name=&amp;quot;borlandrefactorings&amp;gt;Leveraging What You Have: 10 Top Things Added to Delphi Since Delphi 7 http://edn.embarcadero.com/article/37416&amp;lt;/ref&amp;gt;&lt;br /&gt;
! Yes&lt;br /&gt;
! 17&lt;br /&gt;
! Declare Variable&amp;lt;ref name=&amp;quot;borlanddecvar&amp;quot;&amp;gt;Increased Productivity with Refactoring, Unit Testing, Help Insight, Error Insight, and Sync Edit in Borland Delphi 2005 http://edn.embarcadero.com/article/33278&amp;lt;/ref&amp;gt;&lt;br /&gt;
!&lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure TWinForm1.btnOpen_Click(sender: System.Object;&lt;br /&gt;
  e: System.EventArgs);&lt;br /&gt;
begin&lt;br /&gt;
  if (OpenFileDialog1.ShowDialog =&lt;br /&gt;
      System.Windows.Forms.DialogResult.OK) then&lt;br /&gt;
  begin&lt;br /&gt;
    Assign(f,OpenFileDialog1.FileName);&lt;br /&gt;
    Reset(f);&lt;br /&gt;
    try&lt;br /&gt;
    finally&lt;br /&gt;
      CloseFile(f);&lt;br /&gt;
    end;&lt;br /&gt;
  end;&lt;br /&gt;
end;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
---- After Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure TWinForm1.btnOpen_Click(sender: System.Object;&lt;br /&gt;
  e: System.EventArgs);&lt;br /&gt;
var&lt;br /&gt;
  f: TextFile;&lt;br /&gt;
begin&lt;br /&gt;
  if (OpenFileDialog1.ShowDialog =&lt;br /&gt;
      System.Windows.Forms.DialogResult.OK) then&lt;br /&gt;
  begin&lt;br /&gt;
    Assign(f,OpenFileDialog1.FileName);&lt;br /&gt;
    Reset(f);&lt;br /&gt;
    try&lt;br /&gt;
    finally&lt;br /&gt;
      CloseFile(f);&lt;br /&gt;
    end;&lt;br /&gt;
  end;&lt;br /&gt;
end;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Eclipse &amp;lt;ref name='refeclipse'&amp;gt;Eclipse Refactoring Actions http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-menu-refactor.htm &amp;lt;/ref&amp;gt;&lt;br /&gt;
! C, C++, Java&lt;br /&gt;
! Rename selected Elements, Move selected elements, Change Method Signature, Extraction, Convert Anonymous Class to Nested, Convert Local Variable to Field, Extract Super class and Interface, push down, pull up, Introduce Indirection, Encapsulate Field, Migrate JAR File, Create &amp;amp; Apply Refatoring Scripts&lt;br /&gt;
! Yes&lt;br /&gt;
! 27&lt;br /&gt;
! Introduce Factory&lt;br /&gt;
! &lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
int x= p * 5; 	&lt;br /&gt;
p.x;&lt;br /&gt;
---- After Refactoring&lt;br /&gt;
int x= getFoo(p); (Method Extraction) &lt;br /&gt;
p.getX() (Encapsulate Field)&lt;br /&gt;
|-&lt;br /&gt;
! Kdevelop&lt;br /&gt;
! C, C++&lt;br /&gt;
! Several&amp;lt;ref name=&amp;quot;kdeveloprefactorings&amp;quot;&amp;gt;Kdevelop4: Now with refactoring! http://zwabel.wordpress.com/2008/11/05/kdevelop4-now-with-refactoring/&amp;lt;/ref&amp;gt;. Project wide search/replace and local search/replace.&lt;br /&gt;
! Yes but very limited.&lt;br /&gt;
! 2&lt;br /&gt;
! Rename Class &lt;br /&gt;
! Right click on class name and select Rename&lt;br /&gt;
|-&lt;br /&gt;
! NetBeans &amp;lt;ref name=&amp;quot;refnetbeans&amp;quot;&amp;gt;Refactoring in NetBeans 4.1 http://java.sun.com/developer/technicalArticles/tools/refactoring/&amp;lt;/ref&amp;gt;&lt;br /&gt;
! Java&lt;br /&gt;
! Renaming fields, methods, classes, or packages, Encapsulating fields, Changing method parameters, Moving classes, &lt;br /&gt;
! Yes but very limited &lt;br /&gt;
! 4&lt;br /&gt;
! No&lt;br /&gt;
! &lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Foo{&lt;br /&gt;
  public int n;&lt;br /&gt;
  public int Compute(int val);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
---- After Refactoring (Encapsulating field)&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pubic class Foo{&lt;br /&gt;
  private int n;&lt;br /&gt;
  public int getN();&lt;br /&gt;
  public void setN(int val);&lt;br /&gt;
  public int Compute(int arg);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Padre&lt;br /&gt;
! Perl&lt;br /&gt;
! Lexically Rename Variable, Introduce Temporary Variable&amp;lt;ref name=&amp;quot;padrerefactorings&amp;quot; &amp;gt;How to do it in Padre http://padre.perlide.org/howto.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
! Yes&lt;br /&gt;
! Unknown&lt;br /&gt;
! &lt;br /&gt;
! Right click on item to refactor and select refactoring choice from the context menu&lt;br /&gt;
|-&lt;br /&gt;
! RubyMine&lt;br /&gt;
! Ruby&lt;br /&gt;
! Safe Delete, Rename Refactorings, Push Members Down, Pull Members Up, Move Refactorings, Introduce Variable, Introduce Parameter, Introduce Field, Introduce Constant, Inline, Extract Superclass, Extract Module, Extract Partial, Extract Method, Copy / Clone.&lt;br /&gt;
! Yes&lt;br /&gt;
! 15&lt;br /&gt;
! Introduce Temporary&lt;br /&gt;
!&lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
c = a * b + 5&lt;br /&gt;
d = 2 * a * b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
---- After Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = a * b&lt;br /&gt;
c = t + 5&lt;br /&gt;
d = 2 * t&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Visual Studio &amp;lt;ref name=&amp;quot;refvstudio&amp;quot;&amp;gt;7 Refactoring Methods in Visual Studio 2010 http://p2p.wrox.com/content/articles/7-refactoring-methods-visual-studio-2010 &amp;lt;/ref&amp;gt;&lt;br /&gt;
! C, C++, Java, C#&lt;br /&gt;
! Extract Method, Encapsulate Field, Extract Interface, Rename, Promote Variable to Parameter(Changing method signature), Generate Method Stub(Used for testing purposes)&lt;br /&gt;
! Yes&lt;br /&gt;
! 7&lt;br /&gt;
! No&lt;br /&gt;
! Promoting Variable to Parameter&lt;br /&gt;
---- Before Refactoring &lt;br /&gt;
public void MethodA()&lt;br /&gt;
{&lt;br /&gt;
    MethodB();&lt;br /&gt;
}&lt;br /&gt;
public void MethodB()&lt;br /&gt;
{&lt;br /&gt;
    string output = &amp;quot;Test String&amp;quot;;&lt;br /&gt;
    MessageBox.Show( output);&lt;br /&gt;
}&lt;br /&gt;
---- After Refactoring &lt;br /&gt;
public void MethodA()&lt;br /&gt;
{&lt;br /&gt;
    MethodB(&amp;quot;Test String&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
public void MethodB(string output)&lt;br /&gt;
{&lt;br /&gt;
    MessageBox.Show( output);&lt;br /&gt;
}&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w45_is&amp;diff=66567</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w45 is</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w45_is&amp;diff=66567"/>
		<updated>2012-10-03T22:29:12Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Automated Refactoring'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Refactoring is a technique for restructuring existing code. It is defined as altering the code's internal structure without changing its external behavior. It is done as a series of small transformations which together produce significant restructuring. Since each transformation is small, it's less likely to go wrong. The system is also kept fully working after each small transformation, reducing the chances that a system can get seriously broken during the restructuring.&amp;lt;ref name=&amp;quot;refone&amp;quot;&amp;gt; Refactoring Home Page http://refactoring.com/ &amp;lt;/ref&amp;gt; When people make small changes to the code without fully understanding its design, the code loses its structure, which eventually has a cumulative effect. If the developer cannot understand the code design, then it becomes more difficult for him to preserve it. Timely refactoring helps the code to retain its shape. &amp;lt;ref name=&amp;quot;reftwo&amp;quot;&amp;gt; Why should you Refactor http://sourcemaking.com/refactoring/why-should-you-refactor &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Automated refactoring refers to refactoring which is under the developers control. What we mean by this is that, the system is not responsible for deciding what refactoring is required, rather this is the job of the developer. Automated refactoring is a feature of most widely used IDE's today, which helps expedite the task of refactoring for the developers. &amp;lt;ref name=&amp;quot;refthree&amp;quot;&amp;gt; Refactoring for everyone http://www.ibm.com/developerworks/library/os-ecref/ &amp;lt;/ref&amp;gt;  &lt;br /&gt;
In this article, we are aiming at describing the various techniques for automated refactoring. This article gives a clear understanding of these techniques which are illustrated with examples. We have also given a comparison of the refactoring functionality provided by various frequently used IDE's. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Common Refactoring Techniques==&lt;br /&gt;
===Extraction Refactoring=== &lt;br /&gt;
There are several refactoring techniques related to extraction: Extract method, Extract local variables, Extract constants. The first method &amp;quot;Extract method will create a new method from code you've selected. Extract Local Variable refactoring takes an expression that is being used repeatedly and assigns it to a local variable. Extract constant refactoring is to select a static, constant expression, which the refactoring will convert to a static final constant. This is useful for removing hard-coded numbers and strings from your code&lt;br /&gt;
&lt;br /&gt;
[[1) Extract method:]]&lt;br /&gt;
&lt;br /&gt;
'''Before refactoring'''                &lt;br /&gt;
:public void abc() {&lt;br /&gt;
::int x= 9;&lt;br /&gt;
::int y= 100;&lt;br /&gt;
::int z=x+y;&lt;br /&gt;
::int s=x +z;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
'''After refactoring'''&lt;br /&gt;
:public void abc() {&lt;br /&gt;
::int x=9;&lt;br /&gt;
::int y=50;&lt;br /&gt;
::int z= add(x,y);&lt;br /&gt;
::int s= add(x,z);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:private int add(int x, int y) {&lt;br /&gt;
::return x+y;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
[[2) Extract local variable:]]&lt;br /&gt;
This refactoring technique takes out an expression and then assigns it to a variable.&lt;br /&gt;
This variable is then used in place of expression everywhere in the code where the&lt;br /&gt;
expression is present. It has several advantages. First of all it gives meaningful&lt;br /&gt;
name to an expression which helps in understanding what an expression does. It&lt;br /&gt;
helps in debugging of code and it makes code efficient.&lt;br /&gt;
&lt;br /&gt;
'''Before refactoring'''&lt;br /&gt;
&lt;br /&gt;
:void xyz() {&lt;br /&gt;
::int i=12;&lt;br /&gt;
::int j= 6;&lt;br /&gt;
::int k=15;&lt;br /&gt;
::int p=10;&lt;br /&gt;
::p+=i+j+k;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
In order to perform refactoring in eclipse or any other tool first we select the&lt;br /&gt;
expression (here i+j+k) or any other tool, it asks for name of new variable name.&lt;br /&gt;
Suppose we give it q.&lt;br /&gt;
&lt;br /&gt;
'''After refactoring'''&lt;br /&gt;
&lt;br /&gt;
:void xyz() {&lt;br /&gt;
::int i=12;&lt;br /&gt;
::int j= 6;&lt;br /&gt;
::int k=15;&lt;br /&gt;
::int p=10;&lt;br /&gt;
::p+=q;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
[[3) Extract constant:]]&lt;br /&gt;
&lt;br /&gt;
'''Before refactoring'''&lt;br /&gt;
:void abc() {&lt;br /&gt;
::int a=100;&lt;br /&gt;
::int b=100;&lt;br /&gt;
::int z= a+b;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
Now if we want to extract the constant 100 we will select it and enter a name for it&lt;br /&gt;
say for example ‘num’. Now instead of using 100 we will use the variable ’num’&lt;br /&gt;
&lt;br /&gt;
'''After refactoring'''&lt;br /&gt;
&lt;br /&gt;
:void abc() {&lt;br /&gt;
::int a=num;&lt;br /&gt;
::int b=num;&lt;br /&gt;
::int c=num+num;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
===Inline Refactoring=== &lt;br /&gt;
This type refactoring is the reverse of local variable extraction, in which we remove a local variable and use the value of the variable(typically an expression) in the place where the variable is used. This can be marginally more efficient than using a temporary variable and, by making the code terser, makes it either easier to read or more cryptic, depending on your point of view&amp;lt;ref name=&amp;quot;refthree&amp;quot;&amp;gt; refactoring for everyone http://www.ibm.com/developerworks/library/os-ecref/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inline Refactoring&lt;br /&gt;
&lt;br /&gt;
Put in easy words, Inline refactoring is doing what an external method call does but without making the&lt;br /&gt;
actual call to an external method. This is as good as writing what a method does inline instead of calling&lt;br /&gt;
a method defined outside. Let us illustrate this with an example.&lt;br /&gt;
&lt;br /&gt;
'''Before Inline Refactoring'''&lt;br /&gt;
&lt;br /&gt;
:void funtion1(String msg) {&lt;br /&gt;
::Console.write(msg);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:void function2() {&lt;br /&gt;
::function1(“someString”);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
'''After Inline refactoring'''&lt;br /&gt;
&lt;br /&gt;
:void funtion2() {&lt;br /&gt;
::Console.write(“someString”);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
In this way we are shortening the code but can print statically only one value (in this case someString), each time.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;reffour&amp;quot;&amp;gt; Refactorings-Inline Method: http://www.skorkin.com/2011/02/refactorings-inline-method/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Encapsulating Fields===&lt;br /&gt;
Generally its not a good practice to expose the class attributes through public interface. Thus, Encapsulating such fields will make such fields as private or protected as appropriate and generate getters and setters for the associated field.&lt;br /&gt;
&lt;br /&gt;
Consider the following piece of code. This class has an attribute which is public and can be accessed by any object directly.&lt;br /&gt;
&lt;br /&gt;
'''Before Encapsulating Fields'''&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::public String attr1;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main(String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::Obj.attr1 = “var”;&lt;br /&gt;
:::System.out.println(“Value of attr1 is” + obj.attr1);&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
On making attr1 private, we need to add getter and setter method to access it outside the class. This can be done using automated refactoring technique called Encapsulating a field technique. Now the object obj can access the attribute attr1 only through the defined getter and setter method.&lt;br /&gt;
&lt;br /&gt;
'''After Encapsulating Fields'''&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::private String attr1;&lt;br /&gt;
&lt;br /&gt;
::public String getAttr1() {&lt;br /&gt;
:::return attr1;&lt;br /&gt;
::}&lt;br /&gt;
&lt;br /&gt;
::public setAttr1(String newAttr1Value) {&lt;br /&gt;
:::attr1 = newAttr1Value;&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main (String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::obj.setAttr1(“var”);&lt;br /&gt;
:::System.out .println(“Value of Attr1 is” + obj.getAttr1());&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
===Changing Method Signature===&lt;br /&gt;
Here we change the change the parameters, visibility, and return type of a method.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Before Refactoring'''&lt;br /&gt;
Suppose our class Class1 is defined as below and it has a function funtion1 as shown:&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::public void funtion1(int a) {&lt;br /&gt;
::://do something&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
The method call to function1 will be as follows:&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main(String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::Obj.function1(5);&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''After Refactoring'''&lt;br /&gt;
On change the number of arguments in function1 to 2 instead of 1 the function call should also change. This is done by a concept called Automated refactoring technique for changing method signature.&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::public void funtion1(int a, double b) {&lt;br /&gt;
::://do something&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main(String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::Obj.function1(5, 0.0);&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
When performing the refactoring a default value of 0.0 will be passed to the function as a parameter.&lt;br /&gt;
&lt;br /&gt;
== Other Refactorings ==&lt;br /&gt;
&lt;br /&gt;
There are a multitude of refactorings.  Those that are included with RubyMine&amp;lt;ref name=&amp;quot;reffive&amp;quot;&amp;gt;RubyMine :: Ruby and Ruby on Rails IDE with smart code completion, syntax highlighting and refactoring http://www.jetbrains.com/ruby/features/ruby_ide.html#Refactorings&amp;lt;/ref&amp;gt; include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;[[Rename refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This refactoring technique allows renaming symbols. There&lt;br /&gt;
are several rename refactoring available. These are rename package, rename class,&lt;br /&gt;
rename method, rename variable etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Before refactoring'''&lt;br /&gt;
:class abc() {&lt;br /&gt;
::puts “hello”&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:def message() {&lt;br /&gt;
::a= new abc()&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
In order to rename the class ‘abc’ select it, choose rename option from the tool and give a new name.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''After refactoring'''&lt;br /&gt;
&lt;br /&gt;
:class xyz() {&lt;br /&gt;
::puts “hello”&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:def message() {&lt;br /&gt;
::a= new xyz()&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;[[Extract Module]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This refactoring technique creates a module from the methods of a ruby class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Before refactoring'''&lt;br /&gt;
&lt;br /&gt;
:class abc() {&lt;br /&gt;
::def total_cost(item1, item2) {&lt;br /&gt;
:::return(item1 +item2)&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''After refactoring:''' in the abc.rb file we will have&lt;br /&gt;
&lt;br /&gt;
:class abc()&lt;br /&gt;
::include total_cost&lt;br /&gt;
:end&lt;br /&gt;
&lt;br /&gt;
in total_cost.rb file we will have&lt;br /&gt;
&lt;br /&gt;
:module total_cost&lt;br /&gt;
&lt;br /&gt;
::def total_cost(item1, item2) {&lt;br /&gt;
:::return(item1 +item2)&lt;br /&gt;
::}&lt;br /&gt;
&amp;lt;li&amp;gt;[[Extract Superclass]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It extract certain methods from a class in a superclass.&lt;br /&gt;
For example if a class B contains two methods abc and xyz. We can do refactoring and move method xyz into superclass A.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Before refactoring'''&lt;br /&gt;
&lt;br /&gt;
:class B&lt;br /&gt;
::def abc&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
&lt;br /&gt;
::def xyz&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
:end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''After refactoring'''&lt;br /&gt;
&lt;br /&gt;
:class B&amp;lt;A&lt;br /&gt;
::def abc&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
&lt;br /&gt;
A.rb file&lt;br /&gt;
&lt;br /&gt;
::def xyz&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
&amp;lt;li&amp;gt;[[Introduce Variable]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is when an expression is replaced by a variable and the variable is used in place of the expression each time. A change to the expression will mean a change only at one place.&lt;br /&gt;
Eg. &lt;br /&gt;
:puts “My favorite team is #{get_team()}”&lt;br /&gt;
But if, ::myTeam = get_team();&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then, :puts “My favorite team is #{myTeam}”&lt;br /&gt;
&amp;lt;li&amp;gt;[[Introduce Constant]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is when a new constant is defined and initialized and that constant is used instead of a value.&lt;br /&gt;
Eg. :a = b +3; CONSTANT = 3; a = b + CONSTANT;&lt;br /&gt;
&amp;lt;li&amp;gt;[[Introduce Field]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is when a new field is defined and initialized and then that field is used instead of&lt;br /&gt;
the value.&lt;br /&gt;
Eg. :a = b + 3; @field = 3; a = b + @field;&lt;br /&gt;
&amp;lt;li&amp;gt;[[Introduce Parameter]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This type simply adds a new parameter to a function and updates the method calls according to that.&lt;br /&gt;
Eg.&lt;br /&gt;
:Method signature changes from def function1() to def function1(pass)&lt;br /&gt;
:Method call will change from a=function1() to a=funtion1(something)&lt;br /&gt;
&amp;lt;li&amp;gt;[[Inline Variable]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This works similar to Inline refactoring explained above. &lt;br /&gt;
Eg. Instead of writing a variable’s value to console, we will write a constant to the console, which is basically hard coding the value written to the console.&lt;br /&gt;
&amp;lt;li&amp;gt;[[Pull Up Members]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Refers to moving a field x from a class A to its superclass. Thus, x will be moved out of all of A’s siblings and will have just one copy in A’s superclass.&lt;br /&gt;
Eg. If ‘type’ is an attribute of class Leopard and class Cheetah, then moving ‘type’ to super class of Leopard and Cheetah, that is, class Cat will result into one copy of ‘type’ being in class Cat and ‘type’ will get removed from both Cheetah and Leopard.&lt;br /&gt;
&amp;lt;li&amp;gt;[[Push Down Members]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
These work opposite of pull up members: If ‘type’ is part of class Cat then it will be moved individually to class Cheetah and class Leopard and now no existence of ‘type’ will be found in class Cat.&lt;br /&gt;
&amp;lt;li&amp;gt;[[Override Method]]&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Refactoring support in popular IDE(s)==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! IDE&lt;br /&gt;
! Language(s)&lt;br /&gt;
! Refactoring Support&lt;br /&gt;
! Automated Refactoring?&lt;br /&gt;
! Number of Refactoring support&lt;br /&gt;
! Support for Pattern&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
! Borland Delphi&lt;br /&gt;
! Object Oriented Pascal&lt;br /&gt;
! Rename refactoring, Refactor driven “Find References”, Introduce Variable refactoring, Introduce Field refactoring, Inline Variable refactoring, Change Parameters refactoring, Safe Delete refactoring, Push Members Up / Down refactoring, Pull Members Up refactoring, Extract Superclass refactoring, Extract Interface refactoring, Move Members refactoring, Declare variable refactoring, Declare field refactoring, Extract method refactoring, Find unit/import namespace refactoring, Refactor driven “Find in Files”, Extract to resource string refactoring&amp;lt;ref name=&amp;quot;borlandrefactorings&amp;gt;Leveraging What You Have: 10 Top Things Added to Delphi Since Delphi 7 http://edn.embarcadero.com/article/37416&amp;lt;/ref&amp;gt;&lt;br /&gt;
! Yes&lt;br /&gt;
! 17&lt;br /&gt;
! Declare Variable&amp;lt;ref name=&amp;quot;borlanddecvar&amp;quot;&amp;gt;Increased Productivity with Refactoring, Unit Testing, Help Insight, Error Insight, and Sync Edit in Borland Delphi 2005 http://edn.embarcadero.com/article/33278&amp;lt;/ref&amp;gt;&lt;br /&gt;
!&lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure TWinForm1.btnOpen_Click(sender: System.Object;&lt;br /&gt;
  e: System.EventArgs);&lt;br /&gt;
begin&lt;br /&gt;
  if (OpenFileDialog1.ShowDialog =&lt;br /&gt;
      System.Windows.Forms.DialogResult.OK) then&lt;br /&gt;
  begin&lt;br /&gt;
    Assign(f,OpenFileDialog1.FileName);&lt;br /&gt;
    Reset(f);&lt;br /&gt;
    try&lt;br /&gt;
    finally&lt;br /&gt;
      CloseFile(f);&lt;br /&gt;
    end;&lt;br /&gt;
  end;&lt;br /&gt;
end;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
---- After Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure TWinForm1.btnOpen_Click(sender: System.Object;&lt;br /&gt;
  e: System.EventArgs);&lt;br /&gt;
var&lt;br /&gt;
  f: TextFile;&lt;br /&gt;
begin&lt;br /&gt;
  if (OpenFileDialog1.ShowDialog =&lt;br /&gt;
      System.Windows.Forms.DialogResult.OK) then&lt;br /&gt;
  begin&lt;br /&gt;
    Assign(f,OpenFileDialog1.FileName);&lt;br /&gt;
    Reset(f);&lt;br /&gt;
    try&lt;br /&gt;
    finally&lt;br /&gt;
      CloseFile(f);&lt;br /&gt;
    end;&lt;br /&gt;
  end;&lt;br /&gt;
end;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Eclipse &amp;lt;ref name='refeclipse'&amp;gt;Eclipse Refactoring Actions http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-menu-refactor.htm &amp;lt;/ref&amp;gt;&lt;br /&gt;
! C, C++, Java&lt;br /&gt;
! Rename selected Elements, Move selected elements, Change Method Signature, Extraction, Convert Anonymous Class to Nested, Convert Local Variable to Field, Extract Super class and Interface, push down, pull up, Introduce Indirection, Encapsulate Field, Migrate JAR File, Create &amp;amp; Apply Refatoring Scripts&lt;br /&gt;
! Yes&lt;br /&gt;
! 27&lt;br /&gt;
! Introduce Factory&lt;br /&gt;
! &lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
int x= p * 5; 	&lt;br /&gt;
p.x;&lt;br /&gt;
---- After Refactoring&lt;br /&gt;
int x= getFoo(p); (Method Extraction) &lt;br /&gt;
p.getX() (Encapsulate Field)&lt;br /&gt;
|-&lt;br /&gt;
! Kdevelop&lt;br /&gt;
! C, C++&lt;br /&gt;
! Several&amp;lt;ref name=&amp;quot;kdeveloprefactorings&amp;quot;&amp;gt;Kdevelop4: Now with refactoring! http://zwabel.wordpress.com/2008/11/05/kdevelop4-now-with-refactoring/&amp;lt;/ref&amp;gt;. Project wide search/replace and local search/replace.&lt;br /&gt;
! Yes but very limited.&lt;br /&gt;
! 2&lt;br /&gt;
! Rename Class &lt;br /&gt;
! Right click on class name and select Rename&lt;br /&gt;
|-&lt;br /&gt;
! NetBeans &amp;lt;ref name=&amp;quot;refnetbeans&amp;quot;&amp;gt;Refactoring in NetBeans 4.1 http://java.sun.com/developer/technicalArticles/tools/refactoring/&amp;lt;/ref&amp;gt;&lt;br /&gt;
! Java&lt;br /&gt;
! Renaming fields, methods, classes, or packages, Encapsulating fields, Changing method parameters, Moving classes, &lt;br /&gt;
! Yes but very limited &lt;br /&gt;
! 4&lt;br /&gt;
! No&lt;br /&gt;
! &lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Foo{&lt;br /&gt;
  public int n;&lt;br /&gt;
  public int Compute(int val);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
---- After Refactoring (Encapsulating field)&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pubic class Foo{&lt;br /&gt;
  private int n;&lt;br /&gt;
  public int getN();&lt;br /&gt;
  public void setN(int val);&lt;br /&gt;
  public int Compute(int arg);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Padre&lt;br /&gt;
! Perl&lt;br /&gt;
! Lexically Rename Variable, Introduce Temporary Variable&amp;lt;ref name=&amp;quot;padrerefactorings&amp;quot; &amp;gt;How to do it in Padre http://padre.perlide.org/howto.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
! Yes&lt;br /&gt;
! Unknown&lt;br /&gt;
! &lt;br /&gt;
! Right click on item to refactor and select refactoring choice from the context menu&lt;br /&gt;
|-&lt;br /&gt;
! RubyMine&lt;br /&gt;
! Ruby&lt;br /&gt;
! Safe Delete, Rename Refactorings, Push Members Down, Pull Members Up, Move Refactorings, Introduce Variable, Introduce Parameter, Introduce Field, Introduce Constant, Inline, Extract Superclass, Extract Module, Extract Partial, Extract Method, Copy / Clone.&lt;br /&gt;
! Yes&lt;br /&gt;
! 15&lt;br /&gt;
! Introduce Temporary&lt;br /&gt;
!&lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
c = a * b + 5&lt;br /&gt;
d = 2 * a * b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
---- After Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = a * b&lt;br /&gt;
c = t + 5&lt;br /&gt;
d = 2 * t&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Visual Studio &amp;lt;ref name=&amp;quot;refvstudio&amp;quot;&amp;gt;7 Refactoring Methods in Visual Studio 2010 http://p2p.wrox.com/content/articles/7-refactoring-methods-visual-studio-2010 &amp;lt;/ref&amp;gt;&lt;br /&gt;
! C, C++, Java, C#&lt;br /&gt;
! Extract Method, Encapsulate Field, Extract Interface, Rename, Promote Variable to Parameter(Changing method signature), Generate Method Stub(Used for testing purposes)&lt;br /&gt;
! Yes&lt;br /&gt;
! 7&lt;br /&gt;
! No&lt;br /&gt;
! Promoting Variable to Parameter&lt;br /&gt;
---- Before Refactoring &lt;br /&gt;
public void MethodA()&lt;br /&gt;
{&lt;br /&gt;
    MethodB();&lt;br /&gt;
}&lt;br /&gt;
public void MethodB()&lt;br /&gt;
{&lt;br /&gt;
    string output = &amp;quot;Test String&amp;quot;;&lt;br /&gt;
    MessageBox.Show( output);&lt;br /&gt;
}&lt;br /&gt;
---- After Refactoring &lt;br /&gt;
public void MethodA()&lt;br /&gt;
{&lt;br /&gt;
    MethodB(&amp;quot;Test String&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
public void MethodB(string output)&lt;br /&gt;
{&lt;br /&gt;
    MessageBox.Show( output);&lt;br /&gt;
}&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w45_is&amp;diff=65673</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w45 is</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w45_is&amp;diff=65673"/>
		<updated>2012-09-27T23:34:10Z</updated>

		<summary type="html">&lt;p&gt;Smbuch: Created page with &amp;quot;&amp;lt;big&amp;gt;'''Automated Refactoring'''&amp;lt;/big&amp;gt;  __TOC__  ==Introduction== Refactoring is a technique for restructuring existing code. It is defined as altering the code's internal struct...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Automated Refactoring'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Refactoring is a technique for restructuring existing code. It is defined as altering the code's internal structure without changing its external behavior. It is done as a series of small transformations which together produce significant restructuring. Since each transformation is small, it's less likely to go wrong. The system is also kept fully working after each small transformation, reducing the chances that a system can get seriously broken during the restructuring.&amp;lt;ref name=&amp;quot;refone&amp;quot;&amp;gt; Refactoring Home Page http://refactoring.com/ &amp;lt;/ref&amp;gt; When people make small changes to the code without fully understanding its design, the code loses its structure, which eventually has a cumulative effect. If the developer cannot understand the code design, then it becomes more difficult for him to preserve it. Timely refactoring helps the code to retain its shape. &amp;lt;ref name=&amp;quot;reftwo&amp;quot;&amp;gt; Why should you Refactor http://sourcemaking.com/refactoring/why-should-you-refactor &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Automated refactoring refers to refactoring which is under the developers control. What we mean by this is that, the system is not responsible for deciding what refactoring is required, rather this is the job of the developer. Automated refactoring is a feature of most widely used IDE's today, which helps expedite the task of refactoring for the developers. &amp;lt;ref name=&amp;quot;refthree&amp;quot;&amp;gt; Refactoring for everyone http://www.ibm.com/developerworks/library/os-ecref/ &amp;lt;/ref&amp;gt;  &lt;br /&gt;
In this article, we are aiming at describing the various techniques for automated refactoring. This article gives a clear understanding of these techniques which are illustrated with examples. We have also given a comparison of the refactoring functionality provided by various frequently used IDE's. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Common Refactoring Techniques==&lt;br /&gt;
===Extraction Refactoring=== &lt;br /&gt;
There are several refactoring techniques related to extraction: Extract method, Extract local variables, extract constants. The first method &amp;quot;Extract method will create a new method from code you've selected. Extract Local Variable refactoring takes an expression that is being used repeatedly and assigns it to a local variable. Extract constant refactoring is to select a static, constant expression, which the refactoring will convert to a static final constant. This is useful for removing hard-coded numbers and strings from your code&lt;br /&gt;
&lt;br /&gt;
1) Extract method:&lt;br /&gt;
&lt;br /&gt;
Before refactoring&lt;br /&gt;
&lt;br /&gt;
:public void abc() {&lt;br /&gt;
::int x= 9;&lt;br /&gt;
::int y= 100;&lt;br /&gt;
::int z=x+y;&lt;br /&gt;
::int s=x +z;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
After refactoring&lt;br /&gt;
&lt;br /&gt;
:public void abc() {&lt;br /&gt;
::int x=9;&lt;br /&gt;
::int y=50;&lt;br /&gt;
::int z= add(x,y);&lt;br /&gt;
::int s= add(x,z);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:private int add(int x, int y) {&lt;br /&gt;
::return x+y;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
2) extract local variable&lt;br /&gt;
This refactoring technique takes out an expression and then assigns it to a variable.&lt;br /&gt;
This variable is then used in place of expression everywhere in the code where the&lt;br /&gt;
expression is present. It has several advantages. First of all it gives meaningful&lt;br /&gt;
name to an expression which helps in understanding what an expression does. It&lt;br /&gt;
helps in debugging of code and it makes code efficient.&lt;br /&gt;
&lt;br /&gt;
Before refactoring&lt;br /&gt;
&lt;br /&gt;
:void xyz() {&lt;br /&gt;
::int i=12;&lt;br /&gt;
::int j= 6;&lt;br /&gt;
::int k=15;&lt;br /&gt;
::int p=10;&lt;br /&gt;
::p+=i+j+k;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
In order to perform refactoring in eclipse or any other tool first we select the&lt;br /&gt;
expression (here i+j+k) or any other tool it asks for name of new variable name.&lt;br /&gt;
Suppose we give it q.&lt;br /&gt;
&lt;br /&gt;
After refactoring&lt;br /&gt;
&lt;br /&gt;
:void xyz() {&lt;br /&gt;
::int i=12;&lt;br /&gt;
::int j= 6;&lt;br /&gt;
::int k=15;&lt;br /&gt;
::int p=10;&lt;br /&gt;
::p+=q;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
3) Extract constant&lt;br /&gt;
&lt;br /&gt;
:void abc() {&lt;br /&gt;
::int a=100;&lt;br /&gt;
::int b=100;&lt;br /&gt;
::int z= a+b;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
Now if we want to extract the constant 100 we will select it and enter a name for it&lt;br /&gt;
say for example ‘num’. Now instead of using 100 we will use the variable ’num’&lt;br /&gt;
So after refactoring&lt;br /&gt;
&lt;br /&gt;
:void abc() {&lt;br /&gt;
::int a=num;&lt;br /&gt;
::int b=num;&lt;br /&gt;
::int c=num+num;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
===Inline Refactoring=== &lt;br /&gt;
This type refactoring is the reverse of local variable extraction, in which we remove a local variable and use the value of the variable(typically an expression) in the place where the variable is used. This can be marginally more efficient than using a temporary variable and, by making the code terser, makes it either easier to read or more cryptic, depending on your point of view&amp;lt;ref name=&amp;quot;refthree&amp;quot;&amp;gt; refactoring for everyone http://www.ibm.com/developerworks/library/os-ecref/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inline Refactoring&lt;br /&gt;
&lt;br /&gt;
Put in easy words, Inline refactoring is doing what an external method call does but without making the&lt;br /&gt;
actual call to an external method. This is as good as writing what a method does inline instead of calling&lt;br /&gt;
a method defined outside. Let us illustrate this with an example.&lt;br /&gt;
&lt;br /&gt;
Before Inline Refactoring&lt;br /&gt;
&lt;br /&gt;
:void funtion1(String msg) {&lt;br /&gt;
::Console.write(msg);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:void function2() {&lt;br /&gt;
::function1(“someString”);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
After Inline refactoring:&lt;br /&gt;
&lt;br /&gt;
:void funtion2() {&lt;br /&gt;
::Console.write(“someString”);&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
In this way we are shortening the code but can print statically only one value (in this case someString), each time.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;reffour&amp;quot;&amp;gt; Refactorings-Inline Method: http://www.skorkin.com/2011/02/refactorings-inline-method/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Encapsulating Fields===&lt;br /&gt;
Generally its not a good practice to expose the class attributes through public interface. Thus, Encapsulating such fields will make such fields as private or protected as appropriate and generate getters and setters for the associated field.&lt;br /&gt;
&lt;br /&gt;
Consider the following piece of code. This class has an attribute which is public and can be accessed by any object directly.&lt;br /&gt;
&lt;br /&gt;
Before Encapsulating Fields&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::public String attr1;&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main(String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::Obj.attr1 = “var”;&lt;br /&gt;
:::System.out.println(“Value of attr1 is” + obj.attr1);&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
On making attr1 private, we need to add getter and setter method to access it outside the class. This can be done using automated refactoring technique called Encapsulating a field technique. Now the object obj can access the attribute attr1 only through the defined getter and setter method.&lt;br /&gt;
&lt;br /&gt;
After Encapsulating Fields&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::private String attr1;&lt;br /&gt;
&lt;br /&gt;
::public String getAttr1() {&lt;br /&gt;
:::return attr1;&lt;br /&gt;
::}&lt;br /&gt;
&lt;br /&gt;
::public setAttr1(String newAttr1Value) {&lt;br /&gt;
:::attr1 = newAttr1Value;&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main (String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::obj.setAttr1(“var”);&lt;br /&gt;
:::System.out .println(“Value of Attr1 is” + obj.getAttr1());&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
===Changing Method Signature===&lt;br /&gt;
Here we change the change the parameters, visibility, and return type of a method.&lt;br /&gt;
&lt;br /&gt;
Suppose our class Class1 is defined as below and it has a function funtion1 as shown:&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::public void funtion1(int a) {&lt;br /&gt;
::://do something&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
The method call to function1 will be as follows:&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main(String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::Obj.function1(5);&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
On change the number of arguments in function1 to 2 instead of 1 the function call should also change. This is done by a concept called Automated refactoring technique for changing method signature.&lt;br /&gt;
&lt;br /&gt;
:public class Class1 {&lt;br /&gt;
::public void funtion1(int a, double b) {&lt;br /&gt;
::://do something&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:public class Class2 {&lt;br /&gt;
::public static void main(String [] args) {&lt;br /&gt;
:::Class1 obj = new Class1();&lt;br /&gt;
:::Obj.function1(5, 0.0);&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
When performing the refactoring a default value of 0.0 will be passed to the function as a parameter.&lt;br /&gt;
&lt;br /&gt;
== Other Refactorings ==&lt;br /&gt;
&lt;br /&gt;
There are a multitude of refactorings.  Those that are included with RubyMine&amp;lt;ref name=&amp;quot;reffive&amp;quot;&amp;gt;RubyMine :: Ruby and Ruby on Rails IDE with smart code completion, syntax highlighting and refactoring http://www.jetbrains.com/ruby/features/ruby_ide.html#Refactorings&amp;lt;/ref&amp;gt; include:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rename refactoring&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This refactoring technique allows renaming symbols. There&lt;br /&gt;
are several rename refactoring available. These are rename package, rename class,&lt;br /&gt;
rename method, rename variable etc.&lt;br /&gt;
Before refactoring&lt;br /&gt;
&lt;br /&gt;
:class abc() {&lt;br /&gt;
::puts “hello”&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:def message() {&lt;br /&gt;
::a= new abc()&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
In order to rename the class ‘abc’ select it, choose rename option from the tool and give a new name.&lt;br /&gt;
&lt;br /&gt;
After refactoring&lt;br /&gt;
&lt;br /&gt;
:class xyz() {&lt;br /&gt;
::puts “hello”&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
:def message() {&lt;br /&gt;
::a= new xyz()&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;Extract Module&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This refactoring technique creates a module from the methods of a ruby class.&lt;br /&gt;
&lt;br /&gt;
Before refactoring&lt;br /&gt;
&lt;br /&gt;
:class abc() {&lt;br /&gt;
::def total_cost(item1, item2) {&lt;br /&gt;
:::return(item1 +item2)&lt;br /&gt;
::}&lt;br /&gt;
:}&lt;br /&gt;
&lt;br /&gt;
After refactoring: in the abc.rb file we will have&lt;br /&gt;
&lt;br /&gt;
:class abc()&lt;br /&gt;
::include total_cost&lt;br /&gt;
:end&lt;br /&gt;
&lt;br /&gt;
in total_cost.rb file we will have&lt;br /&gt;
&lt;br /&gt;
:module total_cost&lt;br /&gt;
&lt;br /&gt;
::def total_cost(item1, item2) {&lt;br /&gt;
:::return(item1 +item2)&lt;br /&gt;
::}&lt;br /&gt;
&amp;lt;li&amp;gt;Extract Superclass&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It extract certain methods from a class in a superclass.&lt;br /&gt;
For example if a class B contains two methods abc and xyz. We can do refactoring and move method xyz into superclass A.&lt;br /&gt;
&lt;br /&gt;
Before refactoring&lt;br /&gt;
&lt;br /&gt;
:class B&lt;br /&gt;
::def abc&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
&lt;br /&gt;
::def xyz&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
:end&lt;br /&gt;
&lt;br /&gt;
After refactoring&lt;br /&gt;
&lt;br /&gt;
:class B&amp;lt;A&lt;br /&gt;
::def abc&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
&lt;br /&gt;
A.rb file&lt;br /&gt;
&lt;br /&gt;
::def xyz&lt;br /&gt;
::://some code&lt;br /&gt;
::end&lt;br /&gt;
&amp;lt;li&amp;gt;Introduce Variable&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is when an expression is replaced by a variable and the variable is used in place of the expression each time. A change to the expression will mean a change only at one place.&lt;br /&gt;
Eg. &lt;br /&gt;
:puts “My favorite team is #{get_team()}”&lt;br /&gt;
But if, ::myTeam = get_team();&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then, :puts “My favorite team is #{myTeam}”&lt;br /&gt;
&amp;lt;li&amp;gt;Introduce Constant&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is when a new constant is defined and initialized and that constant is used instead of a value.&lt;br /&gt;
Eg. :a = b +3; CONSTANT = 3; a = b + CONSTANT;&lt;br /&gt;
&amp;lt;li&amp;gt;Introduce Field&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is when a new field is defined and initialized and then that field is used instead of&lt;br /&gt;
the value.&lt;br /&gt;
Eg. :a = b + 3; @field = 3; a = b + @field;&lt;br /&gt;
&amp;lt;li&amp;gt;Introduce Parameter&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This type simply adds a new parameter to a function and updates the method calls according to that.&lt;br /&gt;
Eg.&lt;br /&gt;
:Method signature changes from def function1() to def function1(pass)&lt;br /&gt;
:Method call will change from a=function1() to a=funtion1(something)&lt;br /&gt;
&amp;lt;li&amp;gt;Inline Variable&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This works similar to Inline refactoring explained above. &lt;br /&gt;
Eg. Instead of writing a variable’s value to console, we will write a constant to the console, which is basically hard coding the value written to the console.&lt;br /&gt;
&amp;lt;li&amp;gt;Pull Up Members&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Refers to moving a field x from a class A to its superclass. Thus, x will be moved out of all of A’s siblings and will have just one copy in A’s superclass.&lt;br /&gt;
Eg. If ‘type’ is an attribute of class Leopard and class Cheetah, then moving ‘type’ to super class of Leopard and Cheetah, that is, class Cat will result into one copy of ‘type’ being in class Cat and ‘type’ will get removed from both Cheetah and Leopard.&lt;br /&gt;
&amp;lt;li&amp;gt;Push Down Members&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
These work opposite of pull up members: If ‘type’ is part of class Cat then it will be moved individually to class Cheetah and class Leopard and now no existence of ‘type’ will be found in class Cat.&lt;br /&gt;
&amp;lt;li&amp;gt;Override Method&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Refactoring support in popular IDE(s)==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! IDE&lt;br /&gt;
! Language(s)&lt;br /&gt;
! Refactoring Support&lt;br /&gt;
! Automated Refactoring?&lt;br /&gt;
! Number of Refactoring support&lt;br /&gt;
! Support for Pattern&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
! Borland Delphi&lt;br /&gt;
! Object Oriented Pascal&lt;br /&gt;
! Rename refactoring, Refactor driven “Find References”, Introduce Variable refactoring, Introduce Field refactoring, Inline Variable refactoring, Change Parameters refactoring, Safe Delete refactoring, Push Members Up / Down refactoring, Pull Members Up refactoring, Extract Superclass refactoring, Extract Interface refactoring, Move Members refactoring, Declare variable refactoring, Declare field refactoring, Extract method refactoring, Find unit/import namespace refactoring, Refactor driven “Find in Files”, Extract to resource string refactoring&amp;lt;ref name=&amp;quot;borlandrefactorings&amp;gt;Leveraging What You Have: 10 Top Things Added to Delphi Since Delphi 7 http://edn.embarcadero.com/article/37416&amp;lt;/ref&amp;gt;&lt;br /&gt;
! Yes&lt;br /&gt;
! 17&lt;br /&gt;
! Declare Variable&amp;lt;ref name=&amp;quot;borlanddecvar&amp;quot;&amp;gt;Increased Productivity with Refactoring, Unit Testing, Help Insight, Error Insight, and Sync Edit in Borland Delphi 2005 http://edn.embarcadero.com/article/33278&amp;lt;/ref&amp;gt;&lt;br /&gt;
!&lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure TWinForm1.btnOpen_Click(sender: System.Object;&lt;br /&gt;
  e: System.EventArgs);&lt;br /&gt;
begin&lt;br /&gt;
  if (OpenFileDialog1.ShowDialog =&lt;br /&gt;
      System.Windows.Forms.DialogResult.OK) then&lt;br /&gt;
  begin&lt;br /&gt;
    Assign(f,OpenFileDialog1.FileName);&lt;br /&gt;
    Reset(f);&lt;br /&gt;
    try&lt;br /&gt;
    finally&lt;br /&gt;
      CloseFile(f);&lt;br /&gt;
    end;&lt;br /&gt;
  end;&lt;br /&gt;
end;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
---- After Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure TWinForm1.btnOpen_Click(sender: System.Object;&lt;br /&gt;
  e: System.EventArgs);&lt;br /&gt;
var&lt;br /&gt;
  f: TextFile;&lt;br /&gt;
begin&lt;br /&gt;
  if (OpenFileDialog1.ShowDialog =&lt;br /&gt;
      System.Windows.Forms.DialogResult.OK) then&lt;br /&gt;
  begin&lt;br /&gt;
    Assign(f,OpenFileDialog1.FileName);&lt;br /&gt;
    Reset(f);&lt;br /&gt;
    try&lt;br /&gt;
    finally&lt;br /&gt;
      CloseFile(f);&lt;br /&gt;
    end;&lt;br /&gt;
  end;&lt;br /&gt;
end;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Eclipse &amp;lt;ref name='refeclipse'&amp;gt;Eclipse Refactoring Actions http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-menu-refactor.htm &amp;lt;/ref&amp;gt;&lt;br /&gt;
! C, C++, Java&lt;br /&gt;
! Rename selected Elements, Move selected elements, Change Method Signature, Extraction, Convert Anonymous Class to Nested, Convert Local Variable to Field, Extract Super class and Interface, push down, pull up, Introduce Indirection, Encapsulate Field, Migrate JAR File, Create &amp;amp; Apply Refatoring Scripts&lt;br /&gt;
! Yes&lt;br /&gt;
! 27&lt;br /&gt;
! Introduce Factory&lt;br /&gt;
! &lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
int x= p * 5; 	&lt;br /&gt;
p.x;&lt;br /&gt;
---- After Refactoring&lt;br /&gt;
int x= getFoo(p); (Method Extraction) &lt;br /&gt;
p.getX() (Encapsulate Field)&lt;br /&gt;
|-&lt;br /&gt;
! Kdevelop&lt;br /&gt;
! C, C++&lt;br /&gt;
! Several&amp;lt;ref name=&amp;quot;kdeveloprefactorings&amp;quot;&amp;gt;Kdevelop4: Now with refactoring! http://zwabel.wordpress.com/2008/11/05/kdevelop4-now-with-refactoring/&amp;lt;/ref&amp;gt;. Project wide search/replace and local search/replace.&lt;br /&gt;
! Yes but very limited.&lt;br /&gt;
! 2&lt;br /&gt;
! Rename Class &lt;br /&gt;
! Right click on class name and select Rename&lt;br /&gt;
|-&lt;br /&gt;
! NetBeans &amp;lt;ref name=&amp;quot;refnetbeans&amp;quot;&amp;gt;Refactoring in NetBeans 4.1 http://java.sun.com/developer/technicalArticles/tools/refactoring/&amp;lt;/ref&amp;gt;&lt;br /&gt;
! Java&lt;br /&gt;
! Renaming fields, methods, classes, or packages, Encapsulating fields, Changing method parameters, Moving classes, &lt;br /&gt;
! Yes but very limited &lt;br /&gt;
! 4&lt;br /&gt;
! No&lt;br /&gt;
! &lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Foo{&lt;br /&gt;
  public int n;&lt;br /&gt;
  public int Compute(int val);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
---- After Refactoring (Encapsulating field)&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pubic class Foo{&lt;br /&gt;
  private int n;&lt;br /&gt;
  public int getN();&lt;br /&gt;
  public void setN(int val);&lt;br /&gt;
  public int Compute(int arg);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Padre&lt;br /&gt;
! Perl&lt;br /&gt;
! Lexically Rename Variable, Introduce Temporary Variable&amp;lt;ref name=&amp;quot;padrerefactorings&amp;quot; &amp;gt;How to do it in Padre http://padre.perlide.org/howto.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
! Yes&lt;br /&gt;
! Unknown&lt;br /&gt;
! &lt;br /&gt;
! Right click on item to refactor and select refactoring choice from the context menu&lt;br /&gt;
|-&lt;br /&gt;
! RubyMine&lt;br /&gt;
! Ruby&lt;br /&gt;
! Safe Delete, Rename Refactorings, Push Members Down, Pull Members Up, Move Refactorings, Introduce Variable, Introduce Parameter, Introduce Field, Introduce Constant, Inline, Extract Superclass, Extract Module, Extract Partial, Extract Method, Copy / Clone.&lt;br /&gt;
! Yes&lt;br /&gt;
! 15&lt;br /&gt;
! Introduce Temporary&lt;br /&gt;
!&lt;br /&gt;
---- Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
c = a * b + 5&lt;br /&gt;
d = 2 * a * b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
---- After Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = a * b&lt;br /&gt;
c = t + 5&lt;br /&gt;
d = 2 * t&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Visual Studio &amp;lt;ref name=&amp;quot;refvstudio&amp;quot;&amp;gt;7 Refactoring Methods in Visual Studio 2010 http://p2p.wrox.com/content/articles/7-refactoring-methods-visual-studio-2010 &amp;lt;/ref&amp;gt;&lt;br /&gt;
! C, C++, Java, C#&lt;br /&gt;
! Extract Method, Encapsulate Field, Extract Interface, Rename, Promote Variable to Parameter(Changing method signature), Generate Method Stub(Used for testing purposes)&lt;br /&gt;
! Yes&lt;br /&gt;
! 7&lt;br /&gt;
! No&lt;br /&gt;
! Promoting Variable to Parameter&lt;br /&gt;
---- Before Refactoring &lt;br /&gt;
public void MethodA()&lt;br /&gt;
{&lt;br /&gt;
    MethodB();&lt;br /&gt;
}&lt;br /&gt;
public void MethodB()&lt;br /&gt;
{&lt;br /&gt;
    string output = &amp;quot;Test String&amp;quot;;&lt;br /&gt;
    MessageBox.Show( output);&lt;br /&gt;
}&lt;br /&gt;
---- After Refactoring &lt;br /&gt;
public void MethodA()&lt;br /&gt;
{&lt;br /&gt;
    MethodB(&amp;quot;Test String&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
public void MethodB(string output)&lt;br /&gt;
{&lt;br /&gt;
    MessageBox.Show( output);&lt;br /&gt;
}&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smbuch</name></author>
	</entry>
</feed>