<?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=Annice</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=Annice"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Annice"/>
	<updated>2026-07-20T18:59:38Z</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_2w53_iv&amp;diff=71037</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=71037"/>
		<updated>2012-11-20T02:11:40Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, Directory of Sites=&lt;br /&gt;
This article is presented in the form of directory of a number of sites explaining the use of Singleton pattern.Each of the link to the site is accompanied with the topics covered, a brief summary and a highlight of major points covered in the corresponding web page.&lt;br /&gt;
== [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia] ==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
== [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
== [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
== [http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html Simply Singleton] ==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''':&lt;br /&gt;
Capabilities of Singleton pattern,Singleton design pattern class diagram, Example, use of protected constructors.&lt;br /&gt;
&lt;br /&gt;
'''Summary''':&lt;br /&gt;
The Singleton design pattern addresses all of the previous paragraph's concerns. With the Singleton design pattern you can:&lt;br /&gt;
* Ensure that only one instance of a class is created.&lt;br /&gt;
* Provide a global point of access to the object.&lt;br /&gt;
* Allow multiple instances in the future without affecting a singleton class's clients.&lt;br /&gt;
&lt;br /&gt;
As in , [http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html#resources Design Patterns], &amp;quot;Ensure a class has a single instance, and provide a global point of access to it&amp;quot;&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:4.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Other than the lazy initialization process, a classicSingleton class can also implement a protected constructor so client cannot instantiate ClassicSingleton instances; however, you may be surprised to discover that the following code is perfectly legal:&lt;br /&gt;
&lt;br /&gt;
 public class SingletonInstantiator { &lt;br /&gt;
 public SingletonInstantiator() { &lt;br /&gt;
 ClassicSingleton instance = ClassicSingleton.getInstance();&lt;br /&gt;
 ClassicSingleton anotherInstance =&lt;br /&gt;
 new ClassicSingleton();&lt;br /&gt;
 ... &lt;br /&gt;
  } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Protected constructors can be called by subclasses and by other classes in the same package. Because ClassicSingleton and SingletonInstantiator are in the same package (the default package), SingletonInstantiator() methods can create ClassicSingleton instances. This dilemma has two solutions: You can make the ClassicSingleton constructor private so that only ClassicSingleton() methods call it; however, that means ClassicSingleton cannot be subclassed. Sometimes, that is a desirable solution; if so, it's a good idea to declare your singleton class final, which makes that intention explicit and allows the compiler to apply performance optimizations. The other solution is to put your singleton class in an explicit package, so classes in other packages (including the default package) cannot instantiate singleton instances.&lt;br /&gt;
&lt;br /&gt;
== [http://javapapers.com/design-patterns/singleton-pattern/ JavaPaper on Singleton]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''':&lt;br /&gt;
Strategy for Singleton instance creation, Early and lazy instantiation in singleton pattern, Singleton and Serialization&lt;br /&gt;
&lt;br /&gt;
'''Summary''':&lt;br /&gt;
There are only two points in the definition of a singleton design pattern,&lt;br /&gt;
* There should be only one instance allowed for a class and&lt;br /&gt;
* We should allow global point of access to that single instance.&lt;br /&gt;
&lt;br /&gt;
The key is not the problem and definition. In singleton pattern, trickier part is implementation and management of that single instance.&lt;br /&gt;
&lt;br /&gt;
We suppress the constructor and don’t allow even a single instance for the class. But we declare an attribute for that same class inside and create instance for that and return it. [http://javapapers.com/design-patterns/abstract-factory-pattern/ Factory design pattern] can be used to create the singleton instance.&lt;br /&gt;
&lt;br /&gt;
You need to be careful with multiple threads. If you don’t synchronize the method which is going to return the instance then, there is a possibility of allowing multiple instances in a multi-threaded scenario. Do the synchronization at block level considering the performance issues. In the below example for singleton pattern, you can see that it is threadsafe.&lt;br /&gt;
&lt;br /&gt;
 package com.javapapers.sample.designpattern;&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static Singleton singleInstance;&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getSingleInstance() {&lt;br /&gt;
 if (singleInstance == null) {&lt;br /&gt;
    synchronized (Singleton.class) {&lt;br /&gt;
      if (singleInstance == null) {&lt;br /&gt;
        singleInstance = new Singleton();&lt;br /&gt;
       }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return singleInstance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
'''Singleton and Serialization''' : Using [http://javapapers.com/core-java/java-serialization/ serialization], single instance contract of the singleton pattern can be violated. You can serialize and de-serialize and get a new instance of the same singleton class. Using java api, you can implement the below method and override the instance read from the stream. So that you can always ensure that you have single instance.&lt;br /&gt;
&lt;br /&gt;
 ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;&lt;br /&gt;
&lt;br /&gt;
'''Usage of Singleton Pattern in Java API''': &lt;br /&gt;
&lt;br /&gt;
 java.lang.Runtime#getRuntime() &lt;br /&gt;
 java.awt.Desktop#getDesktop()&lt;br /&gt;
&lt;br /&gt;
== [http://www.codeproject.com/Articles/307233/Singleton-Pattern-Positive-and-Negative-Aspects Positive and Negative aspects of Singleton] ==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''':&lt;br /&gt;
Introduction, Positive sides of Singleton, Lazy and Static initialization, Negative sides of Singleton, When to use a Singleton class.&lt;br /&gt;
&lt;br /&gt;
'''Summary''':&lt;br /&gt;
positive sides: The anatomy of a singleton class is very simple to understand. The class typically has a private constructor which will prohibit you to make any instance of the singleton class; instead you will access a static property or static function of the singleton class to get the reference of a preconfigured instance. These properties/methods ensure that there will be only one instance of the singleton class throughout the lifetime of the application.&lt;br /&gt;
&lt;br /&gt;
The one and only instance of a singleton class is created within the singleton class and its reference is consumed by the callers. The creation process of the instance can be done using any of the following methods:&lt;br /&gt;
&lt;br /&gt;
'''Lazy Initialization'''&lt;br /&gt;
If you opt for the lazy instantiation paradigm, then the singleton variable will not get memory until the property or function designated to return the reference is first called. This type of instantiation is very helpful if your singleton class is resource intense.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:5.gif]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In order to make it thread-safe, One way is the use of double-checked locking. In double-checked locking, synchronization is only effective when the singleton variable is null, i.e., only for the first time call to Instance.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:6.gif]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization'''&lt;br /&gt;
In static initialization, memory is allocated to the variable at the time it is declared. The instance creation takes place behind the scenes when any of the member singleton classes is accessed for the first time. The main advantage of this type of implementation is that the CLR automatically takes care of race conditions I explained in lazy instantiation. We don't have to use any special synchronization constructs here.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:7.gif]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* Inheriting a singleton class should be prohibited.&lt;br /&gt;
* Singleton takes over static classes on the following shortcomings:&lt;br /&gt;
  - Static classes don’t promote inheritance. If your class has some interface to derive from, static classes makes it impossible.&lt;br /&gt;
  - You cannot specify any creation logic with static methods. &lt;br /&gt;
  - Static methods are procedural code.&lt;br /&gt;
&lt;br /&gt;
'''Negative sides of Singleton''':&lt;br /&gt;
* They deviate from the Single Responsibility Principle. A singleton class has the responsibility to create an instance of itself along with other business responsibilities. However, this issue can be solved by delegating the creation part to a factory object.&lt;br /&gt;
* Singleton classes cannot be sub classed.&lt;br /&gt;
* Singletons can hide dependencies. One of the features of an efficient system architecture is minimizing dependencies between classes. This will in turn help you while conducting unit tests and while isolating any part of the program to a separate assembly.&lt;br /&gt;
&lt;br /&gt;
However, it is commonly accepted that the singleton can yield best results in a situation where various parts of an application concurrently try to access a shared resource. An example of a shared resource would be Logger, Print Spooler, etc. When designing a singleton, consider the following points:&lt;br /&gt;
* Singleton classes must be memory-leak free. The instance of the singleton class is to be created once and it remains for the lifetime of the application.&lt;br /&gt;
* A real singleton class is not easily extensible.&lt;br /&gt;
* Derive the singleton class from an interface. This helps while doing unit testing (using Dependency Injection).&lt;br /&gt;
&lt;br /&gt;
== [http://sourcemaking.com/design_patterns/singleton The Design pattern called Singleton]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Intent, Problem, Discussion, Structure, Example, Check List, Rules of Thumb  &lt;br /&gt;
&lt;br /&gt;
'''Summary''':&lt;br /&gt;
Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.&lt;br /&gt;
Singleton should be considered only if all three of the following criteria are satisfied:&lt;br /&gt;
* Ownership of the single instance cannot be reasonably assigned&lt;br /&gt;
* Lazy initialization is desirable&lt;br /&gt;
* Global access is not otherwise provided for&lt;br /&gt;
&lt;br /&gt;
If ownership of the single instance, when and how initialization occurs, and global access are not issues, Singleton is not sufficiently interesting.The Singleton pattern can be extended to support access to an application-specific number of instances. Make the class of the single instance responsible for access and “initialization on first use”. The single instance is a private static attribute. The accessor function is a public static method.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, “The President of the United States” is a global point of access that identifies the person in the office.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:8.gif]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''Check List''':&lt;br /&gt;
* Define a private static attribute in the “single instance” class.&lt;br /&gt;
* Define a public static accessor function in the class.&lt;br /&gt;
* Do “lazy initialization” (creation on first use) in the accessor function.&lt;br /&gt;
* Define all constructors to be protected or private.&lt;br /&gt;
* Clients may only use the accessor function to manipulate the Singleton.&lt;br /&gt;
&lt;br /&gt;
'''Rules of thumb''':&lt;br /&gt;
* Abstract Factory, Builder, and Prototype can use Singleton in their implementation.&lt;br /&gt;
* Facade objects are often Singletons because only one Facade object is required.&lt;br /&gt;
* State objects are often Singletons.&lt;br /&gt;
* The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and,   &lt;br /&gt;
you can change your mind and manage any number of instances.&lt;br /&gt;
* The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it.&lt;br /&gt;
* When is Singleton unnecessary? Short answer: most of the time. Long answer: when it’s simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally.&lt;br /&gt;
&lt;br /&gt;
== [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
== [http://www.oodesign.com/singleton-pattern.html Singleton Applications]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==[http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
* Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
* Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
* You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
== [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Extracting the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.&lt;br /&gt;
&lt;br /&gt;
== [http://www.roseindia.net/designpattern/singleton_pattern.shtml Singleton Pattern Usage and Benefits]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Usage, Benefits, Example&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and usage in multithreaded environment.Singleton patterns are often used as global variables because the global variables permit allocation and initialization whenever required. They don't permit to pollute the global namespace with unnecessary variables.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
 package singleton;&lt;br /&gt;
 public class Logger {&lt;br /&gt;
 private String fileName;&lt;br /&gt;
 private Properties properties;&lt;br /&gt;
 private Priority priority;&lt;br /&gt;
 private Logger() {&lt;br /&gt;
 logger = this;&lt;br /&gt;
 }&lt;br /&gt;
 public int getRegisteredLevel() {&lt;br /&gt;
 int i = 0;&lt;br /&gt;
 try {&lt;br /&gt;
 InputStream inputstream = getClass().getResourceAsStream(&amp;quot;Logger.properties&amp;quot;);&lt;br /&gt;
 properties.load(inputstream);&lt;br /&gt;
 inputstream.close();&lt;br /&gt;
 i = Integer.parseInt(properties.getProperty(&amp;quot;logger.registeredlevel&amp;quot;));&lt;br /&gt;
 if(i &amp;lt; 0 || i &amp;gt; 3)&lt;br /&gt;
 i = 0;&lt;br /&gt;
 }&lt;br /&gt;
 catch(Exception exception) {&lt;br /&gt;
 System.out.println(&amp;quot;Logger: Failed in the getRegisteredLevel method&amp;quot;);&lt;br /&gt;
 exception.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
 return i;&lt;br /&gt;
 }&lt;br /&gt;
 public static void initialize() {&lt;br /&gt;
 logger = new Logger();&lt;br /&gt;
 }&lt;br /&gt;
 // singleton - pattern&lt;br /&gt;
 private static Logger logger;&lt;br /&gt;
 public static Logger getLogger() {&lt;br /&gt;
 return logger;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== [http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern Critique of the Singleton Pattern]==&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Critique of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern.&lt;br /&gt;
&lt;br /&gt;
==[http://www.ibm.com/developerworks/webservices/library/co-single/index.html Use Singletons Wisely]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
When to use, Moving away from Singletons, Aggregating Singletons&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
The programming community discourages using global data and objects. Still, there are times when an application needs a single instance of a given class and a global point of access to that class. The general solution is the design pattern known as singletons. However, singletons are unnecessarily difficult to test and may make strong assumptions about the applications that will use them. In this article the author discusses strategies for avoiding the singleton pattern for that majority of cases where it is not appropriate. He also describes the properties of some classes that are truly singletons.&lt;br /&gt;
&lt;br /&gt;
'''Aggregating Singletons: The Toolbox''':&lt;br /&gt;
Singleton abuse can be avoided by looking at the problem from a different angle. Suppose an application needs only one instance of a class and the application configures that class at startup: Why should the class itself be responsible for being a singleton? It seems quite logical for the application to take on this responsibility, since the application requires this kind of behavior. The application, not the component, should be the singleton. The application then makes an instance of the component available for any application-specific code to use. When an application uses several such components, it can aggregate them into what we have called a toolbox.&lt;br /&gt;
Put simply, the application's toolbox is a singleton that is responsible either for configuring itself or for allowing the application's startup mechanism to configure it. The general pattern of the Toolbox singleton is as shown in the example provided in this article&lt;br /&gt;
The Toolbox is itself a singleton, and it manages the lifetime of the various component instances. Either the application configures it, or it asks the application for configuration information in method initialize. Now the application can decide how many instances of which classes it requires. Changes in those decisions may affect application-specific code, but not reusable, infrastructure-level code. Moreover, testing infrastructure code is much easier, as those classes do not rely on the way in which any application may choose to use them.&lt;br /&gt;
&lt;br /&gt;
== [http://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious When Not to Use Singleton]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Appropriate Use of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
The Gang of Four states that you'll want to use Singleton there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point or when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;br /&gt;
&lt;br /&gt;
http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/&lt;br /&gt;
&lt;br /&gt;
http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html&lt;br /&gt;
&lt;br /&gt;
http://javapapers.com/design-patterns/singleton-pattern/&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/307233/Singleton-Pattern-Positive-and-Negative-Aspects&lt;br /&gt;
&lt;br /&gt;
http://sourcemaking.com/design_patterns/singleton&lt;br /&gt;
&lt;br /&gt;
http://c2.com/cgi/wiki?SingletonPattern&lt;br /&gt;
&lt;br /&gt;
http://www.oodesign.com/singleton-pattern.html&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/ff650316.aspx&lt;br /&gt;
&lt;br /&gt;
http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html&lt;br /&gt;
&lt;br /&gt;
http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern&lt;br /&gt;
&lt;br /&gt;
http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern&lt;br /&gt;
&lt;br /&gt;
http://www.roseindia.net/designpattern/singleton_pattern.shtml&lt;br /&gt;
&lt;br /&gt;
http://www.ibm.com/developerworks/webservices/library/co-single/index.html&lt;br /&gt;
&lt;br /&gt;
http://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70716</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70716"/>
		<updated>2012-11-19T20:59:11Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, Directory of Sites=&lt;br /&gt;
== [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia] ==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
== [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
== [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
== [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
== [http://www.oodesign.com/singleton-pattern.html Singleton Applications]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==[http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
* Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
* Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
* You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
== [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Extracting the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.&lt;br /&gt;
&lt;br /&gt;
== [http://www.roseindia.net/designpattern/singleton_pattern.shtml Singleton Pattern Usage and Benefits]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Usage, Benefits, Example&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and usage in multithreaded environment.Singleton patterns are often used as global variables because the global variables permit allocation and initialization whenever required. They don't permit to pollute the global namespace with unnecessary variables.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
 package singleton;&lt;br /&gt;
 public class Logger {&lt;br /&gt;
 private String fileName;&lt;br /&gt;
 private Properties properties;&lt;br /&gt;
 private Priority priority;&lt;br /&gt;
 private Logger() {&lt;br /&gt;
 logger = this;&lt;br /&gt;
 }&lt;br /&gt;
 public int getRegisteredLevel() {&lt;br /&gt;
 int i = 0;&lt;br /&gt;
 try {&lt;br /&gt;
 InputStream inputstream = getClass().getResourceAsStream(&amp;quot;Logger.properties&amp;quot;);&lt;br /&gt;
 properties.load(inputstream);&lt;br /&gt;
 inputstream.close();&lt;br /&gt;
 i = Integer.parseInt(properties.getProperty(&amp;quot;logger.registeredlevel&amp;quot;));&lt;br /&gt;
 if(i &amp;lt; 0 || i &amp;gt; 3)&lt;br /&gt;
 i = 0;&lt;br /&gt;
 }&lt;br /&gt;
 catch(Exception exception) {&lt;br /&gt;
 System.out.println(&amp;quot;Logger: Failed in the getRegisteredLevel method&amp;quot;);&lt;br /&gt;
 exception.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
 return i;&lt;br /&gt;
 }&lt;br /&gt;
 public static void initialize() {&lt;br /&gt;
 logger = new Logger();&lt;br /&gt;
 }&lt;br /&gt;
 // singleton - pattern&lt;br /&gt;
 private static Logger logger;&lt;br /&gt;
 public static Logger getLogger() {&lt;br /&gt;
 return logger;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== [http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern Critique of the Singleton Pattern]==&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Critique of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern.&lt;br /&gt;
&lt;br /&gt;
==[http://www.ibm.com/developerworks/webservices/library/co-single/index.html Use Singletons Wisely]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
When to use, Moving away from Singletons, Aggregating Singletons&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
The programming community discourages using global data and objects. Still, there are times when an application needs a single instance of a given class and a global point of access to that class. The general solution is the design pattern known as singletons. However, singletons are unnecessarily difficult to test and may make strong assumptions about the applications that will use them. In this article the author discusses strategies for avoiding the singleton pattern for that majority of cases where it is not appropriate. He also describes the properties of some classes that are truly singletons.&lt;br /&gt;
&lt;br /&gt;
'''Aggregating Singletons: The Toolbox''':&lt;br /&gt;
Singleton abuse can be avoided by looking at the problem from a different angle. Suppose an application needs only one instance of a class and the application configures that class at startup: Why should the class itself be responsible for being a singleton? It seems quite logical for the application to take on this responsibility, since the application requires this kind of behavior. The application, not the component, should be the singleton. The application then makes an instance of the component available for any application-specific code to use. When an application uses several such components, it can aggregate them into what we have called a toolbox.&lt;br /&gt;
Put simply, the application's toolbox is a singleton that is responsible either for configuring itself or for allowing the application's startup mechanism to configure it. The general pattern of the Toolbox singleton is as shown in the example provided in this article&lt;br /&gt;
The Toolbox is itself a singleton, and it manages the lifetime of the various component instances. Either the application configures it, or it asks the application for configuration information in method initialize. Now the application can decide how many instances of which classes it requires. Changes in those decisions may affect application-specific code, but not reusable, infrastructure-level code. Moreover, testing infrastructure code is much easier, as those classes do not rely on the way in which any application may choose to use them.&lt;br /&gt;
&lt;br /&gt;
== [http://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious When Not to Use Singleton]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Appropriate Use of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
The Gang of Four states that you'll want to use Singleton there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point or when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;br /&gt;
&lt;br /&gt;
http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/&lt;br /&gt;
&lt;br /&gt;
http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html&lt;br /&gt;
&lt;br /&gt;
http://javapapers.com/design-patterns/singleton-pattern/&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/307233/Singleton-Pattern-Positive-and-Negative-Aspects&lt;br /&gt;
&lt;br /&gt;
http://sourcemaking.com/design_patterns/singleton&lt;br /&gt;
&lt;br /&gt;
http://c2.com/cgi/wiki?SingletonPattern&lt;br /&gt;
&lt;br /&gt;
http://www.oodesign.com/singleton-pattern.html&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/ff650316.aspx&lt;br /&gt;
&lt;br /&gt;
http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html&lt;br /&gt;
&lt;br /&gt;
http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern&lt;br /&gt;
&lt;br /&gt;
http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern&lt;br /&gt;
&lt;br /&gt;
http://www.roseindia.net/designpattern/singleton_pattern.shtml&lt;br /&gt;
&lt;br /&gt;
http://www.ibm.com/developerworks/webservices/library/co-single/index.html&lt;br /&gt;
&lt;br /&gt;
http://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70711</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70711"/>
		<updated>2012-11-19T20:57:34Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, Directory of Sites=&lt;br /&gt;
== [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia] ==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
== [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
== [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
== [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
== [http://www.oodesign.com/singleton-pattern.html Singleton Applications]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==[http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
* Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
* Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
* You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
== [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Extracting the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.&lt;br /&gt;
&lt;br /&gt;
== [http://www.roseindia.net/designpattern/singleton_pattern.shtml Singleton Pattern Usage and Benefits]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Usage, Benefits, Example&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and usage in multithreaded environment.Singleton patterns are often used as global variables because the global variables permit allocation and initialization whenever required. They don't permit to pollute the global namespace with unnecessary variables.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
 package singleton;&lt;br /&gt;
 public class Logger {&lt;br /&gt;
 private String fileName;&lt;br /&gt;
 private Properties properties;&lt;br /&gt;
 private Priority priority;&lt;br /&gt;
 private Logger() {&lt;br /&gt;
 logger = this;&lt;br /&gt;
 }&lt;br /&gt;
 public int getRegisteredLevel() {&lt;br /&gt;
 int i = 0;&lt;br /&gt;
 try {&lt;br /&gt;
 InputStream inputstream = getClass().getResourceAsStream(&amp;quot;Logger.properties&amp;quot;);&lt;br /&gt;
 properties.load(inputstream);&lt;br /&gt;
 inputstream.close();&lt;br /&gt;
 i = Integer.parseInt(properties.getProperty(&amp;quot;logger.registeredlevel&amp;quot;));&lt;br /&gt;
 if(i &amp;lt; 0 || i &amp;gt; 3)&lt;br /&gt;
 i = 0;&lt;br /&gt;
 }&lt;br /&gt;
 catch(Exception exception) {&lt;br /&gt;
 System.out.println(&amp;quot;Logger: Failed in the getRegisteredLevel method&amp;quot;);&lt;br /&gt;
 exception.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
 return i;&lt;br /&gt;
 }&lt;br /&gt;
 public static void initialize() {&lt;br /&gt;
 logger = new Logger();&lt;br /&gt;
 }&lt;br /&gt;
 // singleton - pattern&lt;br /&gt;
 private static Logger logger;&lt;br /&gt;
 public static Logger getLogger() {&lt;br /&gt;
 return logger;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== [http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern Critique of the Singleton Pattern]==&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Critique of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern.&lt;br /&gt;
&lt;br /&gt;
==[http://www.ibm.com/developerworks/webservices/library/co-single/index.html Use Singletons Wisely]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
When to use, Moving away from Singletons, Aggregating Singletons&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
The programming community discourages using global data and objects. Still, there are times when an application needs a single instance of a given class and a global point of access to that class. The general solution is the design pattern known as singletons. However, singletons are unnecessarily difficult to test and may make strong assumptions about the applications that will use them. In this article the author discusses strategies for avoiding the singleton pattern for that majority of cases where it is not appropriate. He also describes the properties of some classes that are truly singletons.&lt;br /&gt;
&lt;br /&gt;
'''Aggregating Singletons: The Toolbox''':&lt;br /&gt;
Singleton abuse can be avoided by looking at the problem from a different angle. Suppose an application needs only one instance of a class and the application configures that class at startup: Why should the class itself be responsible for being a singleton? It seems quite logical for the application to take on this responsibility, since the application requires this kind of behavior. The application, not the component, should be the singleton. The application then makes an instance of the component available for any application-specific code to use. When an application uses several such components, it can aggregate them into what we have called a toolbox.&lt;br /&gt;
Put simply, the application's toolbox is a singleton that is responsible either for configuring itself or for allowing the application's startup mechanism to configure it. The general pattern of the Toolbox singleton is as shown in the example provided in this article&lt;br /&gt;
The Toolbox is itself a singleton, and it manages the lifetime of the various component instances. Either the application configures it, or it asks the application for configuration information in method initialize. Now the application can decide how many instances of which classes it requires. Changes in those decisions may affect application-specific code, but not reusable, infrastructure-level code. Moreover, testing infrastructure code is much easier, as those classes do not rely on the way in which any application may choose to use them.&lt;br /&gt;
&lt;br /&gt;
== [http://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious When Not to Use Singleton]==&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Appropriate Use of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
The Gang of Four states that you'll want to use Singleton there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point or when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;br /&gt;
http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/&lt;br /&gt;
http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html&lt;br /&gt;
http://javapapers.com/design-patterns/singleton-pattern/&lt;br /&gt;
http://www.codeproject.com/Articles/307233/Singleton-Pattern-Positive-and-Negative-Aspects&lt;br /&gt;
http://sourcemaking.com/design_patterns/singleton&lt;br /&gt;
http://c2.com/cgi/wiki?SingletonPattern&lt;br /&gt;
http://www.oodesign.com/singleton-pattern.html&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/ff650316.aspx&lt;br /&gt;
http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html&lt;br /&gt;
http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/&lt;br /&gt;
http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern&lt;br /&gt;
http://www.roseindia.net/designpattern/singleton_pattern.shtml&lt;br /&gt;
http://www.ibm.com/developerworks/webservices/library/co-single/index.html&lt;br /&gt;
http://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70706</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70706"/>
		<updated>2012-11-19T20:53:05Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia] =&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
= [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]=&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
= [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]=&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
= [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]=&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
= [http://www.oodesign.com/singleton-pattern.html Singleton Applications]=&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=[http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]=&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]=&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
* Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
* Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
* You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
= [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]=&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Extracting the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.&lt;br /&gt;
&lt;br /&gt;
= [http://www.roseindia.net/designpattern/singleton_pattern.shtml Singleton Pattern Usage and Benefits]=&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Usage, Benefits, Example&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and usage in multithreaded environment.Singleton patterns are often used as global variables because the global variables permit allocation and initialization whenever required. They don't permit to pollute the global namespace with unnecessary variables.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
 package singleton;&lt;br /&gt;
 public class Logger {&lt;br /&gt;
 private String fileName;&lt;br /&gt;
 private Properties properties;&lt;br /&gt;
 private Priority priority;&lt;br /&gt;
 private Logger() {&lt;br /&gt;
 logger = this;&lt;br /&gt;
 }&lt;br /&gt;
 public int getRegisteredLevel() {&lt;br /&gt;
 int i = 0;&lt;br /&gt;
 try {&lt;br /&gt;
 InputStream inputstream = getClass().getResourceAsStream(&amp;quot;Logger.properties&amp;quot;);&lt;br /&gt;
 properties.load(inputstream);&lt;br /&gt;
 inputstream.close();&lt;br /&gt;
 i = Integer.parseInt(properties.getProperty(&amp;quot;logger.registeredlevel&amp;quot;));&lt;br /&gt;
 if(i &amp;lt; 0 || i &amp;gt; 3)&lt;br /&gt;
 i = 0;&lt;br /&gt;
 }&lt;br /&gt;
 catch(Exception exception) {&lt;br /&gt;
 System.out.println(&amp;quot;Logger: Failed in the getRegisteredLevel method&amp;quot;);&lt;br /&gt;
 exception.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
 return i;&lt;br /&gt;
 }&lt;br /&gt;
 public static void initialize() {&lt;br /&gt;
 logger = new Logger();&lt;br /&gt;
 }&lt;br /&gt;
 // singleton - pattern&lt;br /&gt;
 private static Logger logger;&lt;br /&gt;
 public static Logger getLogger() {&lt;br /&gt;
 return logger;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
= [http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern Critique of the Singleton Pattern]=&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Critique of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern.&lt;br /&gt;
&lt;br /&gt;
=[http://www.ibm.com/developerworks/webservices/library/co-single/index.html Use Singletons Wisely]=&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
When to use, Moving away from Singletons, Aggregating Singletons&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
The programming community discourages using global data and objects. Still, there are times when an application needs a single instance of a given class and a global point of access to that class. The general solution is the design pattern known as singletons. However, singletons are unnecessarily difficult to test and may make strong assumptions about the applications that will use them. In this article the author discusses strategies for avoiding the singleton pattern for that majority of cases where it is not appropriate. He also describes the properties of some classes that are truly singletons.&lt;br /&gt;
&lt;br /&gt;
'''Aggregating Singletons: The Toolbox''':&lt;br /&gt;
Singleton abuse can be avoided by looking at the problem from a different angle. Suppose an application needs only one instance of a class and the application configures that class at startup: Why should the class itself be responsible for being a singleton? It seems quite logical for the application to take on this responsibility, since the application requires this kind of behavior. The application, not the component, should be the singleton. The application then makes an instance of the component available for any application-specific code to use. When an application uses several such components, it can aggregate them into what we have called a toolbox.&lt;br /&gt;
Put simply, the application's toolbox is a singleton that is responsible either for configuring itself or for allowing the application's startup mechanism to configure it. The general pattern of the Toolbox singleton is as shown in the example provided in this article&lt;br /&gt;
The Toolbox is itself a singleton, and it manages the lifetime of the various component instances. Either the application configures it, or it asks the application for configuration information in method initialize. Now the application can decide how many instances of which classes it requires. Changes in those decisions may affect application-specific code, but not reusable, infrastructure-level code. Moreover, testing infrastructure code is much easier, as those classes do not rely on the way in which any application may choose to use them.&lt;br /&gt;
&lt;br /&gt;
= [http://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious When Not to Use Singleton]=&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Appropriate Use of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
The Gang of Four states that you'll want to use Singleton there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point or when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70541</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70541"/>
		<updated>2012-11-19T06:10:16Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Singleton, directory of sites =&lt;br /&gt;
===1.[http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]===&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
7. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
* Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
* Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
* You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
11. [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Extracting the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.&lt;br /&gt;
&lt;br /&gt;
12. [http://www.roseindia.net/designpattern/singleton_pattern.shtml Singleton Pattern Usage and Benefits]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Usage, Benefits, Example&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and usage in multithreaded environment.Singleton patterns are often used as global variables because the global variables permit allocation and initialization whenever required. They don't permit to pollute the global namespace with unnecessary variables.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
 package singleton;&lt;br /&gt;
 public class Logger {&lt;br /&gt;
 private String fileName;&lt;br /&gt;
 private Properties properties;&lt;br /&gt;
 private Priority priority;&lt;br /&gt;
 private Logger() {&lt;br /&gt;
 logger = this;&lt;br /&gt;
 }&lt;br /&gt;
 public int getRegisteredLevel() {&lt;br /&gt;
 int i = 0;&lt;br /&gt;
 try {&lt;br /&gt;
 InputStream inputstream = getClass().getResourceAsStream(&amp;quot;Logger.properties&amp;quot;);&lt;br /&gt;
 properties.load(inputstream);&lt;br /&gt;
 inputstream.close();&lt;br /&gt;
 i = Integer.parseInt(properties.getProperty(&amp;quot;logger.registeredlevel&amp;quot;));&lt;br /&gt;
 if(i &amp;lt; 0 || i &amp;gt; 3)&lt;br /&gt;
 i = 0;&lt;br /&gt;
 }&lt;br /&gt;
 catch(Exception exception) {&lt;br /&gt;
 System.out.println(&amp;quot;Logger: Failed in the getRegisteredLevel method&amp;quot;);&lt;br /&gt;
 exception.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
 return i;&lt;br /&gt;
 }&lt;br /&gt;
 public static void initialize() {&lt;br /&gt;
 logger = new Logger();&lt;br /&gt;
 }&lt;br /&gt;
 // singleton - pattern&lt;br /&gt;
 private static Logger logger;&lt;br /&gt;
 public static Logger getLogger() {&lt;br /&gt;
 return logger;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
13. [http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern Critique of the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Critique of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern.&lt;br /&gt;
&lt;br /&gt;
14.[http://www.ibm.com/developerworks/webservices/library/co-single/index.html Use Singletons Wisely]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
When to use, Moving away from Singletons, Aggregating Singletons&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
The programming community discourages using global data and objects. Still, there are times when an application needs a single instance of a given class and a global point of access to that class. The general solution is the design pattern known as singletons. However, singletons are unnecessarily difficult to test and may make strong assumptions about the applications that will use them. In this article the author discusses strategies for avoiding the singleton pattern for that majority of cases where it is not appropriate. He also describes the properties of some classes that are truly singletons.&lt;br /&gt;
&lt;br /&gt;
'''Aggregating Singletons: The Toolbox''':&lt;br /&gt;
Singleton abuse can be avoided by looking at the problem from a different angle. Suppose an application needs only one instance of a class and the application configures that class at startup: Why should the class itself be responsible for being a singleton? It seems quite logical for the application to take on this responsibility, since the application requires this kind of behavior. The application, not the component, should be the singleton. The application then makes an instance of the component available for any application-specific code to use. When an application uses several such components, it can aggregate them into what we have called a toolbox.&lt;br /&gt;
Put simply, the application's toolbox is a singleton that is responsible either for configuring itself or for allowing the application's startup mechanism to configure it. The general pattern of the Toolbox singleton is as shown in the example provided in this article&lt;br /&gt;
The Toolbox is itself a singleton, and it manages the lifetime of the various component instances. Either the application configures it, or it asks the application for configuration information in method initialize. Now the application can decide how many instances of which classes it requires. Changes in those decisions may affect application-specific code, but not reusable, infrastructure-level code. Moreover, testing infrastructure code is much easier, as those classes do not rely on the way in which any application may choose to use them.&lt;br /&gt;
&lt;br /&gt;
15. [http://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious When Not to Use Singleton]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Appropriate Use of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
The Gang of Four states that you'll want to use Singleton there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point or when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70519</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70519"/>
		<updated>2012-11-19T05:20:11Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Singleton, directory of sites =&lt;br /&gt;
===1.[http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]===&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
7. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
* Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
* Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
* You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
11. [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Extracting the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.&lt;br /&gt;
&lt;br /&gt;
12. [http://www.roseindia.net/designpattern/singleton_pattern.shtml Singleton Pattern Usage and Benefits]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Usage, Benefits, Example&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and usage in multithreaded environment.Singleton patterns are often used as global variables because the global variables permit allocation and initialization whenever required. They don't permit to pollute the global namespace with unnecessary variables.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
 package singleton;&lt;br /&gt;
 public class Logger {&lt;br /&gt;
 private String fileName;&lt;br /&gt;
 private Properties properties;&lt;br /&gt;
 private Priority priority;&lt;br /&gt;
 private Logger() {&lt;br /&gt;
 logger = this;&lt;br /&gt;
 }&lt;br /&gt;
 public int getRegisteredLevel() {&lt;br /&gt;
 int i = 0;&lt;br /&gt;
 try {&lt;br /&gt;
 InputStream inputstream = getClass().getResourceAsStream(&amp;quot;Logger.properties&amp;quot;);&lt;br /&gt;
 properties.load(inputstream);&lt;br /&gt;
 inputstream.close();&lt;br /&gt;
 i = Integer.parseInt(properties.getProperty(&amp;quot;logger.registeredlevel&amp;quot;));&lt;br /&gt;
 if(i &amp;lt; 0 || i &amp;gt; 3)&lt;br /&gt;
 i = 0;&lt;br /&gt;
 }&lt;br /&gt;
 catch(Exception exception) {&lt;br /&gt;
 System.out.println(&amp;quot;Logger: Failed in the getRegisteredLevel method&amp;quot;);&lt;br /&gt;
 exception.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
 return i;&lt;br /&gt;
 }&lt;br /&gt;
 public static void initialize() {&lt;br /&gt;
 logger = new Logger();&lt;br /&gt;
 }&lt;br /&gt;
 // singleton - pattern&lt;br /&gt;
 private static Logger logger;&lt;br /&gt;
 public static Logger getLogger() {&lt;br /&gt;
 return logger;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
13. [http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern Critique of the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Critique of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern.&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70517</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70517"/>
		<updated>2012-11-19T04:48:21Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Singleton, directory of sites =&lt;br /&gt;
===1.[http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]===&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
7. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
* Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
* Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
* Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
* You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
11. [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Extracting the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
12. [http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern Critique of the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Critique of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern.&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70504</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70504"/>
		<updated>2012-11-19T04:17:08Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Singleton, directory of sites =&lt;br /&gt;
===1.[http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]===&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
1. Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
2. Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
3. Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4. Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
5.The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
6. Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
7. You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
11. [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Extracting the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
12. [http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern Critique of the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Critique of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern.&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70392</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70392"/>
		<updated>2012-11-19T00:26:27Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Singleton, directory of sites =&lt;br /&gt;
===1.[http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]===&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
1. Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
2. Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
3. Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4. Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
5.The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
6. Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
7. You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
11. [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Extracting the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
11. [http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern Critique of the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Critique of the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern.&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70390</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70390"/>
		<updated>2012-11-19T00:21:37Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Singleton, directory of sites =&lt;br /&gt;
===1.[http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]===&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
1. Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
2. Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
3. Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4. Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
5.The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
6. Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
7. You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
11. [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Extracting the Singleton Pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70389</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70389"/>
		<updated>2012-11-19T00:17:03Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Singleton, directory of sites =&lt;br /&gt;
===1.[http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]===&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
1. Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
2. Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
3. Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4. Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
5.The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
6. Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
7. You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70385</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70385"/>
		<updated>2012-11-19T00:10:57Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Singleton, directory of sites =&lt;br /&gt;
===1. [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]===&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
1. Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
2. Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
3. Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4. Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
5.The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
6. Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
7. You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70383</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70383"/>
		<updated>2012-11-19T00:09:27Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Singleton, directory of sites =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===1. [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]===&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
1. Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
2. Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
3. Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4. Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
5.The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
6. Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
7. You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70380</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70380"/>
		<updated>2012-11-19T00:05:17Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, directory of sites=&lt;br /&gt;
==Singleton, directory of sites Pattern==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
1. Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
2. Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
3. Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4. Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
5.The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
6. Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
7. You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70378</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70378"/>
		<updated>2012-11-19T00:04:20Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, directory of sites=&lt;br /&gt;
==Singleton, directory of sites Pattern==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
10. [bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Solution to refactor singleton&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Steps to refactor Singleton:'''&lt;br /&gt;
1. Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.&lt;br /&gt;
 public interface ISingletonRegistry {&lt;br /&gt;
 SingletonClass getSingletonClass();&lt;br /&gt;
 void setSingletonClass(SingletonClass singleton);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
2. Make SingletonClass's constructor public&lt;br /&gt;
&lt;br /&gt;
3. Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
 public class SingletonRegistry implements ISingletonRegistry {&lt;br /&gt;
 private static final SingletonRegistry INSTANCE = new SingletonRegistry();&lt;br /&gt;
 private SingletonClass singleton;&lt;br /&gt;
 public static ISingletonRegistry getInstance() {&lt;br /&gt;
 return INSTANCE;&lt;br /&gt;
 }&lt;br /&gt;
 private SingletonRegistry() {&lt;br /&gt;
 //this is the reason why the constructor should be public&lt;br /&gt;
 singleton = new SingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
 public SingletonClass getSingletonClass() {&lt;br /&gt;
 return singleton;&lt;br /&gt;
 }&lt;br /&gt;
 public void setSingletonClass(SingletonClass singleton) {&lt;br /&gt;
 this.singleton = singleton;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4. Change getInstance method of SingletonClass to get instance from SingletonRegistry.&lt;br /&gt;
 public static SingletonClass getInstance() {&lt;br /&gt;
 return SingletonRegistry.getInstance().getSingletonClass();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
5.The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.&lt;br /&gt;
 public class Client {&lt;br /&gt;
 public void clientMethod() {&lt;br /&gt;
 SingletonRegistry.getInstance() .getSingletonClass().voidMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod(&amp;quot;param&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
6. Delete INSTANCE constant from SingletonClass class.&lt;br /&gt;
&lt;br /&gt;
7. You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.&lt;br /&gt;
&lt;br /&gt;
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70357</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70357"/>
		<updated>2012-11-18T22:57:25Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, directory of sites=&lt;br /&gt;
==Singleton, directory of sites Pattern==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
9. [http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Implementation in C#&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link explains how to implement a singleton in C# both in static and threaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Static Initialization''':&lt;br /&gt;
&lt;br /&gt;
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 public sealed class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static readonly Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton(){}&lt;br /&gt;
 public static Singleton Instance&lt;br /&gt;
 {&lt;br /&gt;
 get &lt;br /&gt;
 { &lt;br /&gt;
 return instance; &lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing Singleton in C# results in the following benefits and liabilities:&lt;br /&gt;
&lt;br /&gt;
'''Benefits''':&lt;br /&gt;
*The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.&lt;br /&gt;
*The Double-Check Locking idiom described earlier in &amp;quot;Multithreaded Singleton&amp;quot; is implemented correctly in the common language runtime.&lt;br /&gt;
&lt;br /&gt;
'''Liabilities''':&lt;br /&gt;
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70356</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70356"/>
		<updated>2012-11-18T22:33:26Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, directory of sites=&lt;br /&gt;
==Singleton, directory of sites Pattern==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Problem of using Singleton in Multithreaded Environment''' : &lt;br /&gt;
&lt;br /&gt;
 static private synchronized Singleton instance_helper(){&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created&lt;br /&gt;
&lt;br /&gt;
'''Solution for Singleton in Multithreaded Environment''' : &lt;br /&gt;
 &lt;br /&gt;
 static private synchronized Singleton instance_helper()&lt;br /&gt;
 {&lt;br /&gt;
 if(flag)&lt;br /&gt;
 {&lt;br /&gt;
 _instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 flag = false;&lt;br /&gt;
 return _instance;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
8. [http://www.oodesign.com/singleton-pattern.html Singleton Applications]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Motivation, Intent, Implementation, Applicability and Examples&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved&lt;br /&gt;
&lt;br /&gt;
'''Serialization''' :&lt;br /&gt;
&lt;br /&gt;
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented. &lt;br /&gt;
&lt;br /&gt;
 public class Singleton implements Serializable {&lt;br /&gt;
 ...&lt;br /&gt;
 // This method is called immediately after an object of this class is deserialized.&lt;br /&gt;
 // This method returns the singleton instance.&lt;br /&gt;
 protected Object readResolve() {&lt;br /&gt;
 return getInstance();&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70159</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70159"/>
		<updated>2012-11-18T14:26:06Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, directory of sites=&lt;br /&gt;
==Singleton, directory of sites Pattern==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Author's solution is create a private synchronized method.''' : &lt;br /&gt;
&lt;br /&gt;
	/** @return The unique instance of this class.&lt;br /&gt;
        */&lt;br /&gt;
	static public Singleton instance() {&lt;br /&gt;
	if(null == _instance) {&lt;br /&gt;
        instance_helper();&lt;br /&gt;
	System.out.println(&amp;quot;CREATE NEW SYSTEM INSTANCE!!!!!!!!!!&amp;quot;);		&lt;br /&gt;
	}else{&lt;br /&gt;
	System.out.println(&amp;quot;REUSE THE SAME SYSTEM INSTANCE!!&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
	return _instance;&lt;br /&gt;
	}/**&lt;br /&gt;
        Solution for thread safe issue, only one thread can create Singleton object.&lt;br /&gt;
        And reduce the expensive cost of synchonize since this method is only call&lt;br /&gt;
        only one time.&lt;br /&gt;
         */&lt;br /&gt;
	 static private synchronized Singleton instance_helper(){&lt;br /&gt;
	 _instance = new Singleton();&lt;br /&gt;
	 return _instance;&lt;br /&gt;
	 } /**Except it doesn't work. &amp;quot;_instance&amp;quot; can be null in the logic for multiple threads and multiple instances being created&lt;br /&gt;
           How about putting a flag in synchronized method ?*/&lt;br /&gt;
	static private synchronized Singleton instance_helper(){&lt;br /&gt;
	if(flag){&lt;br /&gt;
		_instance = new Singleton();&lt;br /&gt;
	}&lt;br /&gt;
	flag = false;&lt;br /&gt;
	return _instance;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70158</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70158"/>
		<updated>2012-11-18T14:24:36Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, directory of sites=&lt;br /&gt;
==Singleton, directory of sites Pattern==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Author's solution is create a private synchronized method.''' : &lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
          @return The unique instance of this class.&lt;br /&gt;
        */&lt;br /&gt;
	static public Singleton instance() {&lt;br /&gt;
	if(null == _instance) {&lt;br /&gt;
		&lt;br /&gt;
         instance_helper();&lt;br /&gt;
	System.out.println(&amp;quot;CREATE NEW    &lt;br /&gt;
          SYSTEM INSTANCE!!!!!!!!!!&amp;quot;);		&lt;br /&gt;
	}else{&lt;br /&gt;
	System.out.println(&amp;quot;REUSE THE SAME SYSTEM INSTANCE!!&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
	return _instance;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
        Solution for thread safe issue, only one thread can create Singleton object.&lt;br /&gt;
        And reduce the expensive cost of synchonize since this method is only call&lt;br /&gt;
        only one time.&lt;br /&gt;
         */&lt;br /&gt;
	 static private synchronized Singleton instance_helper(){&lt;br /&gt;
	 _instance = new Singleton();&lt;br /&gt;
	 return _instance;&lt;br /&gt;
	 }&lt;br /&gt;
&lt;br /&gt;
        /**Except it doesn't work. &amp;quot;_instance&amp;quot; can be null in the logic for multiple threads and multiple instances being created.*/&lt;br /&gt;
&lt;br /&gt;
         /*How about putting a flag in synchronized method ?*/&lt;br /&gt;
	static private synchronized Singleton instance_helper(){&lt;br /&gt;
	if(flag){&lt;br /&gt;
		_instance = new Singleton();&lt;br /&gt;
	}&lt;br /&gt;
	flag = false;&lt;br /&gt;
	return _instance;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70157</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=70157"/>
		<updated>2012-11-18T14:18:06Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, directory of sites=&lt;br /&gt;
==Singleton, directory of sites Pattern==&lt;br /&gt;
&lt;br /&gt;
1. [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns. &lt;br /&gt;
&lt;br /&gt;
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.&lt;br /&gt;
&lt;br /&gt;
Lazy initialization uses double-checking and eager initialization which always creates an instance. &lt;br /&gt;
For instance, Lazy initialization and Eager Initialization example codes are:&lt;br /&gt;
'''Lazy Initialization''':&lt;br /&gt;
 public class SingletonDemo {&lt;br /&gt;
 private static volatile SingletonDemo instance = null;&lt;br /&gt;
 private SingletonDemo() {}&lt;br /&gt;
 public static SingletonDemo getInstance() {&lt;br /&gt;
 if (instance == null) {&lt;br /&gt;
   synchronized (SingletonDemo .class){&lt;br /&gt;
   if (instance == null) {&lt;br /&gt;
   instance = new SingletonDemo ();&lt;br /&gt;
                       }&lt;br /&gt;
                    }&lt;br /&gt;
               }&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
'''Eager Initialization''':&lt;br /&gt;
 public class Singleton {&lt;br /&gt;
 private static final Singleton instance = new Singleton();&lt;br /&gt;
 private Singleton() {}&lt;br /&gt;
 public static Singleton getInstance() {&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :-&amp;gt; &lt;br /&gt;
 Foo := Object clone &lt;br /&gt;
 Foo clone := Foo&lt;br /&gt;
&lt;br /&gt;
'''Drawbacks''' :&lt;br /&gt;
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.&lt;br /&gt;
&lt;br /&gt;
2. [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' :&lt;br /&gt;
Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:3.jpg|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.&lt;br /&gt;
&lt;br /&gt;
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Intent, Description, An Example, Implementation, Benefits&lt;br /&gt;
&lt;br /&gt;
'''Summary''' :&lt;br /&gt;
It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once.&lt;br /&gt;
Then, it describes two solutions for implementing the singleton class. &lt;br /&gt;
In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private.&lt;br /&gt;
The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|[[File:1.gif|x300px]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The code for each case is given as follows: &lt;br /&gt;
&lt;br /&gt;
'''Case 1''' :&lt;br /&gt;
 class Singleton&lt;br /&gt;
 {&lt;br /&gt;
 private static Singleton instance;&lt;br /&gt;
 private static int numOfReference;&lt;br /&gt;
 private string code;&lt;br /&gt;
 private Singleton()&lt;br /&gt;
 {&lt;br /&gt;
 numOfReference = 0;&lt;br /&gt;
 code = &amp;quot;Maasoom Faraz&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 public static Singleton GetInstance()&lt;br /&gt;
 {&lt;br /&gt;
 if(instance == null)&lt;br /&gt;
 {&lt;br /&gt;
 instance = new Singleton();&lt;br /&gt;
 }&lt;br /&gt;
 numOfReference++;&lt;br /&gt;
 return instance;&lt;br /&gt;
 }&lt;br /&gt;
 public static int Reference&lt;br /&gt;
 {&lt;br /&gt;
 get { return numOfReference; }&lt;br /&gt;
 }&lt;br /&gt;
 public string Code&lt;br /&gt;
 {&lt;br /&gt;
 get { return code; }&lt;br /&gt;
 set { code = value;}&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.&lt;br /&gt;
&lt;br /&gt;
'''Case 2''' :&lt;br /&gt;
 class Singleton2&lt;br /&gt;
 {&lt;br /&gt;
 private static int numOfInstance = 0;&lt;br /&gt;
 public Singleton2()&lt;br /&gt;
 {&lt;br /&gt;
 if(numOfInstance == 0)&lt;br /&gt;
 {&lt;br /&gt;
 Console.WriteLine(&amp;quot;\r\nCreating First Object of Singleton2 class...&amp;quot;);&lt;br /&gt;
 numOfInstance++;&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
 throw new Exception(&amp;quot;This class is Singleton,&lt;br /&gt;
 + so only one object of it can be instantiated.&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/&lt;br /&gt;
&lt;br /&gt;
1. [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]&lt;br /&gt;
&lt;br /&gt;
'''Topics Covered''' : &lt;br /&gt;
Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment&lt;br /&gt;
&lt;br /&gt;
'''Summary''' : &lt;br /&gt;
This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.&lt;br /&gt;
&lt;br /&gt;
'''Author's solution is create a private synchronized method.''' : &lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
@return The unique instance of this class.&lt;br /&gt;
/&lt;br /&gt;
	static public Singleton instance() {&lt;br /&gt;
	if(null == _instance) {&lt;br /&gt;
		instance_helper();&lt;br /&gt;
		System.out.println(&amp;quot;CREATE NEW SYSTEM INSTANCE!!!!!!!!!!&amp;quot;);		&lt;br /&gt;
	}else{&lt;br /&gt;
		System.out.println(&amp;quot;REUSE THE SAME SYSTEM INSTANCE!!&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
	return _instance;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
Solution for thread safe issue, only one thread can create Singleton object.&lt;br /&gt;
And reduce the expensive cost of synchonize since this method is only call&lt;br /&gt;
only one time.&lt;br /&gt;
/&lt;br /&gt;
	static private synchronized Singleton instance_helper(){&lt;br /&gt;
	_instance = new Singleton();&lt;br /&gt;
	return _instance;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
Except it doesn't work. &amp;quot;_instance&amp;quot; can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created. See DoubleCheckedLockingIsBroken.&lt;br /&gt;
&lt;br /&gt;
How about putting a flag in synchronized method ?&lt;br /&gt;
	static private synchronized Singleton instance_helper(){&lt;br /&gt;
	if(flag){&lt;br /&gt;
		_instance = new Singleton();&lt;br /&gt;
	}&lt;br /&gt;
	flag = false;&lt;br /&gt;
	return _instance;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=69965</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=69965"/>
		<updated>2012-11-17T19:31:54Z</updated>

		<summary type="html">&lt;p&gt;Annice: Replaced content with &amp;quot;=Singleton, directory of sites=
==Singleton, directory of sites Pattern==

===Introduction===


===Principle===



===Advantages of Singleton, directory of sites Pattern===

...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Singleton, directory of sites=&lt;br /&gt;
==Singleton, directory of sites Pattern==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Principle===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages of Singleton, directory of sites Pattern===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=69964</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w53 iv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w53_iv&amp;diff=69964"/>
		<updated>2012-11-17T19:26:05Z</updated>

		<summary type="html">&lt;p&gt;Annice: Created page with &amp;quot;=Mediator and related patterns= ==Mediator Pattern== ===Introduction===  Mediator pattern comes under the category of behavioral pattern&amp;lt;ref name=&amp;quot;wikibehav&amp;quot;&amp;gt;http://en.wikipedia....&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Mediator and related patterns=&lt;br /&gt;
==Mediator Pattern==&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
Mediator pattern comes under the category of behavioral pattern&amp;lt;ref name=&amp;quot;wikibehav&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Behavioral_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;. It specifies an object that includes the encapsulation about the interaction among various objects.&lt;br /&gt;
&lt;br /&gt;
It prevents objects to interact with each other explicitly and thus it facilitates loose coupling&amp;lt;ref name=&amp;quot;wikiloosecoupling&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Loose_coupling&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;. In other words it handles complex communication among related objects. All the objects communicate with the mediator when they want to interact with each other. Thus it improves maintainability.&lt;br /&gt;
&lt;br /&gt;
All the classes which interact with the mediator are called colleagues. These colleagues know who their mediator is and the mediator also knows which colleagues can interact with it. The colleagues send messages to the mediator object. The mediator sends message to other classes. Thus the object which initiated this communication does not need to have knowledge about other objects. All they need to know is about their mediator who is responsible to carry out this communication.&lt;br /&gt;
&lt;br /&gt;
Following classes/objects take part in this process:&lt;br /&gt;
&lt;br /&gt;
'''Mediator''': Provides interface for communication among colleagues.&lt;br /&gt;
&lt;br /&gt;
'''Concrete Mediator''': It coordinates the colleagues and implements cooperative behavior. It also holds information about the colleagues.&lt;br /&gt;
&lt;br /&gt;
'''Colleague Classes''': Colleague classes know about their mediator.&lt;br /&gt;
&lt;br /&gt;
A real life example of mediator pattern can be of an airport control tower. It is the job of this tower to control all the planes. It decides which plane will take-off and land at what time rather than plane to plane communication.&lt;br /&gt;
&lt;br /&gt;
===Case1: Without the use of Mediator Pattern===&lt;br /&gt;
[[File:Image1_womediator1.png|650 px|thumb|right|Example: Without using Mediator Pattern]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this example, there are four departments of a hospital, namely :Pathology, Radiology, Surgery and Physiology. When one department needs some data from another department, they need to explicitly contact the other department for that data. Since these departments directly interact with each other, the communication between these parties is very complex and results in high coupling. &lt;br /&gt;
&lt;br /&gt;
For Example: When the Pathology department needs X-Ray report from the Sonography department it calls the 'getXRay()' method. Similarly, there are other methods which are often required by other departments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Case2: With the use of Mediator Pattern===&lt;br /&gt;
[[File:Image2_withmediator.png|650 px|thumb|right|Example: Using Mediator Pattern]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To reduce coupling in the above example we add a Hospital Management Mediator which sits in between all the departments and all the communication among them is handled by the mediator. The control logic of the entire system is included in the mediator. &lt;br /&gt;
&lt;br /&gt;
Whenever a new department is added or some functionality of an existing department needs to be changed, only the mediator control logic needs to be modified.&lt;br /&gt;
This facilitates loose coupling as the departments do not need to communicate with each other explicitly. They also do not need to know about internal details of other departments. &lt;br /&gt;
&lt;br /&gt;
For example: the 'getXRay()' method of the Pathology department is now inside the Hospital Management Mediator and is called only of the patient is of type 'Pathology Patient'.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages of Mediator Pattern===&lt;br /&gt;
* It facilitates re-usability of objects. It is possible due to the fact that objects are decoupled from the system.&lt;br /&gt;
* Overall maintenance of the system gets simplified because the control logic is encapsulated in the mediator.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Facade Pattern==&lt;br /&gt;
A Facade design pattern provides a unified interface to a larger body of code. It is a structural pattern&amp;lt;ref name=&amp;quot;wikistructural&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Structural_pattern&amp;lt;br&amp;gt;&amp;lt;/ref&amp;gt;. This makes the use of subsystem easy. This pattern wraps the complicated subsystem with a simple interface. A facade pattern shields users from the complex inner details of the subsystem. Example of facade pattern could be of a web service where users can get access to the small services without knowing anything about their internal structures. They just need an interface which facade pattern provides.&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
For the same example as discussed above let us see how a facade pattern looks.&lt;br /&gt;
&lt;br /&gt;
[[File:image3_facadepattern.png|650 px|thumb|right|Example: Using Facade Pattern]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this case a facade pattern provides an interface to the patients through which he can interact with the departments. The internal details of the departments are hidden from the patient. So whenever he needs to get any service from any of the department he only communicates with the interface. The patient may not be provided with all the functionality and he is exposed to only certain functionality.&lt;br /&gt;
&lt;br /&gt;
In this example, the patient has access to the facade interface and can only call the 'getFullReport()' method to obtain the checkup reports. The 'getFullReport()' method in turn calls various methods of various departments. Thus the patient is unaware of the implementation of these methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Comparison of mediator pattern with facade pattern==&lt;br /&gt;
*Mediator pattern is a behavioral pattern whereas facade pattern is a structural pattern.&lt;br /&gt;
&lt;br /&gt;
*Mediator pattern adds extra functionality whereas facade pattern doesn’t add any extra functionality. It only provides an interface to an existing subsystem.&lt;br /&gt;
&lt;br /&gt;
*The components of a subsystem do not have any knowledge about the facade whereas the colleagues have the knowledge of the mediator.&lt;br /&gt;
&lt;br /&gt;
So if we see the above examples we can clearly understand that mediator pattern avoids coupling within a sub-system whereas facade pattern avoids coupling between sub-system and between sub-system and an external entity.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
In an observer pattern an object maintains a list of all its dependents. Thus if any state changes occurs the object notifies all the dependents. The state of an object is an important concern. Whenever state change occurs the objects are updated automatically. All the common components are maintained in an abstraction called ‘subject’ whereas all the variable components are maintained in an abstraction called ‘object’. It is also known as publish-subscribe pattern.  One example of observer pattern can be an event management process. Whenever an event takes place it should be notified to all other objects so that their state gets updated.&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
&lt;br /&gt;
[[File:image3_observer.png|650 px|thumb|right|Example: Using Observer Pattern]]&lt;br /&gt;
&lt;br /&gt;
In this case there is one subject and three dependent objects. The Hospital Management Subject implements the Subject interface. The three dependents/observers are Radiology, Physiology, and Surgery and they implement the Observer interface. The Hospital Management Subject includes a method 'getBloodCount()' which sets the value of blood count variable.&lt;br /&gt;
&lt;br /&gt;
Whenever the value of this variable for a particular patient changes the observers get notified of the change because they are registered with the subject. The 'notify()' method calls the 'update()' method on the observer which sets the value in the observer objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Comparison of mediator pattern with observer pattern==&lt;br /&gt;
*The mediator pattern differs from observer pattern in terms of usage. In order to avoid explicit communication between two or more objects mediator is used. Whereas observer pattern is used in situations where an object changes its state all its dependents are notified and updated automatically to reflect this change.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1994709 Head First Design Patterns]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Mediator_pattern Mediator Pattern]&lt;br /&gt;
*[http://www.blackwasp.co.uk/Mediator.aspx Mediator Design Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Facade_pattern Facade Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Observer_pattern Observer Pattern]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=69963</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=69963"/>
		<updated>2012-11-17T19:25:09Z</updated>

		<summary type="html">&lt;p&gt;Annice: &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;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w22_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w-1w65_am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w59_bc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w60_ns]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w69_as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w39_ka]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w36_av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w37_ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w43_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w53_iv]]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68190</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68190"/>
		<updated>2012-10-25T18:29:01Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Steps */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, provides examples using TDD, and compares the pros and cons of TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
&lt;br /&gt;
There are two main motivations for TDD.&lt;br /&gt;
&lt;br /&gt;
[[File:CostofChange.png|550px|Thumb:Figure 1: Traditional &amp;quot;Cost of Change&amp;quot; curve with the waterfall model superimposed]]&lt;br /&gt;
&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
&lt;br /&gt;
=Principles=&lt;br /&gt;
&lt;br /&gt;
TDD follows several principles:&lt;br /&gt;
&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
&lt;br /&gt;
=Steps=&lt;br /&gt;
[[File:TDD_Steps.png|550px|Thumb:Figure 1: Traditional &amp;quot;Cost of Change&amp;quot; curve with the waterfall model superimposed]]&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Example==&lt;br /&gt;
The following example is take from &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck.&lt;br /&gt;
&lt;br /&gt;
Using the TDD process the author created an object with the following structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dollar {&lt;br /&gt;
 &lt;br /&gt;
  Dollar(int amount) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void times(int mulitplier) {&lt;br /&gt;
    amount *= multiplier;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What's wrong with the code above? Look at the following test:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void testMultiplication() {&lt;br /&gt;
  &lt;br /&gt;
  Dollar five = new Dollar(5);&lt;br /&gt;
  five.times(2);&lt;br /&gt;
  assertEquals(10, five.amount);&lt;br /&gt;
  five.times(3);&lt;br /&gt;
  assertEquals(15, five.amount);&lt;br /&gt;
&lt;br /&gt;
}  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever we perform an operation on a &amp;lt;code&amp;gt;Dollar&amp;lt;/code&amp;gt; the value of the &amp;lt;code&amp;gt;Dollar&amp;lt;/code&amp;gt; changes. In the test above &amp;lt;code&amp;gt;five&amp;lt;/code&amp;gt; isn't 5 after we multiply it by 2, and thus the name &amp;lt;code&amp;gt;five&amp;lt;/code&amp;gt; doesn't make sense anymore. How do we change this? &lt;br /&gt;
&lt;br /&gt;
===Red===&lt;br /&gt;
&lt;br /&gt;
First, we change the &amp;lt;b&amp;gt;test&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void testMultiplication() {&lt;br /&gt;
  Dollar five = new Dollar(5);&lt;br /&gt;
  Dollar product = five.times(2);&lt;br /&gt;
  assertEquals(10, product.amount);&lt;br /&gt;
  product = five.times(3);&lt;br /&gt;
  assertEquals(15, product.amount);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new test won't compile until we change the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Dollar times(int multiplier) {&lt;br /&gt;
  amount *= multiplier;&lt;br /&gt;
  return null;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now the test compiles, but fails. Once again, we change the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Dollar times(int multiplier) {&lt;br /&gt;
  return new Dollar(amount * multiplier);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
&lt;br /&gt;
The test succeeds. So we see that even when we are refactoring we change the test first, then the code. This way we know our refactoring is an improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
&lt;br /&gt;
Using TDD has many benefits.&lt;br /&gt;
&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
&lt;br /&gt;
TDD has a few shortcomings:&lt;br /&gt;
&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
TDD fundamentaly changes how a developer approaches a programming project. It forces him to focus on the design requirements first by mandating that the developer must write a test for anything before he writes code to do it. It has many benefits including ensuring that the project is well tested and, in some cases, reducing development time. However, it is limited in that it can't test user interfaces or databases directly. TDD is a component of [http://en.wikipedia.org/wiki/Extreme_programming Extreme Programming] and often used in conjunction with integration tests. Overall, it is a very effective tool for quickly writing well tested code.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68189</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68189"/>
		<updated>2012-10-25T18:28:06Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Steps */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, provides examples using TDD, and compares the pros and cons of TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
&lt;br /&gt;
There are two main motivations for TDD.&lt;br /&gt;
&lt;br /&gt;
[[File:CostofChange.png|550px|Thumb:Figure 1: Traditional &amp;quot;Cost of Change&amp;quot; curve with the waterfall model superimposed]]&lt;br /&gt;
&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
&lt;br /&gt;
=Principles=&lt;br /&gt;
&lt;br /&gt;
TDD follows several principles:&lt;br /&gt;
&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
[[File:TDD_Steps.png]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Example==&lt;br /&gt;
The following example is take from &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck.&lt;br /&gt;
&lt;br /&gt;
Using the TDD process the author created an object with the following structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dollar {&lt;br /&gt;
 &lt;br /&gt;
  Dollar(int amount) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void times(int mulitplier) {&lt;br /&gt;
    amount *= multiplier;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What's wrong with the code above? Look at the following test:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void testMultiplication() {&lt;br /&gt;
  &lt;br /&gt;
  Dollar five = new Dollar(5);&lt;br /&gt;
  five.times(2);&lt;br /&gt;
  assertEquals(10, five.amount);&lt;br /&gt;
  five.times(3);&lt;br /&gt;
  assertEquals(15, five.amount);&lt;br /&gt;
&lt;br /&gt;
}  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever we perform an operation on a &amp;lt;code&amp;gt;Dollar&amp;lt;/code&amp;gt; the value of the &amp;lt;code&amp;gt;Dollar&amp;lt;/code&amp;gt; changes. In the test above &amp;lt;code&amp;gt;five&amp;lt;/code&amp;gt; isn't 5 after we multiply it by 2, and thus the name &amp;lt;code&amp;gt;five&amp;lt;/code&amp;gt; doesn't make sense anymore. How do we change this? &lt;br /&gt;
&lt;br /&gt;
===Red===&lt;br /&gt;
&lt;br /&gt;
First, we change the &amp;lt;b&amp;gt;test&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void testMultiplication() {&lt;br /&gt;
  Dollar five = new Dollar(5);&lt;br /&gt;
  Dollar product = five.times(2);&lt;br /&gt;
  assertEquals(10, product.amount);&lt;br /&gt;
  product = five.times(3);&lt;br /&gt;
  assertEquals(15, product.amount);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new test won't compile until we change the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Dollar times(int multiplier) {&lt;br /&gt;
  amount *= multiplier;&lt;br /&gt;
  return null;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now the test compiles, but fails. Once again, we change the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Dollar times(int multiplier) {&lt;br /&gt;
  return new Dollar(amount * multiplier);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
&lt;br /&gt;
The test succeeds. So we see that even when we are refactoring we change the test first, then the code. This way we know our refactoring is an improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
&lt;br /&gt;
Using TDD has many benefits.&lt;br /&gt;
&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
&lt;br /&gt;
TDD has a few shortcomings:&lt;br /&gt;
&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
TDD fundamentaly changes how a developer approaches a programming project. It forces him to focus on the design requirements first by mandating that the developer must write a test for anything before he writes code to do it. It has many benefits including ensuring that the project is well tested and, in some cases, reducing development time. However, it is limited in that it can't test user interfaces or databases directly. TDD is a component of [http://en.wikipedia.org/wiki/Extreme_programming Extreme Programming] and often used in conjunction with integration tests. Overall, it is a very effective tool for quickly writing well tested code.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:TDD_Steps.png&amp;diff=68188</id>
		<title>File:TDD Steps.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:TDD_Steps.png&amp;diff=68188"/>
		<updated>2012-10-25T18:27:31Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68187</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68187"/>
		<updated>2012-10-25T18:26:56Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Motivation for TDD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, provides examples using TDD, and compares the pros and cons of TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
&lt;br /&gt;
There are two main motivations for TDD.&lt;br /&gt;
&lt;br /&gt;
[[File:CostofChange.png|550px|Thumb:Figure 1: Traditional &amp;quot;Cost of Change&amp;quot; curve with the waterfall model superimposed]]&lt;br /&gt;
&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
&lt;br /&gt;
=Principles=&lt;br /&gt;
&lt;br /&gt;
TDD follows several principles:&lt;br /&gt;
&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
[[Image:PureFab.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Example==&lt;br /&gt;
The following example is take from &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck.&lt;br /&gt;
&lt;br /&gt;
Using the TDD process the author created an object with the following structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dollar {&lt;br /&gt;
 &lt;br /&gt;
  Dollar(int amount) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void times(int mulitplier) {&lt;br /&gt;
    amount *= multiplier;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What's wrong with the code above? Look at the following test:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void testMultiplication() {&lt;br /&gt;
  &lt;br /&gt;
  Dollar five = new Dollar(5);&lt;br /&gt;
  five.times(2);&lt;br /&gt;
  assertEquals(10, five.amount);&lt;br /&gt;
  five.times(3);&lt;br /&gt;
  assertEquals(15, five.amount);&lt;br /&gt;
&lt;br /&gt;
}  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever we perform an operation on a &amp;lt;code&amp;gt;Dollar&amp;lt;/code&amp;gt; the value of the &amp;lt;code&amp;gt;Dollar&amp;lt;/code&amp;gt; changes. In the test above &amp;lt;code&amp;gt;five&amp;lt;/code&amp;gt; isn't 5 after we multiply it by 2, and thus the name &amp;lt;code&amp;gt;five&amp;lt;/code&amp;gt; doesn't make sense anymore. How do we change this? &lt;br /&gt;
&lt;br /&gt;
===Red===&lt;br /&gt;
&lt;br /&gt;
First, we change the &amp;lt;b&amp;gt;test&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void testMultiplication() {&lt;br /&gt;
  Dollar five = new Dollar(5);&lt;br /&gt;
  Dollar product = five.times(2);&lt;br /&gt;
  assertEquals(10, product.amount);&lt;br /&gt;
  product = five.times(3);&lt;br /&gt;
  assertEquals(15, product.amount);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new test won't compile until we change the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Dollar times(int multiplier) {&lt;br /&gt;
  amount *= multiplier;&lt;br /&gt;
  return null;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now the test compiles, but fails. Once again, we change the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Dollar times(int multiplier) {&lt;br /&gt;
  return new Dollar(amount * multiplier);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
&lt;br /&gt;
The test succeeds. So we see that even when we are refactoring we change the test first, then the code. This way we know our refactoring is an improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
&lt;br /&gt;
Using TDD has many benefits.&lt;br /&gt;
&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
&lt;br /&gt;
TDD has a few shortcomings:&lt;br /&gt;
&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
TDD fundamentaly changes how a developer approaches a programming project. It forces him to focus on the design requirements first by mandating that the developer must write a test for anything before he writes code to do it. It has many benefits including ensuring that the project is well tested and, in some cases, reducing development time. However, it is limited in that it can't test user interfaces or databases directly. TDD is a component of [http://en.wikipedia.org/wiki/Extreme_programming Extreme Programming] and often used in conjunction with integration tests. Overall, it is a very effective tool for quickly writing well tested code.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68186</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68186"/>
		<updated>2012-10-25T18:26:37Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Motivation for TDD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, provides examples using TDD, and compares the pros and cons of TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
&lt;br /&gt;
There are two main motivations for TDD.&lt;br /&gt;
&lt;br /&gt;
[[File:CostofChange.png|350px|Thumb:Figure 1: Traditional &amp;quot;Cost of Change&amp;quot; curve with the waterfall model superimposed]]&lt;br /&gt;
&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
&lt;br /&gt;
=Principles=&lt;br /&gt;
&lt;br /&gt;
TDD follows several principles:&lt;br /&gt;
&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
[[Image:PureFab.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Example==&lt;br /&gt;
The following example is take from &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck.&lt;br /&gt;
&lt;br /&gt;
Using the TDD process the author created an object with the following structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Dollar {&lt;br /&gt;
 &lt;br /&gt;
  Dollar(int amount) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void times(int mulitplier) {&lt;br /&gt;
    amount *= multiplier;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What's wrong with the code above? Look at the following test:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void testMultiplication() {&lt;br /&gt;
  &lt;br /&gt;
  Dollar five = new Dollar(5);&lt;br /&gt;
  five.times(2);&lt;br /&gt;
  assertEquals(10, five.amount);&lt;br /&gt;
  five.times(3);&lt;br /&gt;
  assertEquals(15, five.amount);&lt;br /&gt;
&lt;br /&gt;
}  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever we perform an operation on a &amp;lt;code&amp;gt;Dollar&amp;lt;/code&amp;gt; the value of the &amp;lt;code&amp;gt;Dollar&amp;lt;/code&amp;gt; changes. In the test above &amp;lt;code&amp;gt;five&amp;lt;/code&amp;gt; isn't 5 after we multiply it by 2, and thus the name &amp;lt;code&amp;gt;five&amp;lt;/code&amp;gt; doesn't make sense anymore. How do we change this? &lt;br /&gt;
&lt;br /&gt;
===Red===&lt;br /&gt;
&lt;br /&gt;
First, we change the &amp;lt;b&amp;gt;test&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void testMultiplication() {&lt;br /&gt;
  Dollar five = new Dollar(5);&lt;br /&gt;
  Dollar product = five.times(2);&lt;br /&gt;
  assertEquals(10, product.amount);&lt;br /&gt;
  product = five.times(3);&lt;br /&gt;
  assertEquals(15, product.amount);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The new test won't compile until we change the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Dollar times(int multiplier) {&lt;br /&gt;
  amount *= multiplier;&lt;br /&gt;
  return null;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now the test compiles, but fails. Once again, we change the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Dollar times(int multiplier) {&lt;br /&gt;
  return new Dollar(amount * multiplier);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
&lt;br /&gt;
The test succeeds. So we see that even when we are refactoring we change the test first, then the code. This way we know our refactoring is an improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
&lt;br /&gt;
Using TDD has many benefits.&lt;br /&gt;
&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
&lt;br /&gt;
TDD has a few shortcomings:&lt;br /&gt;
&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
TDD fundamentaly changes how a developer approaches a programming project. It forces him to focus on the design requirements first by mandating that the developer must write a test for anything before he writes code to do it. It has many benefits including ensuring that the project is well tested and, in some cases, reducing development time. However, it is limited in that it can't test user interfaces or databases directly. TDD is a component of [http://en.wikipedia.org/wiki/Extreme_programming Extreme Programming] and often used in conjunction with integration tests. Overall, it is a very effective tool for quickly writing well tested code.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:CostofChange.png&amp;diff=68185</id>
		<title>File:CostofChange.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:CostofChange.png&amp;diff=68185"/>
		<updated>2012-10-25T18:25:55Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68134</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68134"/>
		<updated>2012-10-24T22:09:46Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Motivation for TDD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
[[File:TimevsCostDev.png|350px|Thumb:Figure 1: Traditional &amp;quot;Cost of Change&amp;quot; curve with the waterfall model superimposed]]&lt;br /&gt;
&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
[[Image:PureFab.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68133</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68133"/>
		<updated>2012-10-24T22:09:06Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Motivation for TDD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
[[File:TimevsCostDev.png|350px|Figure 1: Traditional &amp;quot;Cost of Change&amp;quot; curve with the waterfall model superimposed]]&lt;br /&gt;
s&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
[[Image:PureFab.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68072</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68072"/>
		<updated>2012-10-23T21:15:08Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Motivation for TDD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
[[File:TimevsCostDev.png|350px|Figure 1: Traditional &amp;quot;Cost of Change&amp;quot; curve with the waterfall model superimposed]]&lt;br /&gt;
The following sequence is based on the book ''[[Test-Driven Development by Example]]''.&amp;lt;ref name=Beck&amp;gt;Beck, K. Test-Driven Development by Example, Addison Wesley - Vaseem, 2003&amp;lt;/ref&amp;gt;]]&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
[[Image:PureFab.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68071</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68071"/>
		<updated>2012-10-23T21:13:26Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Motivation for TDD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
[[File:TimevsCostDev.png|350px|thumb|right|A graphical representation of the development cycle, using a basic [[flowchart]]]]&lt;br /&gt;
The following sequence is based on the book ''[[Test-Driven Development by Example]]''.&amp;lt;ref name=Beck&amp;gt;Beck, K. Test-Driven Development by Example, Addison Wesley - Vaseem, 2003&amp;lt;/ref&amp;gt;]]&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
[[Image:PureFab.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68070</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68070"/>
		<updated>2012-10-23T21:11:35Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Motivation for TDD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
[[File:TimevsCostDev.png]]&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
[[Image:PureFab.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:TimevsCostDev.png&amp;diff=68069</id>
		<title>File:TimevsCostDev.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:TimevsCostDev.png&amp;diff=68069"/>
		<updated>2012-10-23T21:10:04Z</updated>

		<summary type="html">&lt;p&gt;Annice: Time Vs Cost of change involved in development cycle&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Time Vs Cost of change involved in development cycle&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68068</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68068"/>
		<updated>2012-10-23T21:01:44Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Steps */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
[[Image:PureFab.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68058</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68058"/>
		<updated>2012-10-23T20:21:28Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*[http://www.codeproject.com/Articles/47747/Test-Driven-Development-TDD-Step-by-Step-Part-1-In TDD Step by Step Code Project]&lt;br /&gt;
*[http://integralpath.blogs.com/thinkingoutloud/2005/09/principles_of_t.html Principles of Testing]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68057</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68057"/>
		<updated>2012-10-23T20:20:05Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*[http://www.martinfowler.com/bliki/TestDrivenDevelopment.html www.martinfowler.com/bliki/TestDrivenDevelopment]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68056</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68056"/>
		<updated>2012-10-23T20:18:53Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68055</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68055"/>
		<updated>2012-10-23T20:18:37Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68054</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68054"/>
		<updated>2012-10-23T20:16:22Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[http://www.methodsandtools.com/archive/archive.php?id=20. Methods and Tools Archives]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68053</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68053"/>
		<updated>2012-10-23T20:15:28Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*[www.methodsandtools.com/archive/archive.php?id=20 Methods and Tools Archives]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68052</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68052"/>
		<updated>2012-10-23T20:13:46Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Benefits of TDD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits Of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68051</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68051"/>
		<updated>2012-10-23T20:13:33Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Shortcomings of TDD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings Of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68050</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68050"/>
		<updated>2012-10-23T20:13:15Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Characteristics of a Good Unit Test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics Of A Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68049</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68049"/>
		<updated>2012-10-23T20:12:56Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Characteristics of a good unit test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics of a Good Unit Test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
=Benefits of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68048</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68048"/>
		<updated>2012-10-23T20:12:16Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics of a good unit test=&lt;br /&gt;
A good unit test has the following characteristics.&lt;br /&gt;
*It is important that the test executes fast. If the tests are slow, they will not be run often.&lt;br /&gt;
*Environmental dependencies such as databases, file systems, networks, queues, and so on which will slow down the tests are either separated or simulated. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is.&lt;br /&gt;
*The scope of testing is very limited. If the test fails, it's obvious where to look for the problem. It's important to only test one aspect in a single test.&lt;br /&gt;
*The test should run and pass in isolation (on any machine). If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests.&lt;br /&gt;
*Such a test often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.&lt;br /&gt;
*The intention of testing is clearly revealed. Another developer can look at the test and understand what is expected of the production code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Benefits of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68047</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68047"/>
		<updated>2012-10-23T20:09:14Z</updated>

		<summary type="html">&lt;p&gt;Annice: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Benefits of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Shortcomings of TDD=&lt;br /&gt;
*The actual database or external file is never tested directly by TDD (only production code can test it)&lt;br /&gt;
*If not used carefully, it can add to the total project costs and complexity of the project&lt;br /&gt;
*TDD is unable to test UI.&lt;br /&gt;
*TDD is highly reliant on Refactoring and Programmer Skills.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68045</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68045"/>
		<updated>2012-10-23T20:08:04Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Characteristics of a good unit test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Benefits of TDD=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68044</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w11 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w11_aa&amp;diff=68044"/>
		<updated>2012-10-23T20:07:00Z</updated>

		<summary type="html">&lt;p&gt;Annice: /* Characteristics of a good unit test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.&lt;br /&gt;
&lt;br /&gt;
=Motivation for TDD=&lt;br /&gt;
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.&lt;br /&gt;
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements&lt;br /&gt;
=Principles=&lt;br /&gt;
*Tests serve as examples of how to use a class or method.  Once used to having tests that show how things work (and that they work), developers start inquiring if a test was already there for the piece of code to be developed&lt;br /&gt;
*Developer tests are distinctly different from QA (Quality Analysis) tests and should be kept separate.  QA tests target features and treat the system as a black box.  Unit tests created by the developer operate at a lower level and test different things.&lt;br /&gt;
*Name the tests carefully.  For Example, name test packages like the package being tested with a suffix.  For example, the &amp;quot;DataAccess&amp;quot; project/package is tested by &amp;quot;DataAccess.Test&amp;quot;. Also,  name test classes the same as the class under test with the suffix &amp;quot;Test&amp;quot;.  For example, the class &amp;quot;PrintManager&amp;quot; is tested by the test class &amp;quot;PrintManagerTest&amp;quot;.  This convention makes it easy to find the related class and keeps the class name a noun. You should also name test methods the same as the method being tested with the prefix &amp;quot;Test&amp;quot;.  For example, the method &amp;quot;PrintProductOrder()&amp;quot; is tested by the method &amp;quot;TestPrintProductOrder()&amp;quot;.  This convention keeps the method name a verb that reads as an english phrase.  &lt;br /&gt;
*Write each test before writing the method under test. It encourages the developer to think as a user of the target method before thinking about implementation, which usually results in a cleaner, easier-to-use interface.&lt;br /&gt;
*Follow the &amp;quot;3-As&amp;quot; pattern for test methods: Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As.  Arrange is variable declaration and initialization.  Act is invoking the code under test.  Assert is using the Assert.* methods to verify that expectations were met. &lt;br /&gt;
*When writing application code, only write enough code to make a test work. This technique prevents gold-plating and ensures that you always have a test for the code you write. &lt;br /&gt;
*When you find you need to refractor working code, refractor and re-test prior to writing new code.  This technique ensures your refractoring is correct prior to adding new functionality and applies to creating new methods, introducing inheritance, everything.&lt;br /&gt;
=Steps=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Follow these steps:&lt;br /&gt;
*Understand the requirements of the story, work item, or feature that you are working on.&lt;br /&gt;
*Red:Create a test and make it fail.&lt;br /&gt;
Imagine how the new code should be called and write the test as if the code already existed.&lt;br /&gt;
Create the new production code stub. Write just enough code so that it compiles.&lt;br /&gt;
Run the test. It should fail. This is a calibration measure to ensure that your test is calling the correct code and that the code is not working by accident. This is a meaningful failure, and you expect it to fail.&lt;br /&gt;
*Green: Make the test pass by any means necessary.&lt;br /&gt;
Write the production code to make the test pass. Keep it simple.&lt;br /&gt;
Some advocate the hard-coding of the expected return value first to verify that the test correctly detects success. This varies from practitioner to practitioner.If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. If new functionality is still needed, then another test is needed. Make this one test pass and continue.&lt;br /&gt;
When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.&lt;br /&gt;
*Refractor: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.&lt;br /&gt;
Remove duplication caused by the addition of the new functionality.&lt;br /&gt;
Make design changes to improve the overall solution.&lt;br /&gt;
After each refactoring, rerun all the tests to ensure that they all still pass.&lt;br /&gt;
*Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/ Refractor cycles.&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
==Homework Grades Program==&lt;br /&gt;
&lt;br /&gt;
===Setup===&lt;br /&gt;
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: &amp;lt;b&amp;gt; write a test &amp;lt;/b&amp;gt;. Let's test an average function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:&lt;br /&gt;
&lt;br /&gt;
* class &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* &amp;lt;code&amp;gt;Homework&amp;lt;/code&amp;gt; constructor not declared&lt;br /&gt;
* field &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
* method &amp;lt;code&amp;gt;average&amp;lt;/code&amp;gt; not declared&lt;br /&gt;
&lt;br /&gt;
Now, we fix the first error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Homework(void) {&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Third error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int * grades;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fourth error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int average(int * grades) {&lt;br /&gt;
  return 0; // default return value&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, the test compiles! The code now looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
&lt;br /&gt;
    return 0; // default return value&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;
===Red===&lt;br /&gt;
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array &amp;lt;code&amp;gt;grades&amp;lt;/code&amp;gt;. So, we add to the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Homework {&lt;br /&gt;
&lt;br /&gt;
  int * grades;&lt;br /&gt;
  int numGrades;                         // new&lt;br /&gt;
&lt;br /&gt;
  Homework(void) {&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int average(int * grades) {&lt;br /&gt;
 &lt;br /&gt;
   int avg = 0;                         // new&lt;br /&gt;
 &lt;br /&gt;
   for(int i = 0; i &amp;lt; numGrades; i++) { // new&lt;br /&gt;
     avg += grades[i];                  // new&lt;br /&gt;
   }                                    // new&lt;br /&gt;
 &lt;br /&gt;
   return avg/numGrades;                // new&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;
Of course, we must remember to change the test to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  myHomework = new Homework();&lt;br /&gt;
  myHomework.grades = [100, 50];&lt;br /&gt;
  myHomework.numGrades = 2;&lt;br /&gt;
  assert(myHomework.average(myHomework.grades) == 75);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Green===&lt;br /&gt;
Success! We have a green bar when we run it. &lt;br /&gt;
&lt;br /&gt;
===Refactor===&lt;br /&gt;
The last step is refactoring. Perhaps we don't want a grade to be an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;? Should it be an &amp;lt;code&amp;gt;unsigned int&amp;lt;/code&amp;gt;? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.&lt;br /&gt;
&lt;br /&gt;
==More Examples==&lt;br /&gt;
See &amp;lt;i&amp;gt;Test-Driven Development by Example&amp;lt;/i&amp;gt; by Kent Beck for more examples.&lt;br /&gt;
&lt;br /&gt;
=Characteristics of a good unit test=&lt;br /&gt;
*Automated testing: Automated system makes the testing much more easier and faster. Manual testing is slower and needs human interaction which is error-prone. Automated testing makes sure any particular test is not missing every time we test the system, whereas humans may leave a test out mistakenly during testing process.&lt;br /&gt;
*Better to test New/Modified Functionality: With manual testing whenever you add new functionality or modify existing functionality, QA personal needs to test all systems that may be affected due to the modification which may be time-consuming. This may also leave a hidden bug in the system after modification. With TDD whenever a new functionality is added or existing functionality is modified, the whole set of tests in the system is run to make sure existing tests are passed. This makes sure that existing functionality is not broken.&lt;br /&gt;
*Developer’s confidence: With TDD, developers are more safe to change the system as any inappropriate changes will make some tests to fail. With non-TDD system developers needs to take more care to change existing system as the new change may fail other functionality. But with TDD developers can do so easily as after modification if developer runs the test he/she will find immediately that the change has failed some other tests (i.e. some other part of the system).&lt;br /&gt;
*Manual testing stage is shortened: In non-TDD development, as soon as developers prepare a build, QA personal starts testing. This manual testing takes a reasonable amount of time and increases the time to deliver the product from the build time. With TDD, a major part of the system is tested during development and needs lesser QA involvement in the time between build and final delivery of the product.&lt;br /&gt;
*Alternative Documentation: Unit tests are kind of documentation to system. Each unit test tells about an individual requirement to the module or system. For example, the following test ensures that only a logged in user with proper balance can buy an item when the item exists in stock. This test reflects a user aspect scenario of the system.&lt;br /&gt;
&lt;br /&gt;
     public void CheckUserCanNotPurchaseItemWithoutBalance(){  &lt;br /&gt;
     LoginToTheSystem();       &lt;br /&gt;
     SelectAnItemToBuy();       &lt;br /&gt;
     MakeSureItemExsitsInStock();       &lt;br /&gt;
     MakeSureUserHasBalanceToBuy();      &lt;br /&gt;
     RunTheTransaction(); &lt;br /&gt;
  }			&lt;br /&gt;
*Better way to fix bugs: In TDD approach when QA finds a bug, developer first writes a test that generates the bug (that is the test will fail due to this bug). Then developer modifies code to make sure that the test is passed (i.e., the bug is fixed). This approach makes sure that next time the bug will not appear in the system as a test is written in the system to take care of the bug.&lt;br /&gt;
*Repetition of the same bug reduced: With TDD once a bug is found, it's put under test. So every time you run the whole test in the system, the tests associated with the bug are run and make sure the bugs are not generated again.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Danielle&lt;br /&gt;
=References=&lt;br /&gt;
*[http://catalog.lib.ncsu.edu/record/NCSU1601963 Beck, K. (2002). Test-driven development by example. Addison-Wesley.]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Test_driven_development (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development]&lt;br /&gt;
*[http://c2.com/cgi/wiki?TestDrivenDevelopment (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment]&lt;br /&gt;
*[http://www.agiledata.org/essays/tdd.html Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html]&lt;br /&gt;
*http://www.testdriven.com/  ?&lt;/div&gt;</summary>
		<author><name>Annice</name></author>
	</entry>
</feed>