<?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=Npatowa</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=Npatowa"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Npatowa"/>
	<updated>2026-08-07T13:30:36Z</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/ch2b_2w40_sn&amp;diff=71180</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=71180"/>
		<updated>2012-11-20T05:16:05Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Command_pattern&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern&amp;lt;/ref&amp;gt;.&lt;br /&gt;
== Example of Chain of Responsibility ==&lt;br /&gt;
[[File:COR.jpg]]&lt;br /&gt;
&lt;br /&gt;
The above figure helps to understand the workings of Chain of Responsibility pattern.The example given in &amp;lt;ref&amp;gt;http://javapapers.com/design-patterns/chain-of-responsibility-design-pattern/&amp;lt;/ref&amp;gt; has been described below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Interface&lt;br /&gt;
public interface Chain {&lt;br /&gt;
 &lt;br /&gt;
  public abstract void setNext(Chain nextInChain);&lt;br /&gt;
  public abstract void process(Number request);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Number {&lt;br /&gt;
  private int number;&lt;br /&gt;
 &lt;br /&gt;
  public Number(int number) {&lt;br /&gt;
    this.number = number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public int getNumber() {&lt;br /&gt;
    return number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler1&lt;br /&gt;
public class NegativeProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;lt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;NegativeProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler2&lt;br /&gt;
public class ZeroProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() == 0) {&lt;br /&gt;
      System.out.println(&amp;quot;ZeroProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler3&lt;br /&gt;
public class PositiveProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;gt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;PositiveProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class TestChain {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    //configure Chain of Responsibility&lt;br /&gt;
    Chain c1 = new NegativeProcessor();&lt;br /&gt;
    Chain c2 = new ZeroProcessor();&lt;br /&gt;
    Chain c3 = new PositiveProcessor();&lt;br /&gt;
    c1.setNext(c2);&lt;br /&gt;
    c2.setNext(c3);&lt;br /&gt;
 &lt;br /&gt;
    //calling chain of responsibility&lt;br /&gt;
    c1.process(new Number(99));&lt;br /&gt;
    c1.process(new Number(-30));&lt;br /&gt;
    c1.process(new Number(0));&lt;br /&gt;
    c1.process(new Number(100));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example the Chain interface is implemented by three handlers which handles 3 different types of numbers-positive numbers,negative numbers and the number zero. The first in the chain is the negative number handler which sets the next handler as the zero handler which in turn sets the positive handler as the last component in the chain.The Number class acts as the receiver which has been explained in the Command pattern.The TestChain is the client.&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
# Both in Command and Chain of Responsibility  pattern commands or actions are stored so that it can be later used.&lt;br /&gt;
# The Chain of Responsibility forwards requests along a chain of classes, but the Command pattern forwards a request only to a specific object. &lt;br /&gt;
# The main intent of both the patterns is to decouple senders and receivers.In case of Command pattern that is only one.&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
Chain of Responsibility is used in many applications:&lt;br /&gt;
# It is used often to handle exceptions inside kernel.There will be a chain of interrupt handlers and the request is passed on until somebody handles it.&lt;br /&gt;
# The pattern is used in windows systems to handle events generated from the keyboard or mouse.&lt;br /&gt;
# Single sign on security solutions for web applications.  You might have a handler to check if the user is already authenticated, another handler to check for windows authentication, and a last handler to transfer the request to a logon page&amp;lt;ref&amp;gt;http://codebetter.com/jeremymiller/2005/11/07/using-the-chain-of-responsibility-pattern/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
The memento pattern is a software design pattern that facilitates the restoration an object to its previous state. The memento pattern has three different components:&amp;lt;ref&amp;gt;http://www.colourcoding.net/blog/archive/2009/07/23/reversibility-patterns-memento-and-command.aspx&amp;lt;/ref&amp;gt;&lt;br /&gt;
# Originator - the object that has an internal state and knows to save itself.&lt;br /&gt;
# Caretaker - the object that knows why and when the Originator needs to save and restore itself.&lt;br /&gt;
# Memento - the object (token) that is written and read by the Originator, and taken care by the Caretaker.&lt;br /&gt;
&lt;br /&gt;
The communication between different components in memento pattern happens in the following way:&lt;br /&gt;
The caretaker first asks the originator for a memento object. Then it does the work it was slated to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.In other words, the memento pattern can be viewed as maintaining a &amp;quot;checkpoint&amp;quot; so that the originator can easily rollback to the previous checkpoint.&amp;lt;ref&amp;gt;http://sourcemaking.com/design_patterns/memento&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following diagram illustrates the memento pattern:&amp;lt;br/&amp;gt;&lt;br /&gt;
[[File:Memento.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Example of Memento Pattern ==&lt;br /&gt;
&lt;br /&gt;
The following program illustrates the &amp;quot;undo&amp;quot; usage of the Memento Pattern &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Memento_pattern&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
class Originator {&lt;br /&gt;
    private String state;&lt;br /&gt;
    // The class could also contain additional data that is not part of the&lt;br /&gt;
    // state saved in the memento.&lt;br /&gt;
 &lt;br /&gt;
    public void set(String state) {&lt;br /&gt;
        System.out.println(&amp;quot;Originator: Setting state to &amp;quot; + state);&lt;br /&gt;
        this.state = state;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public Memento saveToMemento() {&lt;br /&gt;
        System.out.println(&amp;quot;Originator: Saving to Memento.&amp;quot;);&lt;br /&gt;
        return new Memento(state);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public void restoreFromMemento(Memento memento) {&lt;br /&gt;
        state = memento.getSavedState();&lt;br /&gt;
        System.out.println(&amp;quot;Originator: State after restoring from Memento: &amp;quot; + state);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public static class Memento {&lt;br /&gt;
        private final String state;&lt;br /&gt;
 &lt;br /&gt;
        public Memento(String stateToSave) {&lt;br /&gt;
            state = stateToSave;&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        public String getSavedState() {&lt;br /&gt;
            return state;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Caretaker {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List&amp;lt;Originator.Memento&amp;gt; savedStates = new ArrayList&amp;lt;Originator.Memento&amp;gt;();&lt;br /&gt;
 &lt;br /&gt;
        Originator originator = new Originator();&lt;br /&gt;
        originator.set(&amp;quot;State1&amp;quot;);&lt;br /&gt;
        originator.set(&amp;quot;State2&amp;quot;);&lt;br /&gt;
        savedStates.add(originator.saveToMemento());&lt;br /&gt;
        originator.set(&amp;quot;State3&amp;quot;);&lt;br /&gt;
        // We can request multiple mementos, and choose which one to roll back to.&lt;br /&gt;
        savedStates.add(originator.saveToMemento());&lt;br /&gt;
        originator.set(&amp;quot;State4&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
        originator.restoreFromMemento(savedStates.get(1));   &lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output is:&lt;br /&gt;
 Originator: Setting state to State1&lt;br /&gt;
 Originator: Setting state to State2&lt;br /&gt;
 Originator: Saving to Memento.&lt;br /&gt;
 Originator: Setting state to State3&lt;br /&gt;
 Originator: Saving to Memento.&lt;br /&gt;
 Originator: Setting state to State4&lt;br /&gt;
 Originator: State after restoring from Memento: State3&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
#The similarity between Command and Memento act as magic tokens to be passed around and invoked at a later time. In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time. &lt;br /&gt;
#Polymorphism is important to Command, but not to Memento because its interface is so narrow that a memento can only be passed as a value.&lt;br /&gt;
#Command can use Memento to maintain the state required for an undo operation.&lt;br /&gt;
#In practice, the Memento pattern is a little brittle. Changes in the behavior of related objects could lead to changes in what has to be stored. However since command pattern separates out the responsibility for reversibility into the relevant transitions, it proves to be less brittle.  Furthermore, command objects can be extended in further directions, taking in permissions, interruptibility, batching, to name but a few.&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The following are the applications where memento pattern can be used:&lt;br /&gt;
#An interesting game in which memento pattern can be used is Prince of Persia: Sands of Time. In the game, you can hit a button that reverses time. By storing the state of every actor in recent frames, it can just as easily rewind them.   &lt;br /&gt;
# Another simple application where memento pattern can be used is a calculator that finds the result of addition of two numbers, with the additional option to undo last operation and restore previous result.&lt;br /&gt;
# Memento pattern is useful when you need to find the seed of a pseudo random number generator and the state in a finite state machine.&lt;br /&gt;
&lt;br /&gt;
In all the above applications the &amp;quot;undo&amp;quot; feature is common. Hence, An unlimited “undo” and “redo” capability can be readily implemented with a stack of Command objects and a stack of Memento objects.&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
The unofficially accepted definition for the Strategy Pattern is: ''&amp;quot;Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.&amp;quot;''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern&amp;lt;/ref&amp;gt;. In other words, the Strategy Pattern encapsulates a collection of functions that do more or less similar tasks but not identical tasks. Strategy pattern is used when we need to take a decision of which strategy to use based on the input parameters. An important feature of strategy pattern is that client is aware of all the available strategies and which strategy to adopt. It helps is design a system that is elegant, extensible, and powerful. The following diagram illustrates the strategy pattern: &amp;lt;br/&amp;gt;&lt;br /&gt;
[[File:Strategy.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The following example is in Java.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// The classes that implement a concrete strategy should implement this.&lt;br /&gt;
// The Context class uses this to call the concrete strategy.&lt;br /&gt;
interface IStrategy {&lt;br /&gt;
    int execute(int a, int b); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Implements the algorithm using the strategy interface&lt;br /&gt;
class ConcreteStrategyAdd implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategyAdd's execute()&amp;quot;);&lt;br /&gt;
        return a + b;  // Do an addition with a and b&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ConcreteStrategySubtract implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategySubtract's execute()&amp;quot;);&lt;br /&gt;
        return a - b;  // Do a subtraction with a and b&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ConcreteStrategyMultiply implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategyMultiply's execute()&amp;quot;);&lt;br /&gt;
        return a * b;   // Do a multiplication with a and b&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object&lt;br /&gt;
class Context {&lt;br /&gt;
&lt;br /&gt;
    private IStrategy strategy;&lt;br /&gt;
&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Context(IStrategy strategy) {&lt;br /&gt;
        this.strategy = strategy;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int executeStrategy(int a, int b) {&lt;br /&gt;
        return strategy.execute(a, b);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Test application&lt;br /&gt;
class StrategyExample {&lt;br /&gt;
&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
&lt;br /&gt;
        Context context;&lt;br /&gt;
&lt;br /&gt;
        // Three contexts following different strategies&lt;br /&gt;
        context = new Context(new ConcreteStrategyAdd());&lt;br /&gt;
        int resultA = context.executeStrategy(3,4);&lt;br /&gt;
&lt;br /&gt;
        context = new Context(new ConcreteStrategySubtract());&lt;br /&gt;
        int resultB = context.executeStrategy(3,4);&lt;br /&gt;
&lt;br /&gt;
        context = new Context(new ConcreteStrategyMultiply());&lt;br /&gt;
        int resultC = context.executeStrategy(3,4);&lt;br /&gt;
     &lt;br /&gt;
        System.out.println(&amp;quot;Result A : &amp;quot; + resultA );&lt;br /&gt;
        System.out.println(&amp;quot;Result B : &amp;quot; + resultB );&lt;br /&gt;
        System.out.println(&amp;quot;Result C : &amp;quot; + resultC );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
The difference between Strategy Pattern and Command pattern is in the purpose itself:&lt;br /&gt;
#Command encapsulates a single action. It therefore tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later - or it is used to provide undo functionality as explained in the Command pattern section.Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded later.&lt;br /&gt;
#Strategies encapsulate algorithms. Commands separate the sender from the receiver of a request, they turn a request into an object. If it's an algorithm, how something will be done, use a Strategy. If you need to separate the call of a method from its execution use a Command. &lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The applications where strategy pattern is useful are as follows:&lt;br /&gt;
#Strategy pattern is used in an system where we try to book the most inexpensive ticket from source to destination. Depending on the users preference of seats, time of transit and other preferences the system chooses the algorithm that will cater to all the user needs at an optimum fare.&lt;br /&gt;
#Strategy pattern can be used when we have to sort numbers. Depending on the type of input, we can make use of the best algorithm. For example, if we know that the numbers are almost sorted then we make use of insertion sort. If the numbers to be sorted are within a range, then we make use of counting sort. If the input sequence is random then our best bet is to use merge sort.&lt;br /&gt;
# A strategy pattern can be used when we want to read a file that has been transferred over a network. If the file is an XML File then use an XML parsing algorithm. If it is JSON file then use a JSON parser and so on.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
Thus, we see that Command Pattern is quite useful. The Chain of Responsibility, Memento ad Strategy pattern adopt the concept of Command patterns and have their own variations which make them useful in different scenarios as mentioned in each of the sections. The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that knows how to perform it. The major disadvantage of the pattern is that it results in many Command classes that can clutter up a design. If the classes are not designed properly it may lead to bloating of the design and will increase the cost of maintaining such a design.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
 [http://www.oodesign.com/command-pattern.html]http://www.oodesign.com/command-pattern.html&lt;br /&gt;
 [http://www.go4expert.com/forums/showthread.php?t=5127#command]http://www.go4expert.com/forums/showthread.php?t=5127#command&lt;br /&gt;
 [http://www.mydeveloperconnection.com/html/gof_design_patterns.htm]http://www.mydeveloperconnection.com/html/gof_design_patterns.htm&lt;br /&gt;
 [http://java.dzone.com/articles/design-patterns-command]http://java.dzone.com/articles/design-patterns-command&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=71175</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=71175"/>
		<updated>2012-11-20T05:14:22Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Command_pattern&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern&amp;lt;/ref&amp;gt;.&lt;br /&gt;
== Example of Chain of Responsibility ==&lt;br /&gt;
[[File:COR.jpg]]&lt;br /&gt;
&lt;br /&gt;
The above figure helps to understand the workings of Chain of Responsibility pattern.The example given in &amp;lt;ref&amp;gt;http://javapapers.com/design-patterns/chain-of-responsibility-design-pattern/&amp;lt;/ref&amp;gt; has been described below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Interface&lt;br /&gt;
public interface Chain {&lt;br /&gt;
 &lt;br /&gt;
  public abstract void setNext(Chain nextInChain);&lt;br /&gt;
  public abstract void process(Number request);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Number {&lt;br /&gt;
  private int number;&lt;br /&gt;
 &lt;br /&gt;
  public Number(int number) {&lt;br /&gt;
    this.number = number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public int getNumber() {&lt;br /&gt;
    return number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler1&lt;br /&gt;
public class NegativeProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;lt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;NegativeProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler2&lt;br /&gt;
public class ZeroProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() == 0) {&lt;br /&gt;
      System.out.println(&amp;quot;ZeroProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler3&lt;br /&gt;
public class PositiveProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;gt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;PositiveProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class TestChain {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    //configure Chain of Responsibility&lt;br /&gt;
    Chain c1 = new NegativeProcessor();&lt;br /&gt;
    Chain c2 = new ZeroProcessor();&lt;br /&gt;
    Chain c3 = new PositiveProcessor();&lt;br /&gt;
    c1.setNext(c2);&lt;br /&gt;
    c2.setNext(c3);&lt;br /&gt;
 &lt;br /&gt;
    //calling chain of responsibility&lt;br /&gt;
    c1.process(new Number(99));&lt;br /&gt;
    c1.process(new Number(-30));&lt;br /&gt;
    c1.process(new Number(0));&lt;br /&gt;
    c1.process(new Number(100));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example the Chain interface is implemented by three handlers which handles 3 different types of numbers-positive numbers,negative numbers and the number zero. The first in the chain is the negative number handler which sets the next handler as the zero handler which in turn sets the positive handler as the last component in the chain.The Number class acts as the receiver which has been explained in the Command pattern.The TestChain is the client.&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
# Both in Command and Chain of Responsibility  pattern commands or actions are stored so that it can be later used.&lt;br /&gt;
# The Chain of Responsibility forwards requests along a chain of classes, but the Command pattern forwards a request only to a specific object. &lt;br /&gt;
# The main intent of both the patterns is to decouple senders and receivers.In case of Command pattern that is only one.&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
Chain of Responsibility is used in many applications:&lt;br /&gt;
# It is used often to handle exceptions inside kernel.There will be a chain of interrupt handlers and the request is passed on until somebody handles it.&lt;br /&gt;
# The pattern is used in windows systems to handle events generated from the keyboard or mouse.&lt;br /&gt;
# Single sign on security solutions for web applications.  You might have a handler to check if the user is already authenticated, another handler to check for windows authentication, and a last handler to transfer the request to a logon page&amp;lt;ref&amp;gt;http://codebetter.com/jeremymiller/2005/11/07/using-the-chain-of-responsibility-pattern/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
The memento pattern is a software design pattern that facilitates the restoration an object to its previous state. The memento pattern has three different components:&amp;lt;ref&amp;gt;http://www.colourcoding.net/blog/archive/2009/07/23/reversibility-patterns-memento-and-command.aspx&amp;lt;/ref&amp;gt;&lt;br /&gt;
# Originator - the object that has an internal state and knows to save itself.&lt;br /&gt;
# Caretaker - the object that knows why and when the Originator needs to save and restore itself.&lt;br /&gt;
# Memento - the object (token) that is written and read by the Originator, and taken care by the Caretaker.&lt;br /&gt;
&lt;br /&gt;
The communication between different components in memento pattern happens in the following way:&lt;br /&gt;
The caretaker first asks the originator for a memento object. Then it does the work it was slated to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.In other words, the memento pattern can be viewed as maintaining a &amp;quot;checkpoint&amp;quot; so that the originator can easily rollback to the previous checkpoint.&amp;lt;ref&amp;gt;http://sourcemaking.com/design_patterns/memento&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following diagram illustrates the memento pattern:&amp;lt;br/&amp;gt;&lt;br /&gt;
[[File:Memento.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Example of Memento Pattern ==&lt;br /&gt;
&lt;br /&gt;
The following program illustrates the &amp;quot;undo&amp;quot; usage of the Memento Pattern &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Memento_pattern&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
class Originator {&lt;br /&gt;
    private String state;&lt;br /&gt;
    // The class could also contain additional data that is not part of the&lt;br /&gt;
    // state saved in the memento.&lt;br /&gt;
 &lt;br /&gt;
    public void set(String state) {&lt;br /&gt;
        System.out.println(&amp;quot;Originator: Setting state to &amp;quot; + state);&lt;br /&gt;
        this.state = state;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public Memento saveToMemento() {&lt;br /&gt;
        System.out.println(&amp;quot;Originator: Saving to Memento.&amp;quot;);&lt;br /&gt;
        return new Memento(state);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public void restoreFromMemento(Memento memento) {&lt;br /&gt;
        state = memento.getSavedState();&lt;br /&gt;
        System.out.println(&amp;quot;Originator: State after restoring from Memento: &amp;quot; + state);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public static class Memento {&lt;br /&gt;
        private final String state;&lt;br /&gt;
 &lt;br /&gt;
        public Memento(String stateToSave) {&lt;br /&gt;
            state = stateToSave;&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        public String getSavedState() {&lt;br /&gt;
            return state;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Caretaker {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List&amp;lt;Originator.Memento&amp;gt; savedStates = new ArrayList&amp;lt;Originator.Memento&amp;gt;();&lt;br /&gt;
 &lt;br /&gt;
        Originator originator = new Originator();&lt;br /&gt;
        originator.set(&amp;quot;State1&amp;quot;);&lt;br /&gt;
        originator.set(&amp;quot;State2&amp;quot;);&lt;br /&gt;
        savedStates.add(originator.saveToMemento());&lt;br /&gt;
        originator.set(&amp;quot;State3&amp;quot;);&lt;br /&gt;
        // We can request multiple mementos, and choose which one to roll back to.&lt;br /&gt;
        savedStates.add(originator.saveToMemento());&lt;br /&gt;
        originator.set(&amp;quot;State4&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
        originator.restoreFromMemento(savedStates.get(1));   &lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output is:&lt;br /&gt;
 Originator: Setting state to State1&lt;br /&gt;
 Originator: Setting state to State2&lt;br /&gt;
 Originator: Saving to Memento.&lt;br /&gt;
 Originator: Setting state to State3&lt;br /&gt;
 Originator: Saving to Memento.&lt;br /&gt;
 Originator: Setting state to State4&lt;br /&gt;
 Originator: State after restoring from Memento: State3&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
#The similarity between Command and Memento act as magic tokens to be passed around and invoked at a later time. In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time. &lt;br /&gt;
#Polymorphism is important to Command, but not to Memento because its interface is so narrow that a memento can only be passed as a value.&lt;br /&gt;
#Command can use Memento to maintain the state required for an undo operation.&lt;br /&gt;
#In practice, the Memento pattern is a little brittle. Changes in the behavior of related objects could lead to changes in what has to be stored. However since command pattern separates out the responsibility for reversibility into the relevant transitions, it proves to be less brittle.  Furthermore, command objects can be extended in further directions, taking in permissions, interruptibility, batching, to name but a few.&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The following are the applications where memento pattern can be used:&lt;br /&gt;
#An interesting game in which memento pattern can be used is Prince of Persia: Sands of Time. In the game, you can hit a button that reverses time. By storing the state of every actor in recent frames, it can just as easily rewind them.   &lt;br /&gt;
# Another simple application where memento pattern can be used is a calculator that finds the result of addition of two numbers, with the additional option to undo last operation and restore previous result.&lt;br /&gt;
# Memento pattern is useful when you need to find the seed of a pseudo random number generator and the state in a finite state machine.&lt;br /&gt;
&lt;br /&gt;
In all the above applications the &amp;quot;undo&amp;quot; feature is common. Hence, An unlimited “undo” and “redo” capability can be readily implemented with a stack of Command objects and a stack of Memento objects.&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
The unofficially accepted definition for the Strategy Pattern is: ''&amp;quot;Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.&amp;quot;''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern&amp;lt;/ref&amp;gt;. In other words, the Strategy Pattern encapsulates a collection of functions that do more or less similar tasks but not identical tasks. Strategy pattern is used when we need to take a decision of which strategy to use based on the input parameters. An important feature of strategy pattern is that client is aware of all the available strategies and which strategy to adopt. It helps is design a system that is elegant, extensible, and powerful. The following diagram illustrates the strategy pattern: &amp;lt;br/&amp;gt;&lt;br /&gt;
[[File:Strategy.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The following example is in Java.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// The classes that implement a concrete strategy should implement this.&lt;br /&gt;
// The Context class uses this to call the concrete strategy.&lt;br /&gt;
interface IStrategy {&lt;br /&gt;
    int execute(int a, int b); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Implements the algorithm using the strategy interface&lt;br /&gt;
class ConcreteStrategyAdd implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategyAdd's execute()&amp;quot;);&lt;br /&gt;
        return a + b;  // Do an addition with a and b&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ConcreteStrategySubtract implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategySubtract's execute()&amp;quot;);&lt;br /&gt;
        return a - b;  // Do a subtraction with a and b&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ConcreteStrategyMultiply implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategyMultiply's execute()&amp;quot;);&lt;br /&gt;
        return a * b;   // Do a multiplication with a and b&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object&lt;br /&gt;
class Context {&lt;br /&gt;
&lt;br /&gt;
    private IStrategy strategy;&lt;br /&gt;
&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Context(IStrategy strategy) {&lt;br /&gt;
        this.strategy = strategy;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int executeStrategy(int a, int b) {&lt;br /&gt;
        return strategy.execute(a, b);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Test application&lt;br /&gt;
class StrategyExample {&lt;br /&gt;
&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
&lt;br /&gt;
        Context context;&lt;br /&gt;
&lt;br /&gt;
        // Three contexts following different strategies&lt;br /&gt;
        context = new Context(new ConcreteStrategyAdd());&lt;br /&gt;
        int resultA = context.executeStrategy(3,4);&lt;br /&gt;
&lt;br /&gt;
        context = new Context(new ConcreteStrategySubtract());&lt;br /&gt;
        int resultB = context.executeStrategy(3,4);&lt;br /&gt;
&lt;br /&gt;
        context = new Context(new ConcreteStrategyMultiply());&lt;br /&gt;
        int resultC = context.executeStrategy(3,4);&lt;br /&gt;
     &lt;br /&gt;
        System.out.println(&amp;quot;Result A : &amp;quot; + resultA );&lt;br /&gt;
        System.out.println(&amp;quot;Result B : &amp;quot; + resultB );&lt;br /&gt;
        System.out.println(&amp;quot;Result C : &amp;quot; + resultC );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
The difference between Strategy Pattern and Command pattern is in the purpose itself:&lt;br /&gt;
#Command encapsulates a single action. It therefore tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later - or it is used to provide undo functionality as explained in the Command pattern section.Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded later.&lt;br /&gt;
#Strategies encapsulate algorithms. Commands separate the sender from the receiver of a request, they turn a request into an object. If it's an algorithm, how something will be done, use a Strategy. If you need to separate the call of a method from its execution use a Command. &lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The applications where strategy pattern is useful are as follows:&lt;br /&gt;
#Strategy pattern is used in an system where we try to book the most inexpensive ticket from source to destination. Depending on the users preference of seats, time of transit and other preferences the system chooses the algorithm that will cater to all the user needs at an optimum fare.&lt;br /&gt;
#Strategy pattern can be used when we have to sort numbers. Depending on the type of input, we can make use of the best algorithm. For example, if we know that the numbers are almost sorted then we make use of insertion sort. If the numbers to be sorted are within a range, then we make use of counting sort. If the input sequence is random then our best bet is to use merge sort.&lt;br /&gt;
# A strategy pattern can be used when we want to read a file that has been transferred over a network. If the file is an XML File then use an XML parsing algorithm. If it is JSON file then use a JSON parser and so on.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
Thus, we see that Command Pattern is quite useful. The Chain of Responsibility, Memento ad Strategy pattern adopt the concept of Command patterns and have their own variations which make them useful in different scenarios as mentioned in each of the sections. The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that knows how to perform it. The major disadvantage of the pattern is that it results in many Command classes that can clutter up a design. If the classes are not designed properly it may lead to bloating of the design and will increase the cost of maintaining such a design.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
# [http://www.oodesign.com/command-pattern.html]&lt;br /&gt;
# [http://www.go4expert.com/forums/showthread.php?t=5127#command]&lt;br /&gt;
# [http://www.mydeveloperconnection.com/html/gof_design_patterns.htm]&lt;br /&gt;
# [http://java.dzone.com/articles/design-patterns-command]&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=71164</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=71164"/>
		<updated>2012-11-20T04:48:44Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Command_pattern&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern&amp;lt;/ref&amp;gt;.&lt;br /&gt;
== Example of Chain of Responsibility ==&lt;br /&gt;
[[File:COR.jpg]]&lt;br /&gt;
&lt;br /&gt;
The above figure helps to understand the workings of Chain of Responsibility pattern.The example given in &amp;lt;ref&amp;gt;http://javapapers.com/design-patterns/chain-of-responsibility-design-pattern/&amp;lt;/ref&amp;gt; has been described below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Interface&lt;br /&gt;
public interface Chain {&lt;br /&gt;
 &lt;br /&gt;
  public abstract void setNext(Chain nextInChain);&lt;br /&gt;
  public abstract void process(Number request);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Number {&lt;br /&gt;
  private int number;&lt;br /&gt;
 &lt;br /&gt;
  public Number(int number) {&lt;br /&gt;
    this.number = number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public int getNumber() {&lt;br /&gt;
    return number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler1&lt;br /&gt;
public class NegativeProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;lt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;NegativeProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler2&lt;br /&gt;
public class ZeroProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() == 0) {&lt;br /&gt;
      System.out.println(&amp;quot;ZeroProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler3&lt;br /&gt;
public class PositiveProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;gt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;PositiveProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class TestChain {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    //configure Chain of Responsibility&lt;br /&gt;
    Chain c1 = new NegativeProcessor();&lt;br /&gt;
    Chain c2 = new ZeroProcessor();&lt;br /&gt;
    Chain c3 = new PositiveProcessor();&lt;br /&gt;
    c1.setNext(c2);&lt;br /&gt;
    c2.setNext(c3);&lt;br /&gt;
 &lt;br /&gt;
    //calling chain of responsibility&lt;br /&gt;
    c1.process(new Number(99));&lt;br /&gt;
    c1.process(new Number(-30));&lt;br /&gt;
    c1.process(new Number(0));&lt;br /&gt;
    c1.process(new Number(100));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example the Chain interface is implemented by three handlers which handles 3 different types of numbers-positive numbers,negative numbers and the number zero. The first in the chain is the negative number handler which sets the next handler as the zero handler which in turn sets the positive handler as the last component in the chain.The Number class acts as the receiver which has been explained in the Command pattern.The TestChain is the client.&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
# Both in Command and Chain of Responsibility  pattern commands or actions are stored so that it can be later used.&lt;br /&gt;
# The Chain of Responsibility forwards requests along a chain of classes, but the Command pattern forwards a request only to a specific object. &lt;br /&gt;
# The main intent of both the patterns is to decouple senders and receivers.In case of Command pattern that is only one.&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
Chain of Responsibility is used in many applications:&lt;br /&gt;
# It is used often to handle exceptions inside kernel.There will be a chain of interrupt handlers and the request is passed on until somebody handles it.&lt;br /&gt;
# The pattern is used in windows systems to handle events generated from the keyboard or mouse.&lt;br /&gt;
# Single sign on security solutions for web applications.  You might have a handler to check if the user is already authenticated, another handler to check for windows authentication, and a last handler to transfer the request to a logon page&amp;lt;ref&amp;gt;http://codebetter.com/jeremymiller/2005/11/07/using-the-chain-of-responsibility-pattern/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
The memento pattern is a software design pattern that facilitates the restoration an object to its previous state. The memento pattern has three different components:&amp;lt;ref&amp;gt;http://www.colourcoding.net/blog/archive/2009/07/23/reversibility-patterns-memento-and-command.aspx&amp;lt;/ref&amp;gt;&lt;br /&gt;
# Originator - the object that has an internal state and knows to save itself.&lt;br /&gt;
# Caretaker - the object that knows why and when the Originator needs to save and restore itself.&lt;br /&gt;
# Memento - the object (token) that is written and read by the Originator, and taken care by the Caretaker.&lt;br /&gt;
&lt;br /&gt;
The communication between different components in memento pattern happens in the following way:&lt;br /&gt;
The caretaker first asks the originator for a memento object. Then it does the work it was slated to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.In other words, the memento pattern can be viewed as maintaining a &amp;quot;checkpoint&amp;quot; so that the originator can easily rollback to the previous checkpoint.&amp;lt;ref&amp;gt;http://sourcemaking.com/design_patterns/memento&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following diagram illustrates the memento pattern:&amp;lt;br/&amp;gt;&lt;br /&gt;
[[File:Memento.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Example of Memento Pattern ==&lt;br /&gt;
&lt;br /&gt;
The following program illustrates the &amp;quot;undo&amp;quot; usage of the Memento Pattern &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Memento_pattern&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
class Originator {&lt;br /&gt;
    private String state;&lt;br /&gt;
    // The class could also contain additional data that is not part of the&lt;br /&gt;
    // state saved in the memento.&lt;br /&gt;
 &lt;br /&gt;
    public void set(String state) {&lt;br /&gt;
        System.out.println(&amp;quot;Originator: Setting state to &amp;quot; + state);&lt;br /&gt;
        this.state = state;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public Memento saveToMemento() {&lt;br /&gt;
        System.out.println(&amp;quot;Originator: Saving to Memento.&amp;quot;);&lt;br /&gt;
        return new Memento(state);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public void restoreFromMemento(Memento memento) {&lt;br /&gt;
        state = memento.getSavedState();&lt;br /&gt;
        System.out.println(&amp;quot;Originator: State after restoring from Memento: &amp;quot; + state);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public static class Memento {&lt;br /&gt;
        private final String state;&lt;br /&gt;
 &lt;br /&gt;
        public Memento(String stateToSave) {&lt;br /&gt;
            state = stateToSave;&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        public String getSavedState() {&lt;br /&gt;
            return state;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Caretaker {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List&amp;lt;Originator.Memento&amp;gt; savedStates = new ArrayList&amp;lt;Originator.Memento&amp;gt;();&lt;br /&gt;
 &lt;br /&gt;
        Originator originator = new Originator();&lt;br /&gt;
        originator.set(&amp;quot;State1&amp;quot;);&lt;br /&gt;
        originator.set(&amp;quot;State2&amp;quot;);&lt;br /&gt;
        savedStates.add(originator.saveToMemento());&lt;br /&gt;
        originator.set(&amp;quot;State3&amp;quot;);&lt;br /&gt;
        // We can request multiple mementos, and choose which one to roll back to.&lt;br /&gt;
        savedStates.add(originator.saveToMemento());&lt;br /&gt;
        originator.set(&amp;quot;State4&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
        originator.restoreFromMemento(savedStates.get(1));   &lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output is:&lt;br /&gt;
 Originator: Setting state to State1&lt;br /&gt;
 Originator: Setting state to State2&lt;br /&gt;
 Originator: Saving to Memento.&lt;br /&gt;
 Originator: Setting state to State3&lt;br /&gt;
 Originator: Saving to Memento.&lt;br /&gt;
 Originator: Setting state to State4&lt;br /&gt;
 Originator: State after restoring from Memento: State3&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
#The similarity between Command and Memento act as magic tokens to be passed around and invoked at a later time. In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time. &lt;br /&gt;
#Polymorphism is important to Command, but not to Memento because its interface is so narrow that a memento can only be passed as a value.&lt;br /&gt;
#Command can use Memento to maintain the state required for an undo operation.&lt;br /&gt;
#In practice, the Memento pattern is a little brittle. Changes in the behavior of related objects could lead to changes in what has to be stored. However since command pattern separates out the responsibility for reversibility into the relevant transitions, it proves to be less brittle.  Furthermore, command objects can be extended in further directions, taking in permissions, interruptibility, batching, to name but a few.&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The following are the applications where memento pattern can be used:&lt;br /&gt;
#An interesting game in which memento pattern can be used is Prince of Persia: Sands of Time. In the game, you can hit a button that reverses time. By storing the state of every actor in recent frames, it can just as easily rewind them.   &lt;br /&gt;
# Another simple application where memento pattern can be used is a calculator that finds the result of addition of two numbers, with the additional option to undo last operation and restore previous result.&lt;br /&gt;
# Memento pattern is useful when you need to find the seed of a pseudo random number generator and the state in a finite state machine.&lt;br /&gt;
&lt;br /&gt;
In all the above applications the &amp;quot;undo&amp;quot; feature is common. Hence, An unlimited “undo” and “redo” capability can be readily implemented with a stack of Command objects and a stack of Memento objects.&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
The unofficially accepted definition for the Strategy Pattern is: ''&amp;quot;Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.&amp;quot;''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern&amp;lt;/ref&amp;gt;. In other words, the Strategy Pattern encapsulates a collection of functions that do more or less similar tasks but not identical tasks. Strategy pattern is used when we need to take a decision of which strategy to use based on the input parameters. An important feature of strategy pattern is that client is aware of all the available strategies and which strategy to adopt. It helps is design a system that is elegant, extensible, and powerful. The following diagram illustrates the strategy pattern: &amp;lt;br/&amp;gt;&lt;br /&gt;
[[File:Strategy.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The following example is in Java.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// The classes that implement a concrete strategy should implement this.&lt;br /&gt;
// The Context class uses this to call the concrete strategy.&lt;br /&gt;
interface IStrategy {&lt;br /&gt;
    int execute(int a, int b); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Implements the algorithm using the strategy interface&lt;br /&gt;
class ConcreteStrategyAdd implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategyAdd's execute()&amp;quot;);&lt;br /&gt;
        return a + b;  // Do an addition with a and b&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ConcreteStrategySubtract implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategySubtract's execute()&amp;quot;);&lt;br /&gt;
        return a - b;  // Do a subtraction with a and b&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ConcreteStrategyMultiply implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategyMultiply's execute()&amp;quot;);&lt;br /&gt;
        return a * b;   // Do a multiplication with a and b&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object&lt;br /&gt;
class Context {&lt;br /&gt;
&lt;br /&gt;
    private IStrategy strategy;&lt;br /&gt;
&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Context(IStrategy strategy) {&lt;br /&gt;
        this.strategy = strategy;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int executeStrategy(int a, int b) {&lt;br /&gt;
        return strategy.execute(a, b);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Test application&lt;br /&gt;
class StrategyExample {&lt;br /&gt;
&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
&lt;br /&gt;
        Context context;&lt;br /&gt;
&lt;br /&gt;
        // Three contexts following different strategies&lt;br /&gt;
        context = new Context(new ConcreteStrategyAdd());&lt;br /&gt;
        int resultA = context.executeStrategy(3,4);&lt;br /&gt;
&lt;br /&gt;
        context = new Context(new ConcreteStrategySubtract());&lt;br /&gt;
        int resultB = context.executeStrategy(3,4);&lt;br /&gt;
&lt;br /&gt;
        context = new Context(new ConcreteStrategyMultiply());&lt;br /&gt;
        int resultC = context.executeStrategy(3,4);&lt;br /&gt;
     &lt;br /&gt;
        System.out.println(&amp;quot;Result A : &amp;quot; + resultA );&lt;br /&gt;
        System.out.println(&amp;quot;Result B : &amp;quot; + resultB );&lt;br /&gt;
        System.out.println(&amp;quot;Result C : &amp;quot; + resultC );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
The difference between Strategy Pattern and Command pattern is in the purpose itself:&lt;br /&gt;
#Command encapsulates a single action. It therefore tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later - or it is used to provide undo functionality as explained in the Command pattern section.Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded later.&lt;br /&gt;
#Strategies encapsulate algorithms. Commands separate the sender from the receiver of a request, they turn a request into an object. If it's an algorithm, how something will be done, use a Strategy. If you need to separate the call of a method from its execution use a Command. &lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The applications where strategy pattern is useful are as follows:&lt;br /&gt;
#Strategy pattern is used in an system where we try to book the most inexpensive ticket from source to destination. Depending on the users preference of seats, time of transit and other preferences the system chooses the algorithm that will cater to all the user needs at an optimum fare.&lt;br /&gt;
#Strategy pattern can be used when we have to sort numbers. Depending on the type of input, we can make use of the best algorithm. For example, if we know that the numbers are almost sorted then we make use of insertion sort. If the numbers to be sorted are within a range, then we make use of counting sort. If the input sequence is random then our best bet is to use merge sort.&lt;br /&gt;
# A strategy pattern can be used when we want to read a file that has been transferred over a network. If the file is an XML File then use an XML parsing algorithm. If it is JSON file then use a JSON parser and so on.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
Thus, we see that Command Pattern is quite useful. The Chain of Responsibility, Memento ad Strategy pattern adopt the concept of Command patterns and have their own variations which make them useful in different scenarios as mentioned in each of the sections. The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that knows how to perform it. The major disadvantage of the pattern is that it results in many Command classes that can clutter up a design. If the classes are not designed properly it may lead to bloating of the design and will increase the cost of maintaining such a design.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=71163</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=71163"/>
		<updated>2012-11-20T04:45:46Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this [http://en.wikipedia.org/wiki/Command_pattern].&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern].&lt;br /&gt;
== Example of Chain of Responsibility ==&lt;br /&gt;
[[File:COR.jpg]]&lt;br /&gt;
&lt;br /&gt;
The above figure helps to understand the workings of Chain of Responsibility pattern.The example given in [http://javapapers.com/design-patterns/chain-of-responsibility-design-pattern/] has been described below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Interface&lt;br /&gt;
public interface Chain {&lt;br /&gt;
 &lt;br /&gt;
  public abstract void setNext(Chain nextInChain);&lt;br /&gt;
  public abstract void process(Number request);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Number {&lt;br /&gt;
  private int number;&lt;br /&gt;
 &lt;br /&gt;
  public Number(int number) {&lt;br /&gt;
    this.number = number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public int getNumber() {&lt;br /&gt;
    return number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler1&lt;br /&gt;
public class NegativeProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;lt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;NegativeProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler2&lt;br /&gt;
public class ZeroProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() == 0) {&lt;br /&gt;
      System.out.println(&amp;quot;ZeroProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler3&lt;br /&gt;
public class PositiveProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;gt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;PositiveProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class TestChain {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    //configure Chain of Responsibility&lt;br /&gt;
    Chain c1 = new NegativeProcessor();&lt;br /&gt;
    Chain c2 = new ZeroProcessor();&lt;br /&gt;
    Chain c3 = new PositiveProcessor();&lt;br /&gt;
    c1.setNext(c2);&lt;br /&gt;
    c2.setNext(c3);&lt;br /&gt;
 &lt;br /&gt;
    //calling chain of responsibility&lt;br /&gt;
    c1.process(new Number(99));&lt;br /&gt;
    c1.process(new Number(-30));&lt;br /&gt;
    c1.process(new Number(0));&lt;br /&gt;
    c1.process(new Number(100));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example the Chain interface is implemented by three handlers which handles 3 different types of numbers-positive numbers,negative numbers and the number zero. The first in the chain is the negative number handler which sets the next handler as the zero handler which in turn sets the positive handler as the last component in the chain.The Number class acts as the receiver which has been explained in the Command pattern.The TestChain is the client.&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
# Both in Command and Chain of Responsibility  pattern commands or actions are stored so that it can be later used.&lt;br /&gt;
# The Chain of Responsibility forwards requests along a chain of classes, but the Command pattern forwards a request only to a specific object. &lt;br /&gt;
# The main intent of both the patterns is to decouple senders and receivers.In case of Command pattern that is only one.&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
Chain of Responsibility is used in many applications:&lt;br /&gt;
# It is used often to handle exceptions inside kernel.There will be a chain of interrupt handlers and the request is passed on until somebody handles it.&lt;br /&gt;
# The pattern is used in windows systems to handle events generated from the keyboard or mouse.&lt;br /&gt;
# Single sign on security solutions for web applications.  You might have a handler to check if the user is already authenticated, another handler to check for windows authentication, and a last handler to transfer the request to a logon page[http://codebetter.com/jeremymiller/2005/11/07/using-the-chain-of-responsibility-pattern/].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
The memento pattern is a software design pattern that facilitates the restoration an object to its previous state. The memento pattern has three different components:[http://www.colourcoding.net/blog/archive/2009/07/23/reversibility-patterns-memento-and-command.aspx]&lt;br /&gt;
# Originator - the object that has an internal state and knows to save itself.&lt;br /&gt;
# Caretaker - the object that knows why and when the Originator needs to save and restore itself.&lt;br /&gt;
# Memento - the object (token) that is written and read by the Originator, and taken care by the Caretaker.&lt;br /&gt;
&lt;br /&gt;
The communication between different components in memento pattern happens in the following way:&lt;br /&gt;
The caretaker first asks the originator for a memento object. Then it does the work it was slated to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.In other words, the memento pattern can be viewed as maintaining a &amp;quot;checkpoint&amp;quot; so that the originator can easily rollback to the previous checkpoint.[http://sourcemaking.com/design_patterns/memento]&lt;br /&gt;
&lt;br /&gt;
The following diagram illustrates the memento pattern:&amp;lt;br/&amp;gt;&lt;br /&gt;
[[File:Memento.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Example of Memento Pattern ==&lt;br /&gt;
&lt;br /&gt;
The following program illustrates the &amp;quot;undo&amp;quot; usage of the Memento Pattern [http://en.wikipedia.org/wiki/Memento_pattern].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
class Originator {&lt;br /&gt;
    private String state;&lt;br /&gt;
    // The class could also contain additional data that is not part of the&lt;br /&gt;
    // state saved in the memento.&lt;br /&gt;
 &lt;br /&gt;
    public void set(String state) {&lt;br /&gt;
        System.out.println(&amp;quot;Originator: Setting state to &amp;quot; + state);&lt;br /&gt;
        this.state = state;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public Memento saveToMemento() {&lt;br /&gt;
        System.out.println(&amp;quot;Originator: Saving to Memento.&amp;quot;);&lt;br /&gt;
        return new Memento(state);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public void restoreFromMemento(Memento memento) {&lt;br /&gt;
        state = memento.getSavedState();&lt;br /&gt;
        System.out.println(&amp;quot;Originator: State after restoring from Memento: &amp;quot; + state);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public static class Memento {&lt;br /&gt;
        private final String state;&lt;br /&gt;
 &lt;br /&gt;
        public Memento(String stateToSave) {&lt;br /&gt;
            state = stateToSave;&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        public String getSavedState() {&lt;br /&gt;
            return state;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Caretaker {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List&amp;lt;Originator.Memento&amp;gt; savedStates = new ArrayList&amp;lt;Originator.Memento&amp;gt;();&lt;br /&gt;
 &lt;br /&gt;
        Originator originator = new Originator();&lt;br /&gt;
        originator.set(&amp;quot;State1&amp;quot;);&lt;br /&gt;
        originator.set(&amp;quot;State2&amp;quot;);&lt;br /&gt;
        savedStates.add(originator.saveToMemento());&lt;br /&gt;
        originator.set(&amp;quot;State3&amp;quot;);&lt;br /&gt;
        // We can request multiple mementos, and choose which one to roll back to.&lt;br /&gt;
        savedStates.add(originator.saveToMemento());&lt;br /&gt;
        originator.set(&amp;quot;State4&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
        originator.restoreFromMemento(savedStates.get(1));   &lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output is:&lt;br /&gt;
 Originator: Setting state to State1&lt;br /&gt;
 Originator: Setting state to State2&lt;br /&gt;
 Originator: Saving to Memento.&lt;br /&gt;
 Originator: Setting state to State3&lt;br /&gt;
 Originator: Saving to Memento.&lt;br /&gt;
 Originator: Setting state to State4&lt;br /&gt;
 Originator: State after restoring from Memento: State3&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
#The similarity between Command and Memento act as magic tokens to be passed around and invoked at a later time. In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time. &lt;br /&gt;
#Polymorphism is important to Command, but not to Memento because its interface is so narrow that a memento can only be passed as a value.&lt;br /&gt;
#Command can use Memento to maintain the state required for an undo operation.&lt;br /&gt;
#In practice, the Memento pattern is a little brittle. Changes in the behavior of related objects could lead to changes in what has to be stored. However since command pattern separates out the responsibility for reversibility into the relevant transitions, it proves to be less brittle.  Furthermore, command objects can be extended in further directions, taking in permissions, interruptibility, batching, to name but a few.&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The following are the applications where memento pattern can be used:&lt;br /&gt;
#An interesting game in which memento pattern can be used is Prince of Persia: Sands of Time. In the game, you can hit a button that reverses time. By storing the state of every actor in recent frames, it can just as easily rewind them.   &lt;br /&gt;
# Another simple application where memento pattern can be used is a calculator that finds the result of addition of two numbers, with the additional option to undo last operation and restore previous result.&lt;br /&gt;
# Memento pattern is useful when you need to find the seed of a pseudorandom number generator and the state in a finite state machine.&lt;br /&gt;
&lt;br /&gt;
In all the above applications the &amp;quot;undo&amp;quot; feature is common. Hence, An unlimited “undo” and “redo” capability can be readily implemented with a stack of Command objects and a stack of Memento objects.&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
The unofficially accepted definition for the Strategy Pattern is: ''&amp;quot;Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.&amp;quot;''[http://en.wikipedia.org/wiki/Strategy_pattern]. In other words, the Strategy Pattern encapsulates a collection of functions that do more or less similar tasks but not identical tasks. Strategy pattern is used when we need to take a decision of which strategy to use based on the input parameters. An important feature of strategy pattern is that client is aware of all the available strategies and which strategy to adopt. It helps is design a system that is elegant, extensible, and powerful. The following diagram illustrates the strategy pattern: &amp;lt;br/&amp;gt;&lt;br /&gt;
[[File:Strategy.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The following example is in Java.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// The classes that implement a concrete strategy should implement this.&lt;br /&gt;
// The Context class uses this to call the concrete strategy.&lt;br /&gt;
interface IStrategy {&lt;br /&gt;
    int execute(int a, int b); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Implements the algorithm using the strategy interface&lt;br /&gt;
class ConcreteStrategyAdd implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategyAdd's execute()&amp;quot;);&lt;br /&gt;
        return a + b;  // Do an addition with a and b&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ConcreteStrategySubtract implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategySubtract's execute()&amp;quot;);&lt;br /&gt;
        return a - b;  // Do a subtraction with a and b&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ConcreteStrategyMultiply implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategyMultiply's execute()&amp;quot;);&lt;br /&gt;
        return a * b;   // Do a multiplication with a and b&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object&lt;br /&gt;
class Context {&lt;br /&gt;
&lt;br /&gt;
    private IStrategy strategy;&lt;br /&gt;
&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Context(IStrategy strategy) {&lt;br /&gt;
        this.strategy = strategy;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int executeStrategy(int a, int b) {&lt;br /&gt;
        return strategy.execute(a, b);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Test application&lt;br /&gt;
class StrategyExample {&lt;br /&gt;
&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
&lt;br /&gt;
        Context context;&lt;br /&gt;
&lt;br /&gt;
        // Three contexts following different strategies&lt;br /&gt;
        context = new Context(new ConcreteStrategyAdd());&lt;br /&gt;
        int resultA = context.executeStrategy(3,4);&lt;br /&gt;
&lt;br /&gt;
        context = new Context(new ConcreteStrategySubtract());&lt;br /&gt;
        int resultB = context.executeStrategy(3,4);&lt;br /&gt;
&lt;br /&gt;
        context = new Context(new ConcreteStrategyMultiply());&lt;br /&gt;
        int resultC = context.executeStrategy(3,4);&lt;br /&gt;
     &lt;br /&gt;
        System.out.println(&amp;quot;Result A : &amp;quot; + resultA );&lt;br /&gt;
        System.out.println(&amp;quot;Result B : &amp;quot; + resultB );&lt;br /&gt;
        System.out.println(&amp;quot;Result C : &amp;quot; + resultC );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
The difference between Strategy Pattern and Command pattern is in the purpose itself:&lt;br /&gt;
#Command encapsulates a single action. It therefore tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later - or it is used to provide undo functionality as explained in the Command pattern section.Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded later.&lt;br /&gt;
#Strategies encapsulate algorithms. Commands separate the sender from the receiver of a request, they turn a request into an object. If it's an algorithm, how something will be done, use a Strategy. If you need to separate the call of a method from its execution use a Command. &lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The applications where strategy pattern is useful are as follows:&lt;br /&gt;
#Strategy pattern is used in an system where we try to book the most inexpensive ticket from source to destination. Depending on the users preference of seats, time of transit and other preferences the system chooses the algorithm that will cater to all the user needs at an optimum fare.&lt;br /&gt;
#Strategy pattern can be used when we have to sort numbers. Depending on the type of input, we can make use of the best algorithm. For example, if we know that the numbers are almost sorted then we make use of insertion sort. If the numbers to be sorted are within a range, then we make use of counting sort. If the input sequence is random then our best bet is to use merge sort.&lt;br /&gt;
# A strategy pattern can be used when we want to read a file that has been transferred over a network. If the file is an XML File then use an XML parsing algorithm. If it is JSON file then use a JSON parser and so on.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
Thus, we see that Command Pattern is quite useful. The Chain of Responsibility, Memento ad Strategy pattern adopt the concept of Command patterns and have their own variations which make them useful in different scenarios as mentioned in each of the sections. The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that knows how to perform it. The major disadvantage of the pattern is that it results in many Command classes that can clutter up a design. If the classes are not designed properly it may lead to bloating of the design and will increase the cost of maintaining such a design.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=71162</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=71162"/>
		<updated>2012-11-20T04:44:18Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this [http://en.wikipedia.org/wiki/Command_pattern].&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern].&lt;br /&gt;
== Example of Chain of Responsibility ==&lt;br /&gt;
[[File:COR.jpg]]&lt;br /&gt;
&lt;br /&gt;
The above figure helps to understand the workings of Chain of Responsibility pattern.The example given in [http://javapapers.com/design-patterns/chain-of-responsibility-design-pattern/] has been described below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Interface&lt;br /&gt;
public interface Chain {&lt;br /&gt;
 &lt;br /&gt;
  public abstract void setNext(Chain nextInChain);&lt;br /&gt;
  public abstract void process(Number request);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Number {&lt;br /&gt;
  private int number;&lt;br /&gt;
 &lt;br /&gt;
  public Number(int number) {&lt;br /&gt;
    this.number = number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public int getNumber() {&lt;br /&gt;
    return number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler1&lt;br /&gt;
public class NegativeProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;lt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;NegativeProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler2&lt;br /&gt;
public class ZeroProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() == 0) {&lt;br /&gt;
      System.out.println(&amp;quot;ZeroProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler3&lt;br /&gt;
public class PositiveProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;gt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;PositiveProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class TestChain {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    //configure Chain of Responsibility&lt;br /&gt;
    Chain c1 = new NegativeProcessor();&lt;br /&gt;
    Chain c2 = new ZeroProcessor();&lt;br /&gt;
    Chain c3 = new PositiveProcessor();&lt;br /&gt;
    c1.setNext(c2);&lt;br /&gt;
    c2.setNext(c3);&lt;br /&gt;
 &lt;br /&gt;
    //calling chain of responsibility&lt;br /&gt;
    c1.process(new Number(99));&lt;br /&gt;
    c1.process(new Number(-30));&lt;br /&gt;
    c1.process(new Number(0));&lt;br /&gt;
    c1.process(new Number(100));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example the Chain interface is implemented by three handlers which handles 3 different types of numbers-positive numbers,negative numbers and the number zero. The first in the chain is the negative number handler which sets the next handler as the zero handler which in turn sets the positive handler as the last component in the chain.The Number class acts as the receiver which has been explained in the Command pattern.The TestChain is the client.&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
# Both in Command and Chain of Responsibility  pattern commands or actions are stored so that it can be later used.&lt;br /&gt;
# The Chain of Responsibility forwards requests along a chain of classes, but the Command pattern forwards a request only to a specific object. &lt;br /&gt;
# The main intent of both the patterns is to decouple senders and receivers.In case of Command pattern that is only one.&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
Chain of Responsibility is used in many applications:&lt;br /&gt;
# It is used often to handle exceptions inside kernel.There will be a chain of interrupt handlers and the request is passed on until somebody handles it.&lt;br /&gt;
# The pattern is used in windows systems to handle events generated from the keyboard or mouse.&lt;br /&gt;
# Single sign on security solutions for web applications.  You might have a handler to check if the user is already authenticated, another handler to check for windows authentication, and a last handler to transfer the request to a logon page[http://codebetter.com/jeremymiller/2005/11/07/using-the-chain-of-responsibility-pattern/].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
The memento pattern is a software design pattern that facilitates the restoration an object to its previous state. The memento pattern has three different components:[http://www.colourcoding.net/blog/archive/2009/07/23/reversibility-patterns-memento-and-command.aspx]&lt;br /&gt;
# Originator - the object that has an internal state and knows to save itself.&lt;br /&gt;
# Caretaker - the object that knows why and when the Originator needs to save and restore itself.&lt;br /&gt;
# Memento - the object (token) that is written and read by the Originator, and taken care by the Caretaker.&lt;br /&gt;
&lt;br /&gt;
The communication between different components in memento pattern happens in the following way:&lt;br /&gt;
The caretaker first asks the originator for a memento object. Then it does the work it was slated to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.In other words, the memento pattern can be viewed as maintaining a &amp;quot;checkpoint&amp;quot; so that the originator can easily rollback to the previous checkpoint.[http://sourcemaking.com/design_patterns/memento]&lt;br /&gt;
&lt;br /&gt;
The following diagram illustrates the memento pattern:&amp;lt;br/&amp;gt;&lt;br /&gt;
[[File:Memento.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Example of Memento Pattern ==&lt;br /&gt;
&lt;br /&gt;
The following program illustrates the &amp;quot;undo&amp;quot; usage of the Memento Pattern [http://en.wikipedia.org/wiki/Memento_pattern].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
class Originator {&lt;br /&gt;
    private String state;&lt;br /&gt;
    // The class could also contain additional data that is not part of the&lt;br /&gt;
    // state saved in the memento.&lt;br /&gt;
 &lt;br /&gt;
    public void set(String state) {&lt;br /&gt;
        System.out.println(&amp;quot;Originator: Setting state to &amp;quot; + state);&lt;br /&gt;
        this.state = state;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public Memento saveToMemento() {&lt;br /&gt;
        System.out.println(&amp;quot;Originator: Saving to Memento.&amp;quot;);&lt;br /&gt;
        return new Memento(state);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public void restoreFromMemento(Memento memento) {&lt;br /&gt;
        state = memento.getSavedState();&lt;br /&gt;
        System.out.println(&amp;quot;Originator: State after restoring from Memento: &amp;quot; + state);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    public static class Memento {&lt;br /&gt;
        private final String state;&lt;br /&gt;
 &lt;br /&gt;
        public Memento(String stateToSave) {&lt;br /&gt;
            state = stateToSave;&lt;br /&gt;
        }&lt;br /&gt;
 &lt;br /&gt;
        public String getSavedState() {&lt;br /&gt;
            return state;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Caretaker {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        List&amp;lt;Originator.Memento&amp;gt; savedStates = new ArrayList&amp;lt;Originator.Memento&amp;gt;();&lt;br /&gt;
 &lt;br /&gt;
        Originator originator = new Originator();&lt;br /&gt;
        originator.set(&amp;quot;State1&amp;quot;);&lt;br /&gt;
        originator.set(&amp;quot;State2&amp;quot;);&lt;br /&gt;
        savedStates.add(originator.saveToMemento());&lt;br /&gt;
        originator.set(&amp;quot;State3&amp;quot;);&lt;br /&gt;
        // We can request multiple mementos, and choose which one to roll back to.&lt;br /&gt;
        savedStates.add(originator.saveToMemento());&lt;br /&gt;
        originator.set(&amp;quot;State4&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
        originator.restoreFromMemento(savedStates.get(1));   &lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output is:&lt;br /&gt;
 Originator: Setting state to State1&lt;br /&gt;
 Originator: Setting state to State2&lt;br /&gt;
 Originator: Saving to Memento.&lt;br /&gt;
 Originator: Setting state to State3&lt;br /&gt;
 Originator: Saving to Memento.&lt;br /&gt;
 Originator: Setting state to State4&lt;br /&gt;
 Originator: State after restoring from Memento: State3&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
#The similarity between Command and Memento act as magic tokens to be passed around and invoked at a later time. In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time. &lt;br /&gt;
#Polymorphism is important to Command, but not to Memento because its interface is so narrow that a memento can only be passed as a value.&lt;br /&gt;
#Command can use Memento to maintain the state required for an undo operation.&lt;br /&gt;
#In practice, the Memento pattern is a little brittle. Changes in the behavior of related objects could lead to changes in what has to be stored. However since command pattern separates out the responsibility for reversibility into the relevant transitions, it proves to be less brittle.  Furthermore, command objects can be extended in further directions, taking in permissions, interruptibility, batching, to name but a few.&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The following are the applications where memento pattern can be used:&lt;br /&gt;
#An interesting game in which memento pattern can be used is Prince of Persia: Sands of Time. In the game, you can hit a button that reverses time. By storing the state of every actor in recent frames, it can just as easily rewind them.   &lt;br /&gt;
# Another simple application where memento pattern can be used is a calculator that finds the result of addition of two numbers, with the additional option to undo last operation and restore previous result.&lt;br /&gt;
# Memento pattern is useful when you need to find the seed of a pseudorandom number generator and the state in a finite state machine.&lt;br /&gt;
&lt;br /&gt;
In all the above applications the &amp;quot;undo&amp;quot; feature is common. Hence, An unlimited “undo” and “redo” capability can be readily implemented with a stack of Command objects and a stack of Memento objects.&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
The unofficially accepted definition for the Strategy Pattern is: ''&amp;quot;Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.&amp;quot;''[http://en.wikipedia.org/wiki/Strategy_pattern]. In other words, the Strategy Pattern encapsulates a collection of functions that do more or less similar tasks but not identical tasks. Strategy pattern is used when we need to take a decision of which strategy to use based on the input parameters. An important feature of strategy pattern is that client is aware of all the available strategies and which strategy to adopt. It helps is design a system that is elegant, extensible, and powerful. The following diagram illustrates the strategy pattern: &amp;lt;br/&amp;gt;&lt;br /&gt;
[[File:Strategy.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The following example is in Java.[http://en.wikipedia.org/wiki/Strategy_pattern]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// The classes that implement a concrete strategy should implement this.&lt;br /&gt;
// The Context class uses this to call the concrete strategy.&lt;br /&gt;
interface IStrategy {&lt;br /&gt;
    int execute(int a, int b); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Implements the algorithm using the strategy interface&lt;br /&gt;
class ConcreteStrategyAdd implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategyAdd's execute()&amp;quot;);&lt;br /&gt;
        return a + b;  // Do an addition with a and b&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ConcreteStrategySubtract implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategySubtract's execute()&amp;quot;);&lt;br /&gt;
        return a - b;  // Do a subtraction with a and b&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ConcreteStrategyMultiply implements IStrategy {&lt;br /&gt;
&lt;br /&gt;
    public int execute(int a, int b) {&lt;br /&gt;
        System.out.println(&amp;quot;Called ConcreteStrategyMultiply's execute()&amp;quot;);&lt;br /&gt;
        return a * b;   // Do a multiplication with a and b&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object&lt;br /&gt;
class Context {&lt;br /&gt;
&lt;br /&gt;
    private IStrategy strategy;&lt;br /&gt;
&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Context(IStrategy strategy) {&lt;br /&gt;
        this.strategy = strategy;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int executeStrategy(int a, int b) {&lt;br /&gt;
        return strategy.execute(a, b);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Test application&lt;br /&gt;
class StrategyExample {&lt;br /&gt;
&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
&lt;br /&gt;
        Context context;&lt;br /&gt;
&lt;br /&gt;
        // Three contexts following different strategies&lt;br /&gt;
        context = new Context(new ConcreteStrategyAdd());&lt;br /&gt;
        int resultA = context.executeStrategy(3,4);&lt;br /&gt;
&lt;br /&gt;
        context = new Context(new ConcreteStrategySubtract());&lt;br /&gt;
        int resultB = context.executeStrategy(3,4);&lt;br /&gt;
&lt;br /&gt;
        context = new Context(new ConcreteStrategyMultiply());&lt;br /&gt;
        int resultC = context.executeStrategy(3,4);&lt;br /&gt;
     &lt;br /&gt;
        System.out.println(&amp;quot;Result A : &amp;quot; + resultA );&lt;br /&gt;
        System.out.println(&amp;quot;Result B : &amp;quot; + resultB );&lt;br /&gt;
        System.out.println(&amp;quot;Result C : &amp;quot; + resultC );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
The difference between Strategy Pattern and Command pattern is in the purpose itself:&lt;br /&gt;
#Command encapsulates a single action. It therefore tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later - or it is used to provide undo functionality as explained in the Command pattern section.Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded later.&lt;br /&gt;
#Strategies encapsulate algorithms. Commands separate the sender from the receiver of a request, they turn a request into an object. If it's an algorithm, how something will be done, use a Strategy. If you need to separate the call of a method from its execution use a Command. &lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The applications where strategy pattern is useful are as follows:&lt;br /&gt;
#Strategy pattern is used in an system where we try to book the most inexpensive ticket from source to destination. Depending on the users preference of seats, time of transit and other preferences the system chooses the algorithm that will cater to all the user needs at an optimum fare.&lt;br /&gt;
#Strategy pattern can be used when we have to sort numbers. Depending on the type of input, we can make use of the best algorithm. For example, if we know that the numbers are almost sorted then we make use of insertion sort. If the numbers to be sorted are within a range, then we make use of counting sort. If the input sequence is random then our best bet is to use merge sort.&lt;br /&gt;
# A strategy pattern can be used when we want to read a file that has been transferred over a network. If the file is an XML File then use an XML parsing algorithm. If it is JSON file then use a JSON parser and so on.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
Thus, we see that Command Pattern is quite useful. The Chain of Responsibility, Memento ad Strategy pattern adopt the concept of Command patterns and have their own variations which make them useful in different scenarios as mentioned in each of the sections. The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that knows how to perform it. The major disadvantage of the pattern is that it results in many Command classes that can clutter up a design. If the classes are not designed properly it may lead to bloating of the design and will increase the cost of maintaining such a design.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=70133</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=70133"/>
		<updated>2012-11-18T08:57:49Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this [http://en.wikipedia.org/wiki/Command_pattern].&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern].&lt;br /&gt;
== Example of Chain of Responsibility ==&lt;br /&gt;
[[File:COR.jpg]]&lt;br /&gt;
&lt;br /&gt;
The above figure helps to understand the workings of Chain of Responsibility pattern.The example given in [http://javapapers.com/design-patterns/chain-of-responsibility-design-pattern/] has been described below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Interface&lt;br /&gt;
public interface Chain {&lt;br /&gt;
 &lt;br /&gt;
  public abstract void setNext(Chain nextInChain);&lt;br /&gt;
  public abstract void process(Number request);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Number {&lt;br /&gt;
  private int number;&lt;br /&gt;
 &lt;br /&gt;
  public Number(int number) {&lt;br /&gt;
    this.number = number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public int getNumber() {&lt;br /&gt;
    return number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler1&lt;br /&gt;
public class NegativeProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;lt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;NegativeProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler2&lt;br /&gt;
public class ZeroProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() == 0) {&lt;br /&gt;
      System.out.println(&amp;quot;ZeroProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Handler3&lt;br /&gt;
public class PositiveProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;gt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;PositiveProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class TestChain {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    //configure Chain of Responsibility&lt;br /&gt;
    Chain c1 = new NegativeProcessor();&lt;br /&gt;
    Chain c2 = new ZeroProcessor();&lt;br /&gt;
    Chain c3 = new PositiveProcessor();&lt;br /&gt;
    c1.setNext(c2);&lt;br /&gt;
    c2.setNext(c3);&lt;br /&gt;
 &lt;br /&gt;
    //calling chain of responsibility&lt;br /&gt;
    c1.process(new Number(99));&lt;br /&gt;
    c1.process(new Number(-30));&lt;br /&gt;
    c1.process(new Number(0));&lt;br /&gt;
    c1.process(new Number(100));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example the Chain interface is implemented by three handlers which handles 3 different types of numbers-positive numbers,negative numbers and the number zero. The first in the chain is the negative number handler which sets the next handler as the zero handler which in turn sets the positive handler as the last component in the chain.The Number class acts as the receiver which has been explained in the Command pattern.The TestChain is the client.&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
# Both in Command and Chain of Responsibility  pattern commands or actions are stored so that it can be later used.&lt;br /&gt;
# The Chain of Responsibility forwards requests along a chain of classes, but the Command pattern forwards a request only to a specific object. &lt;br /&gt;
# The main intent of both the patterns is to decouple senders and receivers.In case of Command pattern that is only one.&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
Chain of Responsibility is used in many applications:&lt;br /&gt;
# It is used often to handle exceptions inside kernel.There will be a chain of interrupt handlers and the request is passed on until somebody handles it.&lt;br /&gt;
# The pattern is used in windows systems to handle events generated from the keyboard or mouse.&lt;br /&gt;
# Single sign on security solutions for web applications.  You might have a handler to check if the user is already authenticated, another handler to check for windows authentication, and a last handler to transfer the request to a logon page[http://codebetter.com/jeremymiller/2005/11/07/using-the-chain-of-responsibility-pattern/].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=70129</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=70129"/>
		<updated>2012-11-18T08:48:31Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this [http://en.wikipedia.org/wiki/Command_pattern].&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern].&lt;br /&gt;
== Example of Chain of Responsibility ==&lt;br /&gt;
[[File:COR.jpg]]&lt;br /&gt;
The above figure helps to understand the workings of Chain of Responsibility pattern.The example given in [http://javapapers.com/design-patterns/chain-of-responsibility-design-pattern/] has been described below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Chain {&lt;br /&gt;
 &lt;br /&gt;
  public abstract void setNext(Chain nextInChain);&lt;br /&gt;
  public abstract void process(Number request);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Number {&lt;br /&gt;
  private int number;&lt;br /&gt;
 &lt;br /&gt;
  public Number(int number) {&lt;br /&gt;
    this.number = number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public int getNumber() {&lt;br /&gt;
    return number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class NegativeProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;lt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;NegativeProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class ZeroProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() == 0) {&lt;br /&gt;
      System.out.println(&amp;quot;ZeroProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class PositiveProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;gt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;PositiveProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class TestChain {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    //configure Chain of Responsibility&lt;br /&gt;
    Chain c1 = new NegativeProcessor();&lt;br /&gt;
    Chain c2 = new ZeroProcessor();&lt;br /&gt;
    Chain c3 = new PositiveProcessor();&lt;br /&gt;
    c1.setNext(c2);&lt;br /&gt;
    c2.setNext(c3);&lt;br /&gt;
 &lt;br /&gt;
    //calling chain of responsibility&lt;br /&gt;
    c1.process(new Number(99));&lt;br /&gt;
    c1.process(new Number(-30));&lt;br /&gt;
    c1.process(new Number(0));&lt;br /&gt;
    c1.process(new Number(100));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
# Both in Command and Chain of Responsibility  pattern commands or actions are stored so that it can be later used.&lt;br /&gt;
# The Chain of Responsibility forwards requests along a chain of classes, but the Command pattern forwards a request only to a specific object. &lt;br /&gt;
# The main intent of both the patterns is to decouple senders and receivers.In case of Command pattern that is only one.&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
Chain of Responsibility is used in many applications:&lt;br /&gt;
# It is used often to handle exceptions inside kernel.There will be a chain of interrupt handlers and the request is passed on until somebody handles it.&lt;br /&gt;
# The pattern is used in windows systems to handle events generated from the keyboard or mouse.&lt;br /&gt;
# Single sign on security solutions for web applications.  You might have a handler to check if the user is already authenticated, another handler to check for windows authentication, and a last handler to transfer the request to a logon page[http://codebetter.com/jeremymiller/2005/11/07/using-the-chain-of-responsibility-pattern/].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:COR.jpg&amp;diff=70128</id>
		<title>File:COR.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:COR.jpg&amp;diff=70128"/>
		<updated>2012-11-18T08:47:59Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=70122</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=70122"/>
		<updated>2012-11-18T08:31:18Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this [http://en.wikipedia.org/wiki/Command_pattern].&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern].&lt;br /&gt;
== Example of Chain of Responsibility ==&lt;br /&gt;
The example given in [http://javapapers.com/design-patterns/chain-of-responsibility-design-pattern/] has been described below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Chain {&lt;br /&gt;
 &lt;br /&gt;
  public abstract void setNext(Chain nextInChain);&lt;br /&gt;
  public abstract void process(Number request);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Number {&lt;br /&gt;
  private int number;&lt;br /&gt;
 &lt;br /&gt;
  public Number(int number) {&lt;br /&gt;
    this.number = number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public int getNumber() {&lt;br /&gt;
    return number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class NegativeProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;lt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;NegativeProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class ZeroProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() == 0) {&lt;br /&gt;
      System.out.println(&amp;quot;ZeroProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class PositiveProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;gt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;PositiveProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class TestChain {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    //configure Chain of Responsibility&lt;br /&gt;
    Chain c1 = new NegativeProcessor();&lt;br /&gt;
    Chain c2 = new ZeroProcessor();&lt;br /&gt;
    Chain c3 = new PositiveProcessor();&lt;br /&gt;
    c1.setNext(c2);&lt;br /&gt;
    c2.setNext(c3);&lt;br /&gt;
 &lt;br /&gt;
    //calling chain of responsibility&lt;br /&gt;
    c1.process(new Number(99));&lt;br /&gt;
    c1.process(new Number(-30));&lt;br /&gt;
    c1.process(new Number(0));&lt;br /&gt;
    c1.process(new Number(100));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
# Both in Command and Chain of Responsibility  pattern commands or actions are stored so that it can be later used.&lt;br /&gt;
# The Chain of Responsibility forwards requests along a chain of classes, but the Command pattern forwards a request only to a specific object. &lt;br /&gt;
# The main intent of both the patterns is to decouple senders and receivers.In case of Command pattern that is only one.&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
Chain of Responsibility is used in many applications:&lt;br /&gt;
# It is used often to handle exceptions inside kernel.There will be a chain of interrupt handlers and the request is passed on until somebody handles it.&lt;br /&gt;
# The pattern is used in windows systems to handle events generated from the keyboard or mouse.&lt;br /&gt;
# Single sign on security solutions for web applications.  You might have a handler to check if the user is already authenticated, another handler to check for windows authentication, and a last handler to transfer the request to a logon page[http://codebetter.com/jeremymiller/2005/11/07/using-the-chain-of-responsibility-pattern/].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=70121</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=70121"/>
		<updated>2012-11-18T08:30:19Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this [http://en.wikipedia.org/wiki/Command_pattern].&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern].&lt;br /&gt;
== Example of Chain of Responsibility ==&lt;br /&gt;
The example given in [http://javapapers.com/design-patterns/chain-of-responsibility-design-pattern/] has been described below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Chain {&lt;br /&gt;
 &lt;br /&gt;
  public abstract void setNext(Chain nextInChain);&lt;br /&gt;
  public abstract void process(Number request);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Number {&lt;br /&gt;
  private int number;&lt;br /&gt;
 &lt;br /&gt;
  public Number(int number) {&lt;br /&gt;
    this.number = number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public int getNumber() {&lt;br /&gt;
    return number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class NegativeProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;lt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;NegativeProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class ZeroProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() == 0) {&lt;br /&gt;
      System.out.println(&amp;quot;ZeroProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class PositiveProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;gt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;PositiveProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class TestChain {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    //configure Chain of Responsibility&lt;br /&gt;
    Chain c1 = new NegativeProcessor();&lt;br /&gt;
    Chain c2 = new ZeroProcessor();&lt;br /&gt;
    Chain c3 = new PositiveProcessor();&lt;br /&gt;
    c1.setNext(c2);&lt;br /&gt;
    c2.setNext(c3);&lt;br /&gt;
 &lt;br /&gt;
    //calling chain of responsibility&lt;br /&gt;
    c1.process(new Number(99));&lt;br /&gt;
    c1.process(new Number(-30));&lt;br /&gt;
    c1.process(new Number(0));&lt;br /&gt;
    c1.process(new Number(100));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
# Both in Command and Chain of Responsibility  pattern commands or actions are stored so that it can be later used.&lt;br /&gt;
# The Chain of Responsibility forwards requests along a chain of classes, but the Command pattern forwards a request only to a specific object. &lt;br /&gt;
# The main intent of both the patterns is to decouple senders and receivers.In case of Command pattern that is only one.&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
Chain of Responsibility is used in many applications:&lt;br /&gt;
# It is used often to handle exceptions inside kernel.There will be a chain of interrupt handlers and the request is passed on until somebody handles it.&lt;br /&gt;
# The pattern is used in windows systems to handle events generated from the keyboard or mouse.&lt;br /&gt;
# Single sign on security solutions for web applications.  You might have a handler to check if the user is already authenticated, another handler to check for windows authentication, and a last handler to transfer the request to a logon page[http://codebetter.com/jeremymiller/2005/11/07/using-the-chain-of-responsibility-pattern/].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69856</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69856"/>
		<updated>2012-11-17T09:14:58Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this [http://en.wikipedia.org/wiki/Command_pattern].&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern].&lt;br /&gt;
== Example of Chain of Responsibility ==&lt;br /&gt;
The example given in [http://javapapers.com/design-patterns/chain-of-responsibility-design-pattern/] has been described below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Chain {&lt;br /&gt;
 &lt;br /&gt;
  public abstract void setNext(Chain nextInChain);&lt;br /&gt;
  public abstract void process(Number request);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Number {&lt;br /&gt;
  private int number;&lt;br /&gt;
 &lt;br /&gt;
  public Number(int number) {&lt;br /&gt;
    this.number = number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public int getNumber() {&lt;br /&gt;
    return number;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class NegativeProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;lt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;NegativeProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class ZeroProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() == 0) {&lt;br /&gt;
      System.out.println(&amp;quot;ZeroProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class PositiveProcessor implements Chain {&lt;br /&gt;
 &lt;br /&gt;
  private Chain nextInChain;&lt;br /&gt;
 &lt;br /&gt;
  public void setNext(Chain c) {&lt;br /&gt;
    nextInChain = c;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public void process(Number request) {&lt;br /&gt;
    if (request.getNumber() &amp;gt; 0) {&lt;br /&gt;
      System.out.println(&amp;quot;PositiveProcessor : &amp;quot; + request.getNumber());&lt;br /&gt;
    } else {&lt;br /&gt;
      nextInChain.process(request);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class TestChain {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    //configure Chain of Responsibility&lt;br /&gt;
    Chain c1 = new NegativeProcessor();&lt;br /&gt;
    Chain c2 = new ZeroProcessor();&lt;br /&gt;
    Chain c3 = new PositiveProcessor();&lt;br /&gt;
    c1.setNext(c2);&lt;br /&gt;
    c2.setNext(c3);&lt;br /&gt;
 &lt;br /&gt;
    //calling chain of responsibility&lt;br /&gt;
    c1.process(new Number(99));&lt;br /&gt;
    c1.process(new Number(-30));&lt;br /&gt;
    c1.process(new Number(0));&lt;br /&gt;
    c1.process(new Number(100));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69850</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69850"/>
		<updated>2012-11-17T08:55:47Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this [http://en.wikipedia.org/wiki/Command_pattern].&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.&lt;br /&gt;
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern].&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69843</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69843"/>
		<updated>2012-11-17T07:41:22Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
The command pattern can be used when the following things are desired:&lt;br /&gt;
# Specify,queue and execute requests at different times. &lt;br /&gt;
# To support operations like '''Undo''','''Redo'''.&lt;br /&gt;
# Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.&lt;br /&gt;
# These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.&lt;br /&gt;
# It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.&lt;br /&gt;
For an additional list of applications users can read this [http://en.wikipedia.org/wiki/Command_pattern].&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69755</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69755"/>
		<updated>2012-11-17T03:28:17Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69754</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69754"/>
		<updated>2012-11-17T03:27:47Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example.&lt;br /&gt;
This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implements Command&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.&lt;br /&gt;
&lt;br /&gt;
Light is the receiver class which contains the commands to be executed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And finally there is the Client class which will use the commands to switch the lights on and off.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69748</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69748"/>
		<updated>2012-11-17T02:58:31Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
[[File:Command.jpg]]&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Command.jpg&amp;diff=69747</id>
		<title>File:Command.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Command.jpg&amp;diff=69747"/>
		<updated>2012-11-17T02:57:22Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69744</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69744"/>
		<updated>2012-11-17T02:19:23Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Concrete Command&lt;br /&gt;
public class LightOnCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOnCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOn();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 //Concrete Command&lt;br /&gt;
public class LightOffCommand implementsCommand&lt;br /&gt;
{&lt;br /&gt;
    //reference to the light&lt;br /&gt;
    Light light;&lt;br /&gt;
    &lt;br /&gt;
    public LightOffCommand(Light light)&lt;br /&gt;
    {&lt;br /&gt;
        this.light = light;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void execute()&lt;br /&gt;
    {&lt;br /&gt;
        light.switchOff();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Receiver&lt;br /&gt;
public class Light&lt;br /&gt;
{&lt;br /&gt;
   private boolean on;&lt;br /&gt;
  &lt;br /&gt;
   public void switchOn()&lt;br /&gt;
   {&lt;br /&gt;
      on = true;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
   public void switchOff()&lt;br /&gt;
   {&lt;br /&gt;
      on = false;&lt;br /&gt;
   }&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Invoker&lt;br /&gt;
public class RemoteControl&lt;br /&gt;
{&lt;br /&gt;
    private Command command;&lt;br /&gt;
&lt;br /&gt;
    public void setCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
        this.command = command;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
    public void pressButton()&lt;br /&gt;
    {&lt;br /&gt;
        command.execute();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Client&lt;br /&gt;
public class Client&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String[] args)&lt;br /&gt;
    {&lt;br /&gt;
        RemoteControl control = new RemoteControl();&lt;br /&gt;
        &lt;br /&gt;
        Light light = new Light();&lt;br /&gt;
        &lt;br /&gt;
        Command lightsOn = new LightsOnCommand(light);&lt;br /&gt;
        Command lightsOff = new LightsOffCommand(light);&lt;br /&gt;
        &lt;br /&gt;
        //switch on&lt;br /&gt;
        control.setCommand(lightsOn);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
        &lt;br /&gt;
        //switch off&lt;br /&gt;
        control.setCommand(lightsOff);&lt;br /&gt;
        control.pressButton();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69743</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69743"/>
		<updated>2012-11-17T02:16:19Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//Command&lt;br /&gt;
public interface Command&lt;br /&gt;
{&lt;br /&gt;
    public void execute();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69742</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69742"/>
		<updated>2012-11-17T02:04:44Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction to Command Pattern =&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
== A Java Example ==&lt;br /&gt;
&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Chain of Responsibility =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Memento =&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Strategy =&lt;br /&gt;
&lt;br /&gt;
== Comparison with Command Pattern ==&lt;br /&gt;
== Real life applications ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
== Advantages and Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69741</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69741"/>
		<updated>2012-11-17T02:03:04Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction to Command Pattern ==&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
= A Java Example =&lt;br /&gt;
&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Chain of Responsibility ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Memento ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Strategy ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
=== Advantages and Disadvantages ===&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69736</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69736"/>
		<updated>2012-11-17T01:48:34Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction to Command Pattern ==&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
The intent of the Command pattern can be listed as:&lt;br /&gt;
#encapsulate a request in an object&lt;br /&gt;
# allows the parametrization of clients with different requests&lt;br /&gt;
# allows saving the requests in a queue&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Chain of Responsibility ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Memento ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Strategy ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
=== Advantages and Disadvantages ===&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69548</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69548"/>
		<updated>2012-11-16T09:09:09Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction to Command Pattern ==&lt;br /&gt;
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Chain of Responsibility ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Memento ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Strategy ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
=== Advantages and Disadvantages ===&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69541</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69541"/>
		<updated>2012-11-15T17:30:53Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction to Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Chain of Responsibility ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Memento ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Strategy ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
=== Advantages and Disadvantages ===&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69540</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69540"/>
		<updated>2012-11-15T17:27:58Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction to Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
== Chain of Responsibility ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Memento ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;br /&gt;
&lt;br /&gt;
== Strategy ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69539</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69539"/>
		<updated>2012-11-15T17:27:14Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction to Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
== Chain of Responsibility ==&lt;br /&gt;
&lt;br /&gt;
=== Comparison with Command Pattern ===&lt;br /&gt;
=== Real life applications ===&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69538</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69538"/>
		<updated>2012-11-15T17:24:39Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction to Command Pattern ==&lt;br /&gt;
&lt;br /&gt;
===  Regular Expressions ===&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69537</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69537"/>
		<updated>2012-11-15T17:24:05Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=== Introduction to Command Pattern ===&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69536</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w40 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w40_sn&amp;diff=69536"/>
		<updated>2012-11-15T17:23:19Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: Created page with &amp;quot;=== Introduction to Command Pattern===  ==  Regular Expressions ==&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=== Introduction to Command Pattern===&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=69535</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=69535"/>
		<updated>2012-11-15T17:19:52Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/Table_Of_Contents]]&lt;br /&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/ch2a 2w5 dp]]&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/ch2a_2w3_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 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;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w26 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w16 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w8 vp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w3 jm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w23 sr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w11_aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w15 rr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w33 pv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w20_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w14_bb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w21_ap]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w13_sm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w4_sa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w25_nr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w12_sv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w7_ma]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w6_ar]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w32_mk]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w10_rc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w70_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w67_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w40_sn]]&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66845</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66845"/>
		<updated>2012-10-04T00:25:22Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
==Software Testing==&lt;br /&gt;
In the simplest terms, software testing can be summarized as follows. We provide some test inputs to the software and we get some test outputs from the software. Then we check if the output is acceptable or not. If the output is acceptable then the test case has passed, otherwise it has failed and we have to debug it. The hard part of doing software testing is selecting a good set of test inputs and designing good acceptability tests.&lt;br /&gt;
&lt;br /&gt;
But while testing the software, we have to keep the following things in mind. We have to find bugs as early as possible. The earlier we find the bug, the cheaper it is to fix it.&lt;br /&gt;
Also, more testing is not always better. We may write a lot of test cases but they still may not cover every functionality of our software. &lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically ([http://en.wikipedia.org/wiki/Monkey_patch monkey]) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk). It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Database Setup&amp;lt;/h4&amp;gt;&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;.When we create the rails scaffold for a particular model then it creates the directories unit,functional,integration which contains the different tests for the respective models.After the test cases have been written we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails [http://guides.rubyonrails.org/getting_started.html#generating-a-model generate model] is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Unit Testing==&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object.&lt;br /&gt;
&lt;br /&gt;
Here is another example where we try to test the functionality where a user tries to register with an already existing username&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;username exists&amp;quot; do&lt;br /&gt;
    user = User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    user.save&lt;br /&gt;
    user1 =User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    assert_false user1.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here, a user tries to add a post with a valid title but he leaves the content field blank.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;empty content test&amp;quot; do&lt;br /&gt;
    post = Post.new( :title =&amp;gt; &amp;quot;No content for this post&amp;quot;,:content =&amp;gt; nil  )&lt;br /&gt;
    post.User_id=1;&lt;br /&gt;
    assert_false post.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
==Functional Testing==&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @comment_new = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @post = Post.find(@comment_new.post_id)&lt;br /&gt;
    @comment = comments(:one)#The fixtures contain a row named one&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to post_path(assigns(:post))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.&lt;br /&gt;
&lt;br /&gt;
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.&lt;br /&gt;
&lt;br /&gt;
In the example below, we test the functionality upon deleting a user.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;should destroy user&amp;quot; do&lt;br /&gt;
    assert_difference('User.count', -1) do&lt;br /&gt;
      delete :destroy, id: @user&lt;br /&gt;
    end&lt;br /&gt;
    assert_redirected_to users_path&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavioral-Driven-Development] goes hand in hand with [http://en.wikipedia.org/wiki/Test-driven_development Test-Driven-Development] and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara'''&amp;lt;ref&amp;gt;http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/&amp;lt;/ref&amp;gt; is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2'''&amp;lt;ref&amp;gt;https://github.com/rspec/rspec#readme&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;br /&gt;
 &lt;br /&gt;
The old wiki can be found [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4e_gs here].&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66843</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66843"/>
		<updated>2012-10-04T00:24:24Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
==Software Testing==&lt;br /&gt;
In the simplest terms, software testing can be summarized as follows. We provide some test inputs to the software and we get some test outputs from the software. Then we check if the output is acceptable or not. If the output is acceptable then the test case has passed, otherwise it has failed and we have to debug it. The hard part of doing software testing is selecting a good set of test inputs and designing good acceptability tests.&lt;br /&gt;
&lt;br /&gt;
But while testing the software, we have to keep the following things in mind. We have to find bugs as early as possible. The earlier we find the bug, the cheaper it is to fix it.&lt;br /&gt;
Also, more testing is not always better. We may write a lot of test cases but they still may not cover every functionality of our software. &lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically ([http://en.wikipedia.org/wiki/Monkey_patch monkey]) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk). It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Database Setup&amp;lt;/h4&amp;gt;&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;.When we create the rails scaffold for a particular model then it creates the directories unit,functional,integration which contains the different tests for the respective models.After the test cases have been written we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails [http://guides.rubyonrails.org/getting_started.html#generating-a-model generate model] is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Unit Testing==&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object.&lt;br /&gt;
&lt;br /&gt;
Here is another example where we try to test the functionality where a user tries to register with an already existing username&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;username exists&amp;quot; do&lt;br /&gt;
    user = User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    user.save&lt;br /&gt;
    user1 =User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    assert_false user1.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here, a user tries to add a post with a valid title but he leaves the content field blank.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;empty content test&amp;quot; do&lt;br /&gt;
    post = Post.new( :title =&amp;gt; &amp;quot;No content for this post&amp;quot;,:content =&amp;gt; nil  )&lt;br /&gt;
    post.User_id=1;&lt;br /&gt;
    assert_false post.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
==Functional Testing==&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @comment_new = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @post = Post.find(@comment_new.post_id)&lt;br /&gt;
    @comment = comments(:one)#The fixtures contain a row named one&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to post_path(assigns(:post))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.&lt;br /&gt;
&lt;br /&gt;
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.&lt;br /&gt;
&lt;br /&gt;
In the example below, we test the functionality upon deleting a user.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;should destroy user&amp;quot; do&lt;br /&gt;
    assert_difference('User.count', -1) do&lt;br /&gt;
      delete :destroy, id: @user&lt;br /&gt;
    end&lt;br /&gt;
    assert_redirected_to users_path&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavioral-Driven-Development] goes hand in hand with [http://en.wikipedia.org/wiki/Test-driven_development Test-Driven-Development] and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara'''&amp;lt;ref&amp;gt;http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/&amp;lt;/ref&amp;gt; is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2'''&amp;lt;ref&amp;gt;https://github.com/rspec/rspec#readme&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here]. The old wiki can be found [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4e_gs here].&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66821</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66821"/>
		<updated>2012-10-04T00:13:24Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
==Software Testing==&lt;br /&gt;
In the simplest terms, software testing can be summarized as follows. We provide some test inputs to the software and we get some test outputs from the software. Then we check if the output is acceptable or not. If the output is acceptable then the test case has passed, otherwise it has failed and we have to debug it. The hard part of doing software testing is selecting a good set of test inputs and designing good acceptability tests.&lt;br /&gt;
&lt;br /&gt;
But while testing the software, we have to keep the following things in mind. We have to find bugs as early as possible. The earlier we find the bug, the cheaper it is to fix it.&lt;br /&gt;
Also, more testing is not always better. We may write a lot of test cases but they still may not cover every functionality of our software. &lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically ([http://en.wikipedia.org/wiki/Monkey_patch monkey]) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk). It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Database Setup&amp;lt;/h4&amp;gt;&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;.When we create the rails scaffold for a particular model then it creates the directories unit,functional,integration which contains the different tests for the respective models.After the test cases have been written we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails [http://guides.rubyonrails.org/getting_started.html#generating-a-model generate model] is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Unit Testing==&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object.&lt;br /&gt;
&lt;br /&gt;
Here is another example where we try to test the functionality where a user tries to register with an already existing username&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;username exists&amp;quot; do&lt;br /&gt;
    user = User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    user.save&lt;br /&gt;
    user1 =User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    assert_false user1.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here, a user tries to add a post with a valid title but he leaves the content field blank.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;empty content test&amp;quot; do&lt;br /&gt;
    post = Post.new( :title =&amp;gt; &amp;quot;No content for this post&amp;quot;,:content =&amp;gt; nil  )&lt;br /&gt;
    post.User_id=1;&lt;br /&gt;
    assert_false post.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
==Functional Testing==&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @comment_new = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @post = Post.find(@comment_new.post_id)&lt;br /&gt;
    @comment = comments(:one)#The fixtures contain a row named one&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to post_path(assigns(:post))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.&lt;br /&gt;
&lt;br /&gt;
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.&lt;br /&gt;
&lt;br /&gt;
In the example below, we test the functionality upon deleting a user.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;should destroy user&amp;quot; do&lt;br /&gt;
    assert_difference('User.count', -1) do&lt;br /&gt;
      delete :destroy, id: @user&lt;br /&gt;
    end&lt;br /&gt;
    assert_redirected_to users_path&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavioral-Driven-Development] goes hand in hand with [http://en.wikipedia.org/wiki/Test-driven_development Test-Driven-Development] and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara'''&amp;lt;ref&amp;gt;http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/&amp;lt;/ref&amp;gt; is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2'''&amp;lt;ref&amp;gt;https://github.com/rspec/rspec#readme&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66805</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66805"/>
		<updated>2012-10-04T00:06:31Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
==Software Testing==&lt;br /&gt;
In the simplest terms, software testing can be summarized as follows. We provide some test inputs to the software and we get some test outputs from the software. Then we check if the output is acceptable or not. If the output is acceptable then the test case has passed, otherwise it has failed and we have to debug it. The hard part of doing software testing is selecting a good set of test inputs and designing good acceptability tests.&lt;br /&gt;
&lt;br /&gt;
But while testing the software, we have to keep the following things in mind. We have to find bugs as early as possible. The earlier we find the bug, the cheaper it is to fix it.&lt;br /&gt;
Also, more testing is not always better. We may write a lot of test cases but they still may not cover every functionality of our software. &lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically ([http://en.wikipedia.org/wiki/Monkey_patch monkey]) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk). It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Database Setup&amp;lt;/h4&amp;gt;&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;.When we create the rails scaffold for a particular model then it creates the directories unit,functional,integration which contains the different tests for the respective models.After the test cases have been written we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails [http://guides.rubyonrails.org/getting_started.html#generating-a-model generate model] is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Unit Testing==&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object.&lt;br /&gt;
&lt;br /&gt;
Here is another example where we try to test the functionality where a user tries to register with an already existing username&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;username exists&amp;quot; do&lt;br /&gt;
    user = User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    user.save&lt;br /&gt;
    user1 =User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    assert_false user1.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here, a user tries to add a post with a valid title but he leaves the content field blank.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;empty content test&amp;quot; do&lt;br /&gt;
    post = Post.new( :title =&amp;gt; &amp;quot;No content for this post&amp;quot;,:content =&amp;gt; nil  )&lt;br /&gt;
    post.User_id=1;&lt;br /&gt;
    assert_false post.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
==Functional Testing==&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @comment_new = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @post = Post.find(@comment_new.post_id)&lt;br /&gt;
    @comment = comments(:one)#The fixtures contain a row named one&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to post_path(assigns(:post))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.&lt;br /&gt;
&lt;br /&gt;
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.&lt;br /&gt;
&lt;br /&gt;
In the example below, we test the functionality upon deleting a user.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;should destroy user&amp;quot; do&lt;br /&gt;
    assert_difference('User.count', -1) do&lt;br /&gt;
      delete :destroy, id: @user&lt;br /&gt;
    end&lt;br /&gt;
    assert_redirected_to users_path&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavioral-Driven-Development] goes hand in hand with [http://en.wikipedia.org/wiki/Test-driven_development Test-Driven-Development] and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara'''&amp;lt;ref&amp;gt;http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/&amp;lt;/ref&amp;gt; is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2'''&amp;lt;ref&amp;gt;http://rspec.info/documentation/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66798</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66798"/>
		<updated>2012-10-04T00:04:41Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
==Software Testing==&lt;br /&gt;
In the simplest terms, software testing can be summarized as follows. We provide some test inputs to the software and we get some test outputs from the software. Then we check if the output is acceptable or not. If the output is acceptable then the test case has passed, otherwise it has failed and we have to debug it. The hard part of doing software testing is selecting a good set of test inputs and designing good acceptability tests.&lt;br /&gt;
&lt;br /&gt;
But while testing the software, we have to keep the following things in mind. We have to find bugs as early as possible. The earlier we find the bug, the cheaper it is to fix it.&lt;br /&gt;
Also, more testing is not always better. We may write a lot of test cases but they still may not cover every functionality of our software. &lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically ([http://en.wikipedia.org/wiki/Monkey_patch monkey]) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk). It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Database Setup:&amp;lt;/h4&amp;gt;&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;.When we create the rails scaffold for a particular model then it creates the directories unit,functional,integration which contains the different tests for the respective models.After the test cases have been written we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails [http://guides.rubyonrails.org/getting_started.html#generating-a-model generate model] is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Unit Testing==&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object.&lt;br /&gt;
&lt;br /&gt;
Here is another example where we try to test the functionality where a user tries to register with an already existing username&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;username exists&amp;quot; do&lt;br /&gt;
    user = User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    user.save&lt;br /&gt;
    user1 =User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    assert_false user1.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here, a user tries to add a post with a valid title but he leaves the content field blank.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;empty content test&amp;quot; do&lt;br /&gt;
    post = Post.new( :title =&amp;gt; &amp;quot;No content for this post&amp;quot;,:content =&amp;gt; nil  )&lt;br /&gt;
    post.User_id=1;&lt;br /&gt;
    assert_false post.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
==Functional Testing==&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @comment_new = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @post = Post.find(@comment_new.post_id)&lt;br /&gt;
    @comment = comments(:one)#The fixtures contain a row named one&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to post_path(assigns(:post))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.&lt;br /&gt;
&lt;br /&gt;
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.&lt;br /&gt;
&lt;br /&gt;
In the example below, we test the functionality upon deleting a user.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;should destroy user&amp;quot; do&lt;br /&gt;
    assert_difference('User.count', -1) do&lt;br /&gt;
      delete :destroy, id: @user&lt;br /&gt;
    end&lt;br /&gt;
    assert_redirected_to users_path&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavioral-Driven-Development] goes hand in hand with [http://en.wikipedia.org/wiki/Test-driven_development Test-Driven-Development] and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara'''&amp;lt;ref&amp;gt;http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/&amp;lt;/ref&amp;gt; is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2'''&amp;lt;ref&amp;gt;http://rspec.info/documentation/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66795</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66795"/>
		<updated>2012-10-04T00:03:26Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
==Software Testing==&lt;br /&gt;
In the simplest terms, software testing can be summarized as follows. We provide some test inputs to the software and we get some test outputs from the software. Then we check if the output is acceptable or not. If the output is acceptable then the test case has passed, otherwise it has failed and we have to debug it. The hard part of doing software testing is selecting a good set of test inputs and designing good acceptability tests.&lt;br /&gt;
&lt;br /&gt;
But while testing the software, we have to keep the following things in mind. We have to find bugs as early as possible. The earlier we find the bug, the cheaper it is to fix it.&lt;br /&gt;
Also, more testing is not always better. We may write a lot of test cases but they still may not cover every functionality of our software. &lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically ([http://en.wikipedia.org/wiki/Monkey_patch monkey]) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk). It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Database Setup:&amp;lt;/h4&amp;gt;&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;.When we create the rails scaffold for a particular model then it creates the directories unit,functional,integration which contains the different tests for the respective models.After the test cases have been written we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails [http://guides.rubyonrails.org/getting_started.html#generating-a-model generate model] is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Unit Testing==&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object.&lt;br /&gt;
&lt;br /&gt;
Here is another example where we try to test the functionality where a user tries to register with an already existing username&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;username exists&amp;quot; do&lt;br /&gt;
    user = User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    user.save&lt;br /&gt;
    user1 =User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    assert_false user1.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here, a user tries to add a post with a valid title but he leaves the content field blank.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;empty content test&amp;quot; do&lt;br /&gt;
    post = Post.new( :title =&amp;gt; &amp;quot;No content for this post&amp;quot;,:content =&amp;gt; nil  )&lt;br /&gt;
    post.User_id=1;&lt;br /&gt;
    assert_false post.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
==Functional Testing==&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @comment_new = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @post = Post.find(@comment_new.post_id)&lt;br /&gt;
    @comment = comments(:one)#The fixtures contain a row named one&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to post_path(assigns(:post))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.&lt;br /&gt;
&lt;br /&gt;
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.&lt;br /&gt;
&lt;br /&gt;
In the example below, we test the functionality upon deleting a user.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;should destroy user&amp;quot; do&lt;br /&gt;
    assert_difference('User.count', -1) do&lt;br /&gt;
      delete :destroy, id: @user&lt;br /&gt;
    end&lt;br /&gt;
    assert_redirected_to users_path&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66786</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66786"/>
		<updated>2012-10-04T00:01:55Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
==Software Testing==&lt;br /&gt;
In the simplest terms, software testing can be summarized as follows. We provide some test inputs to the software and we get some test outputs from the software. Then we check if the output is acceptable or not. If the output is acceptable then the test case has passed, otherwise it has failed and we have to debug it. The hard part of doing software testing is selecting a good set of test inputs and designing good acceptability tests.&lt;br /&gt;
&lt;br /&gt;
But while testing the software, we have to keep the following things in mind. We have to find bugs as early as possible. The earlier we find the bug, the cheaper it is to fix it.&lt;br /&gt;
Also, more testing is not always better. We may write a lot of test cases but they still may not cover every functionality of our software. &lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically ([http://en.wikipedia.org/wiki/Monkey_patch monkey]) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk). It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Database Setup:&amp;lt;/h4&amp;gt;&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;.When we create the rails scaffold for a particular model then it creates the directories unit,functional,integration which contains the different tests for the respective models.After the test cases have been written we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Unit Testing:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object.&lt;br /&gt;
&lt;br /&gt;
Here is another example where we try to test the functionality where a user tries to register with an already existing username&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;username exists&amp;quot; do&lt;br /&gt;
    user = User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    user.save&lt;br /&gt;
    user1 =User.new(:username =&amp;gt; &amp;quot;abcdef&amp;quot;, :password =&amp;gt; &amp;quot;abcdef&amp;quot;, :password_confirmation =&amp;gt; &amp;quot;abcdef&amp;quot;)&lt;br /&gt;
    assert_false user1.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here, a user tries to add a post with a valid title but he leaves the content field blank.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;empty content test&amp;quot; do&lt;br /&gt;
    post = Post.new( :title =&amp;gt; &amp;quot;No content for this post&amp;quot;,:content =&amp;gt; nil  )&lt;br /&gt;
    post.User_id=1;&lt;br /&gt;
    assert_false post.save&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;h4&amp;gt;Functional Testing&amp;lt;/h4&amp;gt;&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @comment_new = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @post = Post.find(@comment_new.post_id)&lt;br /&gt;
    @comment = comments(:one)#The fixtures contain a row named one&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to post_path(assigns(:post))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.&lt;br /&gt;
&lt;br /&gt;
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.&lt;br /&gt;
&lt;br /&gt;
In the example below, we test the functionality upon deleting a user.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test &amp;quot;should destroy user&amp;quot; do&lt;br /&gt;
    assert_difference('User.count', -1) do&lt;br /&gt;
      delete :destroy, id: @user&lt;br /&gt;
    end&lt;br /&gt;
    assert_redirected_to users_path&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66061</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66061"/>
		<updated>2012-10-01T06:42:06Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;.When we create the rails scaffold for a particular model then it creates the directories unit,functional,integration which contains the different tests for the respective models.After the test cases have been written we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Unit Testing:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. &lt;br /&gt;
&amp;lt;h4&amp;gt;Functional Testing&amp;lt;/h4&amp;gt;&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @comment_new = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @post = Post.find(@comment_new.post_id)&lt;br /&gt;
    @comment = comments(:one)#The fixtures contain a row named one&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to post_path(assigns(:post))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.&lt;br /&gt;
&lt;br /&gt;
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66060</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=66060"/>
		<updated>2012-10-01T06:40:22Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;.When we create the rails scaffold for a particular model then it creates the directories unit,functional,integration which contains the different tests for the respective models.After the test cases have been written there are a few things we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Unit Testing:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. &lt;br /&gt;
&amp;lt;h4&amp;gt;Functional Testing&amp;lt;/h4&amp;gt;&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @comment_new = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @post = Post.find(@comment_new.post_id)&lt;br /&gt;
    @comment = comments(:one)#The fixtures contain a row named one&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to post_path(assigns(:post))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.&lt;br /&gt;
&lt;br /&gt;
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65957</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65957"/>
		<updated>2012-09-30T07:28:27Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Unit Testing:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. After the test cases have been written there are a few things we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Functional Testing&amp;lt;/h4&amp;gt;&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @comment_new = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @post = Post.find(@comment_new.post_id)&lt;br /&gt;
    @comment = comments(:one)#The fixtures contain a row named one&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create,  { content: @comment_new.content, email: @comment_new.email, post_id: @comment_new.post_id } #parameters that goes with the post request&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to post_path(assigns(:post))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As we see from the code that it is important to set the session variable and also we need to know before hand for which post are we commenting so we set those variables in the setup method itself.Inside the test method we attempt to create a new comment and after that we check in the assert statement whether it has been redirected to the correct path which in this case is the post page for which the comment has been made.&lt;br /&gt;
&lt;br /&gt;
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The assert_select assertion allows you to do this by using a simple yet powerful syntax.&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65955</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65955"/>
		<updated>2012-09-30T06:57:28Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Unit Testing:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. After the test cases have been written there are a few things we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Functional Testing&amp;lt;/h4&amp;gt;&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As it can be seen if no there is no session then no one can comment.If a user is successfully able to comment then he is redirected to the specific post page for which the comment was made.The functional test for this piece of code would look like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CommentsControllerTest &amp;lt; ActionController::TestCase&lt;br /&gt;
  setup do&lt;br /&gt;
    @commentNew = Comment.new(:content =&amp;gt; &amp;quot;Comment to create&amp;quot;, :email =&amp;gt; &amp;quot;test@gm.com&amp;quot;, :post_id =&amp;gt; 1)&lt;br /&gt;
    @comment = comments(:one)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
test &amp;quot;should create comment&amp;quot; do&lt;br /&gt;
    assert_difference('Comment.count') do&lt;br /&gt;
      post :create, comment: { content: @commentNew.content, email: @commentNew.email, post_id: @commentNew.post_id }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    assert_redirected_to comment_path(assigns(:comment))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65952</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65952"/>
		<updated>2012-09-30T06:30:12Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Unit Testing:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. After the test cases have been written there are a few things we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Functional Testing&amp;lt;/h4&amp;gt;&lt;br /&gt;
If unit tests covered models then functional tests took care of the controllers.The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc.Lets say we have a application where users are allowed to post and then comment on those posts.After the user has made a comment then he has to get redirected to that particular post page.Here is how the create method of the comment controller looks like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def create&lt;br /&gt;
    #@comment = Comment.new(params[:comment])&lt;br /&gt;
    if(session[:email] == nil)&lt;br /&gt;
      redirect_to :root&lt;br /&gt;
      return&lt;br /&gt;
    end&lt;br /&gt;
    @comment = Comment.new&lt;br /&gt;
    @comment.post_id = params[:id]&lt;br /&gt;
    @comment.content = params[:content_new]&lt;br /&gt;
    @comment.email = session[:email]&lt;br /&gt;
    @comment.vote_count = 0&lt;br /&gt;
&lt;br /&gt;
    @post = Post.find(@comment.post_id)&lt;br /&gt;
&lt;br /&gt;
    dateTime = Time.new&lt;br /&gt;
    timestamp = dateTime.to_time&lt;br /&gt;
    @post.update_attributes(:updated_at =&amp;gt; timestamp)&lt;br /&gt;
&lt;br /&gt;
    respond_to do |format|&lt;br /&gt;
      if @comment.save&lt;br /&gt;
        format.html { redirect_to :back }&lt;br /&gt;
        format.json { render json: @comment, status: :created, location: @comment }&lt;br /&gt;
      else&lt;br /&gt;
        format.html { render action: &amp;quot;new&amp;quot; }&lt;br /&gt;
        format.json { render json: @comment.errors, status: :unprocessable_entity }&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65945</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65945"/>
		<updated>2012-09-30T05:59:34Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Initial setup:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. After the test cases have been written there are a few things we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].&lt;br /&gt;
&lt;br /&gt;
If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65937</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65937"/>
		<updated>2012-09-30T02:30:17Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Initial setup:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. After the test cases have been written there are a few things we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]].If you are using command line then you can use the following options&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -Itest test/unit/post_test.rb&lt;br /&gt;
Loaded suite unit/post_test&lt;br /&gt;
Started&lt;br /&gt;
.&lt;br /&gt;
Finished in 0.023513 seconds.&lt;br /&gt;
 &lt;br /&gt;
2 tests, 2 assertions, 0 failures, 0 errors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65936</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65936"/>
		<updated>2012-09-30T02:27:53Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Initial setup:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. After the test cases have been written there are a few things we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just do right click on the unit test folder-&amp;gt;Select Run-&amp;gt;All tests in unit.The figure provided below presents a better picture[[File:RunningTest.png]]&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:RunningTest.png&amp;diff=65935</id>
		<title>File:RunningTest.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:RunningTest.png&amp;diff=65935"/>
		<updated>2012-09-30T02:22:59Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: uploaded a new version of &amp;amp;quot;File:RunningTest.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65934</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65934"/>
		<updated>2012-09-30T02:12:43Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Initial setup:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. After the test cases have been written there are a few things we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
After preparing everything we are now ready to run our test.If you are using a Integrated Development Environment(IDE) like RubyMine then you need not worry anything and just press Run-&amp;gt;select your test as shown in the figure[[File:RunningTest.png]]&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:RunningTest.png&amp;diff=65933</id>
		<title>File:RunningTest.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:RunningTest.png&amp;diff=65933"/>
		<updated>2012-09-30T02:12:01Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65884</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65884"/>
		<updated>2012-09-29T08:01:38Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Initial setup:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert .so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object. After the test cases have been written there are a few things we need to prepare the test db.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 rake db:migrate&lt;br /&gt;
 rake db:test:load&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This two commands should suffice but a complete reference of rake commands for testing purpose is mentioned in &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html#preparing-your-application-for-testing&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65882</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w39 sn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w39_sn&amp;diff=65882"/>
		<updated>2012-09-29T07:03:48Z</updated>

		<summary type="html">&lt;p&gt;Npatowa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Setup test environment in Rails==&lt;br /&gt;
Rails provides a basic boiler plate to create tests.There are three environments provided by Rails - production,development and testing.As the names suggest they are used for different purposes.This prevents developers from messing with their development environments.Inside the rails app directory there will be a directory called test.This directory contains folders-unit,functional,integration and fixtures.The unit folder holds tests for the models, the functional folder is meant to hold tests for your controllers, and the integration folder contains tests that involve any number of controllers interacting.Fixtures contain the sample test data.Rails has the Test::Unit included by default but there are other frameworks also available like RSpec&amp;lt;ref&amp;gt;http://rspec.info/&amp;lt;/ref&amp;gt;,Cucumber(for behavior driven development),Shoulda &amp;lt;ref&amp;gt;https://github.com/thoughtbot/shoulda#readme&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Initial setup:&amp;lt;/h4&amp;gt;&lt;br /&gt;
If the application was created using the scaffold command then it should create a stub in test/unit directory.The initial code would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
 &lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
  # Replace this with your real tests.&lt;br /&gt;
  test &amp;quot;the truth&amp;quot; do&lt;br /&gt;
    assert true&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now if we wanted to add real tests to it then let us take two scenarios&lt;br /&gt;
1.Post with empty entries.&lt;br /&gt;
2.Post with actual entries&lt;br /&gt;
The code for these two test cases would look something like this&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'test_helper'&lt;br /&gt;
&lt;br /&gt;
class PostTest &amp;lt; ActiveSupport::TestCase&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new empty&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    assert !p.save, &amp;quot;Saved post without title, content, user, or category&amp;quot;&lt;br /&gt;
    assert p.invalid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  test &amp;quot;Post new correct&amp;quot; do&lt;br /&gt;
    p = Post.new&lt;br /&gt;
    #Post has following fields title,email,content&lt;br /&gt;
    p.title = 'General title'&lt;br /&gt;
    p.content = 'A new content'&lt;br /&gt;
    p.email = 'Azrael@ncsu.edu'&lt;br /&gt;
    #place an assert so as to find out whether this statement is valid or not&lt;br /&gt;
    assert p.valid?&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
test_helper.rb contains the default configuration to run the tests,ActiveSupport::TestCase defines the basic methods for defining &lt;br /&gt;
a test case.The test cases must begin with the name &amp;quot;test&amp;quot;. The statement that actually determines whether the test has passed or not is the assert statement.An assertion is a line of code that evaluates an object (or expression) for expected results.It can check a variety of things like is the expression true or false,is it valid etc. In this example, in the first test case we are checking whether p is an invalid object,if yes then the test has passed because that is the expected thing.Whereas the second test checks whether p is an valid object or not,if its not then the test fails as the expected output in this case is that p should be a valid object&lt;/div&gt;</summary>
		<author><name>Npatowa</name></author>
	</entry>
</feed>