<?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=Sbhatta9</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=Sbhatta9"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Sbhatta9"/>
	<updated>2026-05-15T11:22:25Z</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_2w37_ms&amp;diff=70168</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w37 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w37_ms&amp;diff=70168"/>
		<updated>2012-11-18T16:23:44Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: /* Adapter Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Adapter pattern and the related patterns (Bridge, Decorator, Facade)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The adapter design pattern allows the user to make changes to the existing class with other class libraries without changing the code for the existing class. The Bridge, Decorator and the Facade pattern look somewhat similar to the adapter pattern but their intent is different and that intent is what separates the above patterns from each other.&lt;br /&gt;
&lt;br /&gt;
=== Adapter Pattern ===&lt;br /&gt;
&lt;br /&gt;
Adapter pattern plays an important role when you want to incompatible interfaces to work together. It sis generally used for third party code or code that we cannot modify. If such code uses an interface which is different from the interface we wish to use, adapter is the answer.The real world example for an adapter pattern is the travel power adapter.Different countries have different socket and plug configuration. So you can use an adapter to fit a plug into a socket that initially was not possible due to different interface designs.&lt;br /&gt;
&lt;br /&gt;
==== Implementattion ====&lt;br /&gt;
&lt;br /&gt;
===== Adapter implementation using inheritance =====&lt;br /&gt;
&lt;br /&gt;
This method can be used when you have incompatible method that needs to be used in other class. Using inheritance a &amp;quot;is-a&amp;quot; relationship is established between the base class and super class. The new compatible methods will be contained in the inherited adapter class.&lt;br /&gt;
&lt;br /&gt;
 public class CylindricalSocket {&lt;br /&gt;
   public String supply(String cylinStem1, String cylinStem1) {&lt;br /&gt;
     System.out.println(&amp;quot;Power power power...&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularAdapter extends CylindricalSocket {&lt;br /&gt;
   public String adapt(String rectaStem1, Sting rectaStem2) {&lt;br /&gt;
     //some conversion logic&lt;br /&gt;
     String cylinStem1 = rectaStem1;&lt;br /&gt;
     String cylinStem2 = rectaStem2;&lt;br /&gt;
     return supply(cylinStem1, cylinStem2);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularPlug {&lt;br /&gt;
   private String rectaStem1;&lt;br /&gt;
   private String rectaStem2;&lt;br /&gt;
   public getPower() {&lt;br /&gt;
     RectangulrAdapter adapter = new RectangulrAdapter();&lt;br /&gt;
     String power = adapter.adapt(rectaStem1, rectaStem2);&lt;br /&gt;
     System.out.println(power);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===== Adapter implementation using composition =====&lt;br /&gt;
&lt;br /&gt;
The other approach is to have a base class as an attribute in the adapter class. This creates an &amp;quot;has-a&amp;quot;relationship between the base class and the sub class.&lt;br /&gt;
&lt;br /&gt;
 public class CylindricalSocket {&lt;br /&gt;
   public String supply(String cylinStem1, String cylinStem1) {&lt;br /&gt;
     System.out.println(&amp;quot;Power power power...&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularAdapter {&lt;br /&gt;
   private CylindricalSocket socket;&lt;br /&gt;
 &lt;br /&gt;
   public String adapt(String rectaStem1, Sting rectaStem2) {&lt;br /&gt;
     //some conversion logic&lt;br /&gt;
     socket = new CylindricalSocket();&lt;br /&gt;
     String cylinStem1 = rectaStem1;&lt;br /&gt;
     String cylinStem2 = rectaStem2;&lt;br /&gt;
     return socket.supply(cylinStem1, cylinStem2);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularPlug {&lt;br /&gt;
   private String rectaStem1;&lt;br /&gt;
   private String rectaStem2;&lt;br /&gt;
   public getPower() {&lt;br /&gt;
     RectangulrAdapter adapter = new RectangulrAdapter();&lt;br /&gt;
     String power = adapter.adapt(rectaStem1, rectaStem2);&lt;br /&gt;
     System.out.println(power);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===== Composition or inheritance to implement Adapter Pattern? =====&lt;br /&gt;
&lt;br /&gt;
* Composition is preferred over inheritance since using composition it is easy to change the behavior of a class.&lt;br /&gt;
&lt;br /&gt;
===== Adapter pattern in Java Library=====&lt;br /&gt;
* java.io.InputStreamReader(InputStream)&lt;br /&gt;
* java.io.OutputStreamWriter(OutputStream)&lt;br /&gt;
&lt;br /&gt;
=== Bridge Pattern ===&lt;br /&gt;
As stated by GoF a bridge design patterns intent is to “Decouple an abstraction from its implementation so that the two can vary independently”.&lt;br /&gt;
&lt;br /&gt;
===== Elements of Bridge Design Pattern =====&lt;br /&gt;
* Abstraction: This is where the abstraction interface is defined.&lt;br /&gt;
&lt;br /&gt;
For example: A  Vehicle class which has a manufacture method.&lt;br /&gt;
 &lt;br /&gt;
* Refined Abstraction: Refined Abstraction extends the interface defined by Abstraction.&lt;br /&gt;
&lt;br /&gt;
For example, a Car class or a Bike class which has a manufacture method which is more refined than the Vehicle class.&lt;br /&gt;
 &lt;br /&gt;
* Implementor: It defines the interface for the implementation classes. It defines the basic operation.&lt;br /&gt;
&lt;br /&gt;
For example, the Workshop class acts as an implementor with the work() method.&lt;br /&gt;
&lt;br /&gt;
* Concrete Implementation: This implements the Implementor interface and gives it a more concrete implementation.&lt;br /&gt;
 &lt;br /&gt;
For example the Produce and the Assemble class with the work() method provides the concrete implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Bridge-Pattern.jpg|350 px|thumb|right|UML Diagram for Bridge-Pattern]]&lt;br /&gt;
[[File:WBP.jpg|350 px|thumb|left|Without Bridge-Pattern]][[File:bp.jpg|350 px|thumb|center|With Bridge-Pattern]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Example code for Bridge pattern =====&lt;br /&gt;
Vehicle Interface&lt;br /&gt;
&lt;br /&gt;
 // abstraction in bridge pattern&lt;br /&gt;
 abstract class Vehicle {&lt;br /&gt;
   protected Workshop workShop1;&lt;br /&gt;
   protected Workshop workShop2;&lt;br /&gt;
 &lt;br /&gt;
   protected Vehicle(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     this.workShop1 = workShop1;&lt;br /&gt;
     this.workShop2 = workShop2;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   abstract public void manufacture();&lt;br /&gt;
 }&lt;br /&gt;
Car class &lt;br /&gt;
&lt;br /&gt;
 //Refine abstraction 1 in bridge pattern&lt;br /&gt;
 public class Car extends Vehicle {&lt;br /&gt;
 &lt;br /&gt;
   public Car(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     super(workShop1, workShop2);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void manufacture() {&lt;br /&gt;
     System.out.print(&amp;quot;Car &amp;quot;);&lt;br /&gt;
     workShop1.work();&lt;br /&gt;
     workShop2.work();&lt;br /&gt;
 &lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Bike Class &lt;br /&gt;
&lt;br /&gt;
 // Refine abstraction 2 in bridge pattern&lt;br /&gt;
 public class Bike extends Vehicle {&lt;br /&gt;
 &lt;br /&gt;
   public Bike(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     super(workShop1, workShop2);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void manufacture() {&lt;br /&gt;
     System.out.print(&amp;quot;Bike &amp;quot;);&lt;br /&gt;
     workShop1.work();&lt;br /&gt;
     workShop2.work();&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Workshop Interface &lt;br /&gt;
 &lt;br /&gt;
  // Implementor for bridge pattern&lt;br /&gt;
 public interface Workshop {&lt;br /&gt;
   abstract public void work();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Produce Class &lt;br /&gt;
&lt;br /&gt;
 // Concrete implementation 1 for bridge pattern&lt;br /&gt;
 public class Produce implements Workshop {&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void work() {&lt;br /&gt;
     System.out.print(&amp;quot;Produced&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Assemble Class&lt;br /&gt;
   //Concrete implementation 2 for bridge pattern&lt;br /&gt;
&lt;br /&gt;
 public class Assemble implements Workshop {&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void work() {&lt;br /&gt;
     System.out.println(&amp;quot; Assembled.&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Demonstration of bridge design pattern&lt;br /&gt;
&lt;br /&gt;
 public class BridgePattern {&lt;br /&gt;
 &lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
 &lt;br /&gt;
     Vehicle vehicle1 = new Car(new Produce(), new Assemble());&lt;br /&gt;
     vehicle1.manufacture();&lt;br /&gt;
     Vehicle vehicle2 = new Bike(new Produce(), new Assemble());&lt;br /&gt;
     vehicle2.manufacture();&lt;br /&gt;
 &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Output:&lt;br /&gt;
 Car Produced Assembled.&lt;br /&gt;
 Bike Produced Assembled.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Bridge vs Adapter pattern =====&lt;br /&gt;
* Two incompatable classes can be made to work together using adapter pattern.&lt;br /&gt;
* Bridge pattern creates two separate hierarchies by separating the abstraction from the implementation.&lt;br /&gt;
&lt;br /&gt;
While the two patterns may seem similar according to the GoF- &amp;quot;Adapter makes things work after they're designed; Bridge makes them work before they are. [GoF, p219]&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The adapter pattern is useful when we have code that is either third party or maybe in house, over which we do not have any control and such code do not fit with the interface that we wish to use. In such a case we use either of two (delegation or inheritance) model of the adapter pattern to change the existing classes (which do not fit our interface) and cast them to our desired interface.  &lt;br /&gt;
&lt;br /&gt;
The bridge pattern on the other hand is something we implement upfront on classes that have orthogonal hierarchies. It provides a way to decouple the interface and the implementation in such a way that you don't get an insane number of classes. In bridge pattern we usually deal with classes that vary in their implementations a lot and we want to incorporate the functionality of the implementation classes as much as possible. Bridge pattern is many times implemented with the help adapter pattern to give the orthogonal hierarchies  a common interface. &lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
We have a FileReader interface with two implementations. One that reads file from windows system and one that read Files from Linux systems. Now there are two more interface say MemoryMappedFiles and RADFiles. Now bridge pattern will help you in not having to write four classes  MemoryMappedWindowsFileReader, MemoryMappedLinuxFileReader, RADWindowsFileReader, RADWindowsFileReader. Here is how&lt;br /&gt;
&lt;br /&gt;
Use adapter pattern to adapt MemoryMappedFiles and RADFiles to a common interface (assuming both implementations have SpecialFile interface in common). Also WindowsFileReader and LinuxFileReader are two implementations of the FileReader interface.&lt;br /&gt;
&lt;br /&gt;
public interface CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   public void readFile(); // used for adapting&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SpecialWindowsFileReader extends WindowsFileReader implements CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   SpecialFile specialFile;&lt;br /&gt;
&lt;br /&gt;
   public SpecialWindowsFileReader(SpecialFile specialFile){&lt;br /&gt;
      this.specialFile = specialFile;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   public void readFile(){&lt;br /&gt;
      byte fileBytes[] = this.read(); // read platform specific and convert to bytes&lt;br /&gt;
      this.specialFile(fileBytes) // decouples from the filesystem specifics.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SpecialLinuxFileReader extends LinuxFileReader implements CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   SpecialFile specialFile;&lt;br /&gt;
&lt;br /&gt;
   public SpecialLinuxFileReader(SpecialFile specialFile){&lt;br /&gt;
      this.specialFile = specialFile;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   public void readFile(){&lt;br /&gt;
      byte fileBytes[] = this.read(); // read platform specific and convert to bytes&lt;br /&gt;
      this.specialFile(fileBytes) // decouples from the filesystem specifics.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// bridge pattern&lt;br /&gt;
&lt;br /&gt;
public class Bridge{&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
     CrossPlatformSpecialPurposeFileReader specialFiles[] = new CrossPlatformSpecialPurposeFileReader[2];&lt;br /&gt;
     specialFiles[0] = new SpecialWindowsFileReader(new MemoryMappedFiles());&lt;br /&gt;
     specialFiles[1] = new SpecialLinuxFileReader(new RADFiles());&lt;br /&gt;
     // deouples two orthogonal implementations under one interface.&lt;br /&gt;
     for(CrossPlatformSpecialPurposeFileReader reader : specialFiles){&lt;br /&gt;
            reader.readFile();     &lt;br /&gt;
     }   &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Decorator Pattern ===&lt;br /&gt;
&lt;br /&gt;
The decorator pattern adds responsibility to an object dynamically. The decorator design pattern is used to extend the behavior of an object dynamically. In order to extend the behavior we have to construct an wrapper around the object. Inheritance is not feasible since it is applied to an entire class. With the decorator pattern it is possible to select any particular instance and modify its behavior leaving the other instances unmodified.&lt;br /&gt;
&lt;br /&gt;
[[File:Decorator-Pattern.jpg|200 px|thumb|right|UML Diagram for Decorator-Pattern]]&lt;br /&gt;
==== Implementation of Decorator Pattern ====&lt;br /&gt;
First start with the interface which will be used by the class having the decoration design.&lt;br /&gt;
The example below explains how you first create a base ice cream and then decorate that ice cream by adding new toppings. The added topping changes the behavior of that ice cream. i.e. it gives the ice cream more taste.&lt;br /&gt;
&lt;br /&gt;
The interface below contains an makeIcecream() method which has not been implemented yet.&lt;br /&gt;
 public interface Icecream {&lt;br /&gt;
  public String makeIcecream();&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
The SimpleIceCream Class provides an implementation to the makeIcecream() method. This is the class which acts as a base class on which decorations will be added.&lt;br /&gt;
&lt;br /&gt;
 public class SimpleIcecream implements Icecream {&lt;br /&gt;
 &lt;br /&gt;
  @Override&lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return &amp;quot;Base Icecream&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
The class below is the crux of the design pattern. Here is where all the Decoration is actually provided to an instance of the object.It contains an attribute of type Icecream. Once the instance is assigned using the constructor the instance method will be invoked.&lt;br /&gt;
&lt;br /&gt;
 abstract class IcecreamDecorator implements Icecream {&lt;br /&gt;
 &lt;br /&gt;
  protected Icecream specialIcecream;&lt;br /&gt;
 &lt;br /&gt;
  public IcecreamDecorator(Icecream specialIcecream) {&lt;br /&gt;
    this.specialIcecream = specialIcecream;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The NuttyDecorator and HoneyDecorator are the two concrete classes that implement the abstract decorator class IcecreamDecorator.&lt;br /&gt;
&lt;br /&gt;
 public class NuttyDecorator extends IcecreamDecorator {&lt;br /&gt;
 &lt;br /&gt;
  public NuttyDecorator(Icecream specialIcecream) {&lt;br /&gt;
    super(specialIcecream);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream() + addNuts();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private String addNuts() {&lt;br /&gt;
    return &amp;quot; + cruncy nuts&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HoneyDecorator extends IcecreamDecorator {&lt;br /&gt;
 &lt;br /&gt;
  public HoneyDecorator(Icecream specialIcecream) {&lt;br /&gt;
    super(specialIcecream);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream() + addHoney();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private String addHoney() {&lt;br /&gt;
    return &amp;quot; + sweet honey&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Decorator pattern in Java Library=====&lt;br /&gt;
* java.io.BufferedReader;&lt;br /&gt;
* java.io.FileReader;&lt;br /&gt;
* java.io.Reader;&lt;br /&gt;
&lt;br /&gt;
==== Adaptor Pattern vs Decorator Pattern====&lt;br /&gt;
* Adapter provides a different interface to its subject.&lt;br /&gt;
* Decorator provides an enhanced interface.&lt;br /&gt;
&lt;br /&gt;
One important bit of difference between the two patterns is - In adapter pattern we adapt an existing interface to an interface that suits our needs. On the other hand in decorator pattern the aim is to enhance the existing interface such that the new interface can replace the existing interface without any changes in code.  &lt;br /&gt;
&lt;br /&gt;
http://sourcemaking.com/design_patterns/decorator&lt;br /&gt;
&lt;br /&gt;
=== Facade Pattern ===&lt;br /&gt;
&lt;br /&gt;
[[File:Wof.jpg|200 px|thumb|right|Without Facade-Pattern]]&lt;br /&gt;
[[File:Wf.jpg|200 px|thumb|right|With Facade-Pattern]]&lt;br /&gt;
[[File:FacadeUml.jpg|200 px|thumb|left|Microwave Design using Facade Pattern]]&lt;br /&gt;
GoF defines a facade design pattern as a pattern which , “Provides a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.”&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider a software component. In order to get the job done this component might have to expose a of interfaces so that it can interact with other components in a process flow. Facade pattern simplifies this process by providing an unified interface. It builds an additional layer of abstraction over the existing abstract layer to simplify the process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A microwave oven is made up of components like trasnformer, capacitor, magnetron, waveguide and some small other components. For a microwave to work all these different components needs to be executed in a sequence.  If all these components had separate interfaces it would have been hard and complicated.Hence  oven provides you buttons which can be considered as a facade. When you click on single button the job is done. That single button works as an abstraction layer between the user and the internal components.&lt;br /&gt;
&lt;br /&gt;
==== Facade Pattern in java library ====&lt;br /&gt;
ExternalContext Class in java performs cookie management by using an facade pattern. It makes use of components such as HttpSession, ServletContext, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse.&lt;br /&gt;
&lt;br /&gt;
==== Facade Pattern vs Adapter Pattern ====&lt;br /&gt;
* Adapter pattern is to make an incompatible class work with other class where as facade provides programming comfort by providing an unified interface.&lt;br /&gt;
* Adapter Pattern using inheritance increases inter dependencies whereas facade pattern provides loose coupling&lt;br /&gt;
* Changing data to suit the interface of a subsystem is done by facade whereas changing the structure of a system is done by adapter pattern&lt;br /&gt;
*  Facade increases subsystem independence and portability. This is not possible using the adapter pattern.&lt;br /&gt;
&lt;br /&gt;
Facade defines a new interface, whereas Adapter uses an old interface. Adapter makes two existing interfaces work together as opposed to defining an entirely new one. Adapter and Facade are both wrappers; but they are different kinds of wrappers. The intent of Facade is to produce a simpler interface, and the intent of Adapter is to design to an existing interface. While Facade routinely wraps multiple objects and Adapter wraps a single object; Facade could front-end a single complex object and Adapter could wrap several legacy objects&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w37_ms&amp;diff=70163</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w37 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w37_ms&amp;diff=70163"/>
		<updated>2012-11-18T15:27:26Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Adapter pattern and the related patterns (Bridge, Decorator, Facade)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The adapter design pattern allows the user to make changes to the existing class with other class libraries without changing the code for the existing class. The Bridge, Decorator and the Facade pattern look somewhat similar to the adapter pattern but their intent is different and that intent is what separates the above patterns from each other.&lt;br /&gt;
&lt;br /&gt;
=== Adapter Pattern ===&lt;br /&gt;
&lt;br /&gt;
Adapter pattern plays an important role when you want to incompatible interfaces to work together.The real world example for an adapter pattern is the travel power adapter.Different countries have different socket and plug configuration. So you can use an adapter to fit a plug into a socket that initially was not possible due to different interface designs.&lt;br /&gt;
&lt;br /&gt;
==== Implementattion ====&lt;br /&gt;
&lt;br /&gt;
===== Adapter implementation using inheritance =====&lt;br /&gt;
&lt;br /&gt;
This method can be used when you have incompatible method that needs to be used in other class. Using inheritance a &amp;quot;is-a&amp;quot; relationship is established between the base class and super class. The new compatible methods will be contained in the inherited adapter class.&lt;br /&gt;
&lt;br /&gt;
 public class CylindricalSocket {&lt;br /&gt;
   public String supply(String cylinStem1, String cylinStem1) {&lt;br /&gt;
     System.out.println(&amp;quot;Power power power...&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularAdapter extends CylindricalSocket {&lt;br /&gt;
   public String adapt(String rectaStem1, Sting rectaStem2) {&lt;br /&gt;
     //some conversion logic&lt;br /&gt;
     String cylinStem1 = rectaStem1;&lt;br /&gt;
     String cylinStem2 = rectaStem2;&lt;br /&gt;
     return supply(cylinStem1, cylinStem2);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularPlug {&lt;br /&gt;
   private String rectaStem1;&lt;br /&gt;
   private String rectaStem2;&lt;br /&gt;
   public getPower() {&lt;br /&gt;
     RectangulrAdapter adapter = new RectangulrAdapter();&lt;br /&gt;
     String power = adapter.adapt(rectaStem1, rectaStem2);&lt;br /&gt;
     System.out.println(power);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===== Adapter implementation using composition =====&lt;br /&gt;
&lt;br /&gt;
The other approach is to have a base class as an attribute in the adapter class. This creates an &amp;quot;has-a&amp;quot;relationship between the base class and the sub class.&lt;br /&gt;
&lt;br /&gt;
 public class CylindricalSocket {&lt;br /&gt;
   public String supply(String cylinStem1, String cylinStem1) {&lt;br /&gt;
     System.out.println(&amp;quot;Power power power...&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularAdapter {&lt;br /&gt;
   private CylindricalSocket socket;&lt;br /&gt;
 &lt;br /&gt;
   public String adapt(String rectaStem1, Sting rectaStem2) {&lt;br /&gt;
     //some conversion logic&lt;br /&gt;
     socket = new CylindricalSocket();&lt;br /&gt;
     String cylinStem1 = rectaStem1;&lt;br /&gt;
     String cylinStem2 = rectaStem2;&lt;br /&gt;
     return socket.supply(cylinStem1, cylinStem2);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularPlug {&lt;br /&gt;
   private String rectaStem1;&lt;br /&gt;
   private String rectaStem2;&lt;br /&gt;
   public getPower() {&lt;br /&gt;
     RectangulrAdapter adapter = new RectangulrAdapter();&lt;br /&gt;
     String power = adapter.adapt(rectaStem1, rectaStem2);&lt;br /&gt;
     System.out.println(power);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===== Composition or inheritance to implement Adapter Pattern? =====&lt;br /&gt;
&lt;br /&gt;
* Composition is preferred over inheritance since using composition it is easy to change the behavior of a class.&lt;br /&gt;
&lt;br /&gt;
===== Adapter pattern in Java Library=====&lt;br /&gt;
* java.io.InputStreamReader(InputStream)&lt;br /&gt;
* java.io.OutputStreamWriter(OutputStream)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Bridge Pattern ===&lt;br /&gt;
As stated by GoF a bridge design patterns intent is to “Decouple an abstraction from its implementation so that the two can vary independently”.&lt;br /&gt;
&lt;br /&gt;
===== Elements of Bridge Design Pattern =====&lt;br /&gt;
* Abstraction: This is where the abstraction interface is defined.&lt;br /&gt;
&lt;br /&gt;
For example: A  Vehicle class which has a manufacture method.&lt;br /&gt;
 &lt;br /&gt;
* Refined Abstraction: Refined Abstraction extends the interface defined by Abstraction.&lt;br /&gt;
&lt;br /&gt;
For example, a Car class or a Bike class which has a manufacture method which is more refined than the Vehicle class.&lt;br /&gt;
 &lt;br /&gt;
* Implementor: It defines the interface for the implementation classes. It defines the basic operation.&lt;br /&gt;
&lt;br /&gt;
For example, the Workshop class acts as an implementor with the work() method.&lt;br /&gt;
&lt;br /&gt;
* Concrete Implementation: This implements the Implementor interface and gives it a more concrete implementation.&lt;br /&gt;
 &lt;br /&gt;
For example the Produce and the Assemble class with the work() method provides the concrete implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Bridge-Pattern.jpg|350 px|thumb|right|UML Diagram for Bridge-Pattern]]&lt;br /&gt;
[[File:WBP.jpg|350 px|thumb|left|Without Bridge-Pattern]][[File:bp.jpg|350 px|thumb|center|With Bridge-Pattern]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Example code for Bridge pattern =====&lt;br /&gt;
Vehicle Interface&lt;br /&gt;
&lt;br /&gt;
 // abstraction in bridge pattern&lt;br /&gt;
 abstract class Vehicle {&lt;br /&gt;
   protected Workshop workShop1;&lt;br /&gt;
   protected Workshop workShop2;&lt;br /&gt;
 &lt;br /&gt;
   protected Vehicle(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     this.workShop1 = workShop1;&lt;br /&gt;
     this.workShop2 = workShop2;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   abstract public void manufacture();&lt;br /&gt;
 }&lt;br /&gt;
Car class &lt;br /&gt;
&lt;br /&gt;
 //Refine abstraction 1 in bridge pattern&lt;br /&gt;
 public class Car extends Vehicle {&lt;br /&gt;
 &lt;br /&gt;
   public Car(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     super(workShop1, workShop2);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void manufacture() {&lt;br /&gt;
     System.out.print(&amp;quot;Car &amp;quot;);&lt;br /&gt;
     workShop1.work();&lt;br /&gt;
     workShop2.work();&lt;br /&gt;
 &lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Bike Class &lt;br /&gt;
&lt;br /&gt;
 // Refine abstraction 2 in bridge pattern&lt;br /&gt;
 public class Bike extends Vehicle {&lt;br /&gt;
 &lt;br /&gt;
   public Bike(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     super(workShop1, workShop2);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void manufacture() {&lt;br /&gt;
     System.out.print(&amp;quot;Bike &amp;quot;);&lt;br /&gt;
     workShop1.work();&lt;br /&gt;
     workShop2.work();&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Workshop Interface &lt;br /&gt;
 &lt;br /&gt;
  // Implementor for bridge pattern&lt;br /&gt;
 public interface Workshop {&lt;br /&gt;
   abstract public void work();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Produce Class &lt;br /&gt;
&lt;br /&gt;
 // Concrete implementation 1 for bridge pattern&lt;br /&gt;
 public class Produce implements Workshop {&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void work() {&lt;br /&gt;
     System.out.print(&amp;quot;Produced&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Assemble Class&lt;br /&gt;
   //Concrete implementation 2 for bridge pattern&lt;br /&gt;
&lt;br /&gt;
 public class Assemble implements Workshop {&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void work() {&lt;br /&gt;
     System.out.println(&amp;quot; Assembled.&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Demonstration of bridge design pattern&lt;br /&gt;
&lt;br /&gt;
 public class BridgePattern {&lt;br /&gt;
 &lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
 &lt;br /&gt;
     Vehicle vehicle1 = new Car(new Produce(), new Assemble());&lt;br /&gt;
     vehicle1.manufacture();&lt;br /&gt;
     Vehicle vehicle2 = new Bike(new Produce(), new Assemble());&lt;br /&gt;
     vehicle2.manufacture();&lt;br /&gt;
 &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Output:&lt;br /&gt;
 Car Produced Assembled.&lt;br /&gt;
 Bike Produced Assembled.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Bridge vs Adapter pattern =====&lt;br /&gt;
* Two incompatable classes can be made to work together using adapter pattern.&lt;br /&gt;
* Bridge pattern creates two separate hierarchies by separating the abstraction from the implementation.&lt;br /&gt;
&lt;br /&gt;
While the two patterns may seem similar according to the GoF- &amp;quot;Adapter makes things work after they're designed; Bridge makes them work before they are. [GoF, p219]&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The adapter pattern is useful when we have code that is either third party or maybe in house, over which we do not have any control and such code do not fit with the interface that we wish to use. In such a case we use either of two (delegation or inheritance) model of the adapter pattern to change the existing classes (which do not fit our interface) and cast them to our desired interface.  &lt;br /&gt;
&lt;br /&gt;
The bridge pattern on the other hand is something we implement upfront on classes that have orthogonal hierarchies. It provides a way to decouple the interface and the implementation in such a way that you don't get an insane number of classes. In bridge pattern we usually deal with classes that vary in their implementations a lot and we want to incorporate the functionality of the implementation classes as much as possible. Bridge pattern is many times implemented with the help adapter pattern to give the orthogonal hierarchies  a common interface. &lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
We have a FileReader interface with two implementations. One that reads file from windows system and one that read Files from Linux systems. Now there are two more interface say MemoryMappedFiles and RADFiles. Now bridge pattern will help you in not having to write four classes  MemoryMappedWindowsFileReader, MemoryMappedLinuxFileReader, RADWindowsFileReader, RADWindowsFileReader. Here is how&lt;br /&gt;
&lt;br /&gt;
Use adapter pattern to adapt MemoryMappedFiles and RADFiles to a common interface (assuming both implementations have SpecialFile interface in common). Also WindowsFileReader and LinuxFileReader are two implementations of the FileReader interface.&lt;br /&gt;
&lt;br /&gt;
public interface CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   public void readFile(); // used for adapting&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SpecialWindowsFileReader extends WindowsFileReader implements CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   SpecialFile specialFile;&lt;br /&gt;
&lt;br /&gt;
   public SpecialWindowsFileReader(SpecialFile specialFile){&lt;br /&gt;
      this.specialFile = specialFile;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   public void readFile(){&lt;br /&gt;
      byte fileBytes[] = this.read(); // read platform specific and convert to bytes&lt;br /&gt;
      this.specialFile(fileBytes) // decouples from the filesystem specifics.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SpecialLinuxFileReader extends LinuxFileReader implements CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   SpecialFile specialFile;&lt;br /&gt;
&lt;br /&gt;
   public SpecialLinuxFileReader(SpecialFile specialFile){&lt;br /&gt;
      this.specialFile = specialFile;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   public void readFile(){&lt;br /&gt;
      byte fileBytes[] = this.read(); // read platform specific and convert to bytes&lt;br /&gt;
      this.specialFile(fileBytes) // decouples from the filesystem specifics.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// bridge pattern&lt;br /&gt;
&lt;br /&gt;
public class Bridge{&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
     CrossPlatformSpecialPurposeFileReader specialFiles[] = new CrossPlatformSpecialPurposeFileReader[2];&lt;br /&gt;
     specialFiles[0] = new SpecialWindowsFileReader(new MemoryMappedFiles());&lt;br /&gt;
     specialFiles[1] = new SpecialLinuxFileReader(new RADFiles());&lt;br /&gt;
     // deouples two orthogonal implementations under one interface.&lt;br /&gt;
     for(CrossPlatformSpecialPurposeFileReader reader : specialFiles){&lt;br /&gt;
            reader.readFile();     &lt;br /&gt;
     }   &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Decorator Pattern ===&lt;br /&gt;
&lt;br /&gt;
The decorator pattern adds responsibility to an object dynamically. The decorator design pattern is used to extend the behavior of an object dynamically. In order to extend the behavior we have to construct an wrapper around the object. Inheritance is not feasible since it is applied to an entire class. With the decorator pattern it is possible to select any particular instance and modify its behavior leaving the other instances unmodified.&lt;br /&gt;
&lt;br /&gt;
[[File:Decorator-Pattern.jpg|200 px|thumb|right|UML Diagram for Decorator-Pattern]]&lt;br /&gt;
==== Implementation of Decorator Pattern ====&lt;br /&gt;
First start with the interface which will be used by the class having the decoration design.&lt;br /&gt;
The example below explains how you first create a base ice cream and then decorate that ice cream by adding new toppings. The added topping changes the behavior of that ice cream. i.e. it gives the ice cream more taste.&lt;br /&gt;
&lt;br /&gt;
The interface below contains an makeIcecream() method which has not been implemented yet.&lt;br /&gt;
 public interface Icecream {&lt;br /&gt;
  public String makeIcecream();&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
The SimpleIceCream Class provides an implementation to the makeIcecream() method. This is the class which acts as a base class on which decorations will be added.&lt;br /&gt;
&lt;br /&gt;
 public class SimpleIcecream implements Icecream {&lt;br /&gt;
 &lt;br /&gt;
  @Override&lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return &amp;quot;Base Icecream&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
The class below is the crux of the design pattern. Here is where all the Decoration is actually provided to an instance of the object.It contains an attribute of type Icecream. Once the instance is assigned using the constructor the instance method will be invoked.&lt;br /&gt;
&lt;br /&gt;
 abstract class IcecreamDecorator implements Icecream {&lt;br /&gt;
 &lt;br /&gt;
  protected Icecream specialIcecream;&lt;br /&gt;
 &lt;br /&gt;
  public IcecreamDecorator(Icecream specialIcecream) {&lt;br /&gt;
    this.specialIcecream = specialIcecream;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The NuttyDecorator and HoneyDecorator are the two concrete classes that implement the abstract decorator class IcecreamDecorator.&lt;br /&gt;
&lt;br /&gt;
 public class NuttyDecorator extends IcecreamDecorator {&lt;br /&gt;
 &lt;br /&gt;
  public NuttyDecorator(Icecream specialIcecream) {&lt;br /&gt;
    super(specialIcecream);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream() + addNuts();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private String addNuts() {&lt;br /&gt;
    return &amp;quot; + cruncy nuts&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HoneyDecorator extends IcecreamDecorator {&lt;br /&gt;
 &lt;br /&gt;
  public HoneyDecorator(Icecream specialIcecream) {&lt;br /&gt;
    super(specialIcecream);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream() + addHoney();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private String addHoney() {&lt;br /&gt;
    return &amp;quot; + sweet honey&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Decorator pattern in Java Library=====&lt;br /&gt;
* java.io.BufferedReader;&lt;br /&gt;
* java.io.FileReader;&lt;br /&gt;
* java.io.Reader;&lt;br /&gt;
&lt;br /&gt;
==== Adaptor Pattern vs Decorator Pattern====&lt;br /&gt;
* Adapter provides a different interface to its subject.&lt;br /&gt;
* Decorator provides an enhanced interface.&lt;br /&gt;
&lt;br /&gt;
One important bit of difference between the two patterns is - In adapter pattern we adapt an existing interface to an interface that suits our needs. On the other hand in decorator pattern the aim is to enhance the existing interface such that the new interface can replace the existing interface without any changes in code.  &lt;br /&gt;
&lt;br /&gt;
http://sourcemaking.com/design_patterns/decorator&lt;br /&gt;
&lt;br /&gt;
=== Facade Pattern ===&lt;br /&gt;
&lt;br /&gt;
[[File:Wof.jpg|200 px|thumb|right|Without Facade-Pattern]]&lt;br /&gt;
[[File:Wf.jpg|200 px|thumb|right|With Facade-Pattern]]&lt;br /&gt;
[[File:FacadeUml.jpg|200 px|thumb|left|Microwave Design using Facade Pattern]]&lt;br /&gt;
GoF defines a facade design pattern as a pattern which , “Provides a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.”&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider a software component. In order to get the job done this component might have to expose a of interfaces so that it can interact with other components in a process flow. Facade pattern simplifies this process by providing an unified interface. It builds an additional layer of abstraction over the existing abstract layer to simplify the process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A microwave oven is made up of components like trasnformer, capacitor, magnetron, waveguide and some small other components. For a microwave to work all these different components needs to be executed in a sequence.  If all these components had separate interfaces it would have been hard and complicated.Hence  oven provides you buttons which can be considered as a facade. When you click on single button the job is done. That single button works as an abstraction layer between the user and the internal components.&lt;br /&gt;
&lt;br /&gt;
==== Facade Pattern in java library ====&lt;br /&gt;
ExternalContext Class in java performs cookie management by using an facade pattern. It makes use of components such as HttpSession, ServletContext, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse.&lt;br /&gt;
&lt;br /&gt;
==== Facade Pattern vs Adapter Pattern ====&lt;br /&gt;
* Adapter pattern is to make an incompatible class work with other class where as facade provides programming comfort by providing an unified interface.&lt;br /&gt;
* Adapter Pattern using inheritance increases inter dependencies whereas facade pattern provides loose coupling&lt;br /&gt;
* Changing data to suit the interface of a subsystem is done by facade whereas changing the structure of a system is done by adapter pattern&lt;br /&gt;
*  Facade increases subsystem independence and portability. This is not possible using the adapter pattern.&lt;br /&gt;
&lt;br /&gt;
Facade defines a new interface, whereas Adapter uses an old interface. Adapter makes two existing interfaces work together as opposed to defining an entirely new one. Adapter and Facade are both wrappers; but they are different kinds of wrappers. The intent of Facade is to produce a simpler interface, and the intent of Adapter is to design to an existing interface. While Facade routinely wraps multiple objects and Adapter wraps a single object; Facade could front-end a single complex object and Adapter could wrap several legacy objects&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w37_ms&amp;diff=70162</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w37 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w37_ms&amp;diff=70162"/>
		<updated>2012-11-18T15:22:08Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Adapter pattern and the related patterns (Bridge, Decorator, Facade)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The adapter design pattern allows the user to make changes to the existing class with other class libraries without changing the code for the existing class. The Bridge, Decorator and the Facade pattern look somewhat similar to the adapter pattern but their intent is different and that intent is what separates the above patterns from each other.&lt;br /&gt;
&lt;br /&gt;
=== Adapter Pattern ===&lt;br /&gt;
&lt;br /&gt;
Adapter pattern plays an important role when you want to incompatible interfaces to work together.The real world example for an adapter pattern is the travel power adapter.Different countries have different socket and plug configuration. So you can use an adapter to fit a plug into a socket that initially was not possible due to different interface designs.&lt;br /&gt;
&lt;br /&gt;
==== Implementattion ====&lt;br /&gt;
&lt;br /&gt;
===== Adapter implementation using inheritance =====&lt;br /&gt;
&lt;br /&gt;
This method can be used when you have incompatible method that needs to be used in other class. Using inheritance a &amp;quot;is-a&amp;quot; relationship is established between the base class and super class. The new compatible methods will be contained in the inherited adapter class.&lt;br /&gt;
&lt;br /&gt;
 public class CylindricalSocket {&lt;br /&gt;
   public String supply(String cylinStem1, String cylinStem1) {&lt;br /&gt;
     System.out.println(&amp;quot;Power power power...&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularAdapter extends CylindricalSocket {&lt;br /&gt;
   public String adapt(String rectaStem1, Sting rectaStem2) {&lt;br /&gt;
     //some conversion logic&lt;br /&gt;
     String cylinStem1 = rectaStem1;&lt;br /&gt;
     String cylinStem2 = rectaStem2;&lt;br /&gt;
     return supply(cylinStem1, cylinStem2);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularPlug {&lt;br /&gt;
   private String rectaStem1;&lt;br /&gt;
   private String rectaStem2;&lt;br /&gt;
   public getPower() {&lt;br /&gt;
     RectangulrAdapter adapter = new RectangulrAdapter();&lt;br /&gt;
     String power = adapter.adapt(rectaStem1, rectaStem2);&lt;br /&gt;
     System.out.println(power);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===== Adapter implementation using composition =====&lt;br /&gt;
&lt;br /&gt;
The other approach is to have a base class as an attribute in the adapter class. This creates an &amp;quot;has-a&amp;quot;relationship between the base class and the sub class.&lt;br /&gt;
&lt;br /&gt;
 public class CylindricalSocket {&lt;br /&gt;
   public String supply(String cylinStem1, String cylinStem1) {&lt;br /&gt;
     System.out.println(&amp;quot;Power power power...&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularAdapter {&lt;br /&gt;
   private CylindricalSocket socket;&lt;br /&gt;
 &lt;br /&gt;
   public String adapt(String rectaStem1, Sting rectaStem2) {&lt;br /&gt;
     //some conversion logic&lt;br /&gt;
     socket = new CylindricalSocket();&lt;br /&gt;
     String cylinStem1 = rectaStem1;&lt;br /&gt;
     String cylinStem2 = rectaStem2;&lt;br /&gt;
     return socket.supply(cylinStem1, cylinStem2);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularPlug {&lt;br /&gt;
   private String rectaStem1;&lt;br /&gt;
   private String rectaStem2;&lt;br /&gt;
   public getPower() {&lt;br /&gt;
     RectangulrAdapter adapter = new RectangulrAdapter();&lt;br /&gt;
     String power = adapter.adapt(rectaStem1, rectaStem2);&lt;br /&gt;
     System.out.println(power);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===== Composition or inheritance to implement Adapter Pattern? =====&lt;br /&gt;
&lt;br /&gt;
* Composition is preferred over inheritance since using composition it is easy to change the behavior of a class.&lt;br /&gt;
&lt;br /&gt;
===== Adapter pattern in Java Library=====&lt;br /&gt;
* java.io.InputStreamReader(InputStream)&lt;br /&gt;
* java.io.OutputStreamWriter(OutputStream)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Bridge Pattern ===&lt;br /&gt;
As stated by GoF a bridge design patterns intent is to “Decouple an abstraction from its implementation so that the two can vary independently”.&lt;br /&gt;
&lt;br /&gt;
===== Elements of Bridge Design Pattern =====&lt;br /&gt;
* Abstraction: This is where the abstraction interface is defined.&lt;br /&gt;
&lt;br /&gt;
For example: A  Vehicle class which has a manufacture method.&lt;br /&gt;
 &lt;br /&gt;
* Refined Abstraction: Refined Abstraction extends the interface defined by Abstraction.&lt;br /&gt;
&lt;br /&gt;
For example, a Car class or a Bike class which has a manufacture method which is more refined than the Vehicle class.&lt;br /&gt;
 &lt;br /&gt;
* Implementor: It defines the interface for the implementation classes. It defines the basic operation.&lt;br /&gt;
&lt;br /&gt;
For example, the Workshop class acts as an implementor with the work() method.&lt;br /&gt;
&lt;br /&gt;
* Concrete Implementation: This implements the Implementor interface and gives it a more concrete implementation.&lt;br /&gt;
 &lt;br /&gt;
For example the Produce and the Assemble class with the work() method provides the concrete implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Bridge-Pattern.jpg|350 px|thumb|right|UML Diagram for Bridge-Pattern]]&lt;br /&gt;
[[File:WBP.jpg|350 px|thumb|left|Without Bridge-Pattern]][[File:bp.jpg|350 px|thumb|center|With Bridge-Pattern]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Example code for Bridge pattern =====&lt;br /&gt;
Vehicle Interface&lt;br /&gt;
&lt;br /&gt;
 // abstraction in bridge pattern&lt;br /&gt;
 abstract class Vehicle {&lt;br /&gt;
   protected Workshop workShop1;&lt;br /&gt;
   protected Workshop workShop2;&lt;br /&gt;
 &lt;br /&gt;
   protected Vehicle(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     this.workShop1 = workShop1;&lt;br /&gt;
     this.workShop2 = workShop2;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   abstract public void manufacture();&lt;br /&gt;
 }&lt;br /&gt;
Car class &lt;br /&gt;
&lt;br /&gt;
 //Refine abstraction 1 in bridge pattern&lt;br /&gt;
 public class Car extends Vehicle {&lt;br /&gt;
 &lt;br /&gt;
   public Car(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     super(workShop1, workShop2);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void manufacture() {&lt;br /&gt;
     System.out.print(&amp;quot;Car &amp;quot;);&lt;br /&gt;
     workShop1.work();&lt;br /&gt;
     workShop2.work();&lt;br /&gt;
 &lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Bike Class &lt;br /&gt;
&lt;br /&gt;
 // Refine abstraction 2 in bridge pattern&lt;br /&gt;
 public class Bike extends Vehicle {&lt;br /&gt;
 &lt;br /&gt;
   public Bike(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     super(workShop1, workShop2);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void manufacture() {&lt;br /&gt;
     System.out.print(&amp;quot;Bike &amp;quot;);&lt;br /&gt;
     workShop1.work();&lt;br /&gt;
     workShop2.work();&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Workshop Interface &lt;br /&gt;
 &lt;br /&gt;
  // Implementor for bridge pattern&lt;br /&gt;
 public interface Workshop {&lt;br /&gt;
   abstract public void work();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Produce Class &lt;br /&gt;
&lt;br /&gt;
 // Concrete implementation 1 for bridge pattern&lt;br /&gt;
 public class Produce implements Workshop {&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void work() {&lt;br /&gt;
     System.out.print(&amp;quot;Produced&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Assemble Class&lt;br /&gt;
   //Concrete implementation 2 for bridge pattern&lt;br /&gt;
&lt;br /&gt;
 public class Assemble implements Workshop {&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void work() {&lt;br /&gt;
     System.out.println(&amp;quot; Assembled.&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Demonstration of bridge design pattern&lt;br /&gt;
&lt;br /&gt;
 public class BridgePattern {&lt;br /&gt;
 &lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
 &lt;br /&gt;
     Vehicle vehicle1 = new Car(new Produce(), new Assemble());&lt;br /&gt;
     vehicle1.manufacture();&lt;br /&gt;
     Vehicle vehicle2 = new Bike(new Produce(), new Assemble());&lt;br /&gt;
     vehicle2.manufacture();&lt;br /&gt;
 &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Output:&lt;br /&gt;
 Car Produced Assembled.&lt;br /&gt;
 Bike Produced Assembled.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Bridge vs Adapter pattern =====&lt;br /&gt;
* Two incompatable classes can be made to work together using adapter pattern.&lt;br /&gt;
* Bridge pattern creates two separate hierarchies by separating the abstraction from the implementation.&lt;br /&gt;
&lt;br /&gt;
While the two patterns may seem similar according to the GoF- &amp;quot;Adapter makes things work after they're designed; Bridge makes them work before they are. [GoF, p219]&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The adapter pattern is useful when we have code that is either third party or maybe in house, over which we do not have any control and such code do not fit with the interface that we wish to use. In such a case we use either of two (delegation or inheritance) model of the adapter pattern to change the existing classes (which do not fit our interface) and cast them to our desired interface.  &lt;br /&gt;
&lt;br /&gt;
The bridge pattern on the other hand is something we implement upfront on classes that have orthogonal hierarchies. It provides a way to decouple the interface and the implementation in such a way that you don't get an insane number of classes. In bridge pattern we usually deal with classes that vary in their implementations a lot and we want to incorporate the functionality of the implementation classes as much as possible. Bridge pattern is many times implemented with the help adapter pattern to give the orthogonal hierarchies  a common interface. &lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
We have a FileReader interface with two implementations. One that reads file from windows system and one that read Files from Linux systems. Now there are two more interface say MemoryMappedFiles and RADFiles. Now bridge pattern will help you in not having to write four classes  MemoryMappedWindowsFileReader, MemoryMappedLinuxFileReader, RADWindowsFileReader, RADWindowsFileReader. Here is how&lt;br /&gt;
&lt;br /&gt;
Use adapter pattern to adapt MemoryMappedFiles and RADFiles to a common interface (assuming both implementations have SpecialFile interface in common). Also WindowsFileReader and LinuxFileReader are two implementations of the FileReader interface.&lt;br /&gt;
&lt;br /&gt;
public interface CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   public void readFile(); // used for adapting&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SpecialWindowsFileReader extends WindowsFileReader implements CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   SpecialFile specialFile;&lt;br /&gt;
&lt;br /&gt;
   public SpecialWindowsFileReader(SpecialFile specialFile){&lt;br /&gt;
      this.specialFile = specialFile;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   public void readFile(){&lt;br /&gt;
      byte fileBytes[] = this.read(); // read platform specific and convert to bytes&lt;br /&gt;
      this.specialFile(fileBytes) // decouples from the filesystem specifics.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SpecialLinuxFileReader extends LinuxFileReader implements CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   SpecialFile specialFile;&lt;br /&gt;
&lt;br /&gt;
   public SpecialLinuxFileReader(SpecialFile specialFile){&lt;br /&gt;
      this.specialFile = specialFile;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   public void readFile(){&lt;br /&gt;
      byte fileBytes[] = this.read(); // read platform specific and convert to bytes&lt;br /&gt;
      this.specialFile(fileBytes) // decouples from the filesystem specifics.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// bridge pattern&lt;br /&gt;
&lt;br /&gt;
public class Bridge{&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
     CrossPlatformSpecialPurposeFileReader specialFiles[] = new CrossPlatformSpecialPurposeFileReader[2];&lt;br /&gt;
     specialFiles[0] = new SpecialWindowsFileReader(new MemoryMappedFiles());&lt;br /&gt;
     specialFiles[1] = new SpecialLinuxFileReader(new RADFiles());&lt;br /&gt;
     // deouples two orthogonal implementations under one interface.&lt;br /&gt;
     for(CrossPlatformSpecialPurposeFileReader reader : specialFiles){&lt;br /&gt;
            reader.readFile();     &lt;br /&gt;
     }   &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Decorator Pattern ===&lt;br /&gt;
&lt;br /&gt;
The decorator pattern adds responsibility to an object dynamically. The decorator design pattern is used to extend the behavior of an object dynamically. In order to extend the behavior we have to construct an wrapper around the object. Inheritance is not feasible since it is applied to an entire class. With the decorator pattern it is possible to select any particular instance and modify its behavior leaving the other instances unmodified.&lt;br /&gt;
&lt;br /&gt;
[[File:Decorator-Pattern.jpg|200 px|thumb|right|UML Diagram for Decorator-Pattern]]&lt;br /&gt;
==== Implementation of Decorator Pattern ====&lt;br /&gt;
First start with the interface which will be used by the class having the decoration design.&lt;br /&gt;
The example below explains how you first create a base ice cream and then decorate that ice cream by adding new toppings. The added topping changes the behavior of that ice cream. i.e. it gives the ice cream more taste.&lt;br /&gt;
&lt;br /&gt;
The interface below contains an makeIcecream() method which has not been implemented yet.&lt;br /&gt;
 public interface Icecream {&lt;br /&gt;
  public String makeIcecream();&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
The SimpleIceCream Class provides an implementation to the makeIcecream() method. This is the class which acts as a base class on which decorations will be added.&lt;br /&gt;
&lt;br /&gt;
 public class SimpleIcecream implements Icecream {&lt;br /&gt;
 &lt;br /&gt;
  @Override&lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return &amp;quot;Base Icecream&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
The class below is the crux of the design pattern. Here is where all the Decoration is actually provided to an instance of the object.It contains an attribute of type Icecream. Once the instance is assigned using the constructor the instance method will be invoked.&lt;br /&gt;
&lt;br /&gt;
 abstract class IcecreamDecorator implements Icecream {&lt;br /&gt;
 &lt;br /&gt;
  protected Icecream specialIcecream;&lt;br /&gt;
 &lt;br /&gt;
  public IcecreamDecorator(Icecream specialIcecream) {&lt;br /&gt;
    this.specialIcecream = specialIcecream;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The NuttyDecorator and HoneyDecorator are the two concrete classes that implement the abstract decorator class IcecreamDecorator.&lt;br /&gt;
&lt;br /&gt;
 public class NuttyDecorator extends IcecreamDecorator {&lt;br /&gt;
 &lt;br /&gt;
  public NuttyDecorator(Icecream specialIcecream) {&lt;br /&gt;
    super(specialIcecream);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream() + addNuts();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private String addNuts() {&lt;br /&gt;
    return &amp;quot; + cruncy nuts&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HoneyDecorator extends IcecreamDecorator {&lt;br /&gt;
 &lt;br /&gt;
  public HoneyDecorator(Icecream specialIcecream) {&lt;br /&gt;
    super(specialIcecream);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream() + addHoney();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private String addHoney() {&lt;br /&gt;
    return &amp;quot; + sweet honey&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Decorator pattern in Java Library=====&lt;br /&gt;
* java.io.BufferedReader;&lt;br /&gt;
* java.io.FileReader;&lt;br /&gt;
* java.io.Reader;&lt;br /&gt;
&lt;br /&gt;
==== Adaptor Pattern vs Decorator Pattern====&lt;br /&gt;
* Adapter provides a different interface to its subject.&lt;br /&gt;
* Decorator provides an enhanced interface.&lt;br /&gt;
&lt;br /&gt;
One important bit of difference between the two patterns is - In adapter pattern we adapt an existing interface to an interface that suits our needs. On the other hand in decorator pattern the aim is to enhance the existing interface such that the new interface can replace the existing interface without any changes in code.  &lt;br /&gt;
&lt;br /&gt;
http://sourcemaking.com/design_patterns/decorator&lt;br /&gt;
&lt;br /&gt;
=== Facade Pattern ===&lt;br /&gt;
&lt;br /&gt;
[[File:Wof.jpg|200 px|thumb|right|Without Facade-Pattern]]&lt;br /&gt;
[[File:Wf.jpg|200 px|thumb|right|With Facade-Pattern]]&lt;br /&gt;
[[File:FacadeUml.jpg|200 px|thumb|left|Microwave Design using Facade Pattern]]&lt;br /&gt;
GoF defines a facade design pattern as a pattern which , “Provides a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.”&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider a software component. In order to get the job done this component might have to expose a of interfaces so that it can interact with other components in a process flow. Facade pattern simplifies this process by providing an unified interface. It builds an additional layer of abstraction over the existing abstract layer to simplify the process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A microwave oven is made up of components like trasnformer, capacitor, magnetron, waveguide and some small other components. For a microwave to work all these different components needs to be executed in a sequence.  If all these components had separate interfaces it would have been hard and complicated.Hence  oven provides you buttons which can be considered as a facade. When you click on single button the job is done. That single button works as an abstraction layer between the user and the internal components.&lt;br /&gt;
&lt;br /&gt;
==== Facade Pattern in java library ====&lt;br /&gt;
ExternalContext Class in java performs cookie management by using an facade pattern. It makes use of components such as HttpSession, ServletContext, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse.&lt;br /&gt;
&lt;br /&gt;
==== Facade Pattern vs Adapter Pattern ====&lt;br /&gt;
* Adapter pattern is to make an incompatible class work with other class where as facade provides programming comfort by providing an unified interface.&lt;br /&gt;
* Adapter Pattern using inheritance increases inter dependencies whereas facade pattern provides loose coupling&lt;br /&gt;
* Changing data to suit the interface of a subsystem is done by facade whereas changing the structure of a system is done by adapter pattern&lt;br /&gt;
*  Facade increases subsystem independence and portability. This is not possible using the adapter pattern.&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w37_ms&amp;diff=70161</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w37 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w37_ms&amp;diff=70161"/>
		<updated>2012-11-18T15:06:03Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Adapter pattern and the related patterns (Bridge, Decorator, Facade)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The adapter design pattern allows the user to make changes to the existing class with other class libraries without changing the code for the existing class. The Bridge, Decorator and the Facade pattern look somewhat similar to the adapter pattern but their intent is different and that intent is what separates the above patterns from each other.&lt;br /&gt;
&lt;br /&gt;
=== Adapter Pattern ===&lt;br /&gt;
&lt;br /&gt;
Adapter pattern plays an important role when you want to incompatible interfaces to work together.The real world example for an adapter pattern is the travel power adapter.Different countries have different socket and plug configuration. So you can use an adapter to fit a plug into a socket that initially was not possible due to different interface designs.&lt;br /&gt;
&lt;br /&gt;
==== Implementattion ====&lt;br /&gt;
&lt;br /&gt;
===== Adapter implementation using inheritance =====&lt;br /&gt;
&lt;br /&gt;
This method can be used when you have incompatible method that needs to be used in other class. Using inheritance a &amp;quot;is-a&amp;quot; relationship is established between the base class and super class. The new compatible methods will be contained in the inherited adapter class.&lt;br /&gt;
&lt;br /&gt;
 public class CylindricalSocket {&lt;br /&gt;
   public String supply(String cylinStem1, String cylinStem1) {&lt;br /&gt;
     System.out.println(&amp;quot;Power power power...&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularAdapter extends CylindricalSocket {&lt;br /&gt;
   public String adapt(String rectaStem1, Sting rectaStem2) {&lt;br /&gt;
     //some conversion logic&lt;br /&gt;
     String cylinStem1 = rectaStem1;&lt;br /&gt;
     String cylinStem2 = rectaStem2;&lt;br /&gt;
     return supply(cylinStem1, cylinStem2);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularPlug {&lt;br /&gt;
   private String rectaStem1;&lt;br /&gt;
   private String rectaStem2;&lt;br /&gt;
   public getPower() {&lt;br /&gt;
     RectangulrAdapter adapter = new RectangulrAdapter();&lt;br /&gt;
     String power = adapter.adapt(rectaStem1, rectaStem2);&lt;br /&gt;
     System.out.println(power);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===== Adapter implementation using composition =====&lt;br /&gt;
&lt;br /&gt;
The other approach is to have a base class as an attribute in the adapter class. This creates an &amp;quot;has-a&amp;quot;relationship between the base class and the sub class.&lt;br /&gt;
&lt;br /&gt;
 public class CylindricalSocket {&lt;br /&gt;
   public String supply(String cylinStem1, String cylinStem1) {&lt;br /&gt;
     System.out.println(&amp;quot;Power power power...&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularAdapter {&lt;br /&gt;
   private CylindricalSocket socket;&lt;br /&gt;
 &lt;br /&gt;
   public String adapt(String rectaStem1, Sting rectaStem2) {&lt;br /&gt;
     //some conversion logic&lt;br /&gt;
     socket = new CylindricalSocket();&lt;br /&gt;
     String cylinStem1 = rectaStem1;&lt;br /&gt;
     String cylinStem2 = rectaStem2;&lt;br /&gt;
     return socket.supply(cylinStem1, cylinStem2);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularPlug {&lt;br /&gt;
   private String rectaStem1;&lt;br /&gt;
   private String rectaStem2;&lt;br /&gt;
   public getPower() {&lt;br /&gt;
     RectangulrAdapter adapter = new RectangulrAdapter();&lt;br /&gt;
     String power = adapter.adapt(rectaStem1, rectaStem2);&lt;br /&gt;
     System.out.println(power);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===== Composition or inheritance to implement Adapter Pattern? =====&lt;br /&gt;
&lt;br /&gt;
* Composition is preferred over inheritance since using composition it is easy to change the behavior of a class.&lt;br /&gt;
&lt;br /&gt;
===== Adapter pattern in Java Library=====&lt;br /&gt;
* java.io.InputStreamReader(InputStream)&lt;br /&gt;
* java.io.OutputStreamWriter(OutputStream)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Bridge Pattern ===&lt;br /&gt;
As stated by GoF a bridge design patterns intent is to “Decouple an abstraction from its implementation so that the two can vary independently”.&lt;br /&gt;
&lt;br /&gt;
===== Elements of Bridge Design Pattern =====&lt;br /&gt;
* Abstraction: This is where the abstraction interface is defined.&lt;br /&gt;
&lt;br /&gt;
For example: A  Vehicle class which has a manufacture method.&lt;br /&gt;
 &lt;br /&gt;
* Refined Abstraction: Refined Abstraction extends the interface defined by Abstraction.&lt;br /&gt;
&lt;br /&gt;
For example, a Car class or a Bike class which has a manufacture method which is more refined than the Vehicle class.&lt;br /&gt;
 &lt;br /&gt;
* Implementor: It defines the interface for the implementation classes. It defines the basic operation.&lt;br /&gt;
&lt;br /&gt;
For example, the Workshop class acts as an implementor with the work() method.&lt;br /&gt;
&lt;br /&gt;
* Concrete Implementation: This implements the Implementor interface and gives it a more concrete implementation.&lt;br /&gt;
 &lt;br /&gt;
For example the Produce and the Assemble class with the work() method provides the concrete implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Bridge-Pattern.jpg|350 px|thumb|right|UML Diagram for Bridge-Pattern]]&lt;br /&gt;
[[File:WBP.jpg|350 px|thumb|left|Without Bridge-Pattern]][[File:bp.jpg|350 px|thumb|center|With Bridge-Pattern]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Example code for Bridge pattern =====&lt;br /&gt;
Vehicle Interface&lt;br /&gt;
&lt;br /&gt;
 // abstraction in bridge pattern&lt;br /&gt;
 abstract class Vehicle {&lt;br /&gt;
   protected Workshop workShop1;&lt;br /&gt;
   protected Workshop workShop2;&lt;br /&gt;
 &lt;br /&gt;
   protected Vehicle(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     this.workShop1 = workShop1;&lt;br /&gt;
     this.workShop2 = workShop2;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   abstract public void manufacture();&lt;br /&gt;
 }&lt;br /&gt;
Car class &lt;br /&gt;
&lt;br /&gt;
 //Refine abstraction 1 in bridge pattern&lt;br /&gt;
 public class Car extends Vehicle {&lt;br /&gt;
 &lt;br /&gt;
   public Car(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     super(workShop1, workShop2);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void manufacture() {&lt;br /&gt;
     System.out.print(&amp;quot;Car &amp;quot;);&lt;br /&gt;
     workShop1.work();&lt;br /&gt;
     workShop2.work();&lt;br /&gt;
 &lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Bike Class &lt;br /&gt;
&lt;br /&gt;
 // Refine abstraction 2 in bridge pattern&lt;br /&gt;
 public class Bike extends Vehicle {&lt;br /&gt;
 &lt;br /&gt;
   public Bike(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     super(workShop1, workShop2);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void manufacture() {&lt;br /&gt;
     System.out.print(&amp;quot;Bike &amp;quot;);&lt;br /&gt;
     workShop1.work();&lt;br /&gt;
     workShop2.work();&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Workshop Interface &lt;br /&gt;
 &lt;br /&gt;
  // Implementor for bridge pattern&lt;br /&gt;
 public interface Workshop {&lt;br /&gt;
   abstract public void work();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Produce Class &lt;br /&gt;
&lt;br /&gt;
 // Concrete implementation 1 for bridge pattern&lt;br /&gt;
 public class Produce implements Workshop {&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void work() {&lt;br /&gt;
     System.out.print(&amp;quot;Produced&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Assemble Class&lt;br /&gt;
   //Concrete implementation 2 for bridge pattern&lt;br /&gt;
&lt;br /&gt;
 public class Assemble implements Workshop {&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void work() {&lt;br /&gt;
     System.out.println(&amp;quot; Assembled.&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Demonstration of bridge design pattern&lt;br /&gt;
&lt;br /&gt;
 public class BridgePattern {&lt;br /&gt;
 &lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
 &lt;br /&gt;
     Vehicle vehicle1 = new Car(new Produce(), new Assemble());&lt;br /&gt;
     vehicle1.manufacture();&lt;br /&gt;
     Vehicle vehicle2 = new Bike(new Produce(), new Assemble());&lt;br /&gt;
     vehicle2.manufacture();&lt;br /&gt;
 &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Output:&lt;br /&gt;
 Car Produced Assembled.&lt;br /&gt;
 Bike Produced Assembled.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Bridge vs Adapter pattern =====&lt;br /&gt;
* Two incompatable classes can be made to work together using adapter pattern.&lt;br /&gt;
* Bridge pattern creates two separate hierarchies by separating the abstraction from the implementation.&lt;br /&gt;
&lt;br /&gt;
While the two patterns may seem similar according to the GoF- &amp;quot;Adapter makes things work after they're designed; Bridge makes them work before they are. [GoF, p219]&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The adapter pattern is useful when we have code that is either third party or maybe in house, over which we do not have any control and such code do not fit with the interface that we wish to use. In such a case we use either of two (delegation or inheritance) model of the adapter pattern to change the existing classes (which do not fit our interface) and cast them to our desired interface.  &lt;br /&gt;
&lt;br /&gt;
The bridge pattern on the other hand is something we implement upfront on classes that have orthogonal hierarchies. It provides a way to decouple the interface and the implementation in such a way that you don't get an insane number of classes. In bridge pattern we usually deal with classes that vary in their implementations a lot and we want to incorporate the functionality of the implementation classes as much as possible. Bridge pattern is many times implemented with the help adapter pattern to give the orthogonal hierarchies  a common interface. &lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
We have a FileReader interface with two implementations. One that reads file from windows system and one that read Files from Linux systems. Now there are two more interface say MemoryMappedFiles and RADFiles. Now bridge pattern will help you in not having to write four classes  MemoryMappedWindowsFileReader, MemoryMappedLinuxFileReader, RADWindowsFileReader, RADWindowsFileReader. Here is how&lt;br /&gt;
&lt;br /&gt;
Use adapter pattern to adapt MemoryMappedFiles and RADFiles to a common interface (assuming both implementations have SpecialFile interface in common). Also WindowsFileReader and LinuxFileReader are two implementations of the FileReader interface.&lt;br /&gt;
&lt;br /&gt;
public interface CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   public void readFile(); // used for adapting&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SpecialWindowsFileReader extends WindowsFileReader implements CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   SpecialFile specialFile;&lt;br /&gt;
&lt;br /&gt;
   public SpecialWindowsFileReader(SpecialFile specialFile){&lt;br /&gt;
      this.specialFile = specialFile;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   public void readFile(){&lt;br /&gt;
      byte fileBytes[] = this.read(); // read platform specific and convert to bytes&lt;br /&gt;
      this.specialFile(fileBytes) // decouples from the filesystem specifics.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SpecialLinuxFileReader extends LinuxFileReader implements CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   SpecialFile specialFile;&lt;br /&gt;
&lt;br /&gt;
   public SpecialLinuxFileReader(SpecialFile specialFile){&lt;br /&gt;
      this.specialFile = specialFile;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   public void readFile(){&lt;br /&gt;
      byte fileBytes[] = this.read(); // read platform specific and convert to bytes&lt;br /&gt;
      this.specialFile(fileBytes) // decouples from the filesystem specifics.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// bridge pattern&lt;br /&gt;
&lt;br /&gt;
public class Bridge{&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
     CrossPlatformSpecialPurposeFileReader specialFiles[] = new CrossPlatformSpecialPurposeFileReader[2];&lt;br /&gt;
     specialFiles[0] = new SpecialWindowsFileReader(new MemoryMappedFiles());&lt;br /&gt;
     specialFiles[1] = new SpecialLinuxFileReader(new RADFiles());&lt;br /&gt;
     // deouples two orthogonal implementations under one interface.&lt;br /&gt;
     for(CrossPlatformSpecialPurposeFileReader reader : specialFiles){&lt;br /&gt;
            reader.readFile();     &lt;br /&gt;
     }   &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Decorator Pattern ===&lt;br /&gt;
&lt;br /&gt;
The decorator pattern adds responsibility to an object dynamically. The decorator design pattern is used to extend the behavior of an object dynamically. In order to extend the behavior we have to construct an wrapper around the object. Inheritance is not feasible since it is applied to an entire class. With the decorator pattern it is possible to select any particular instance and modify its behavior leaving the other instances unmodified.&lt;br /&gt;
&lt;br /&gt;
[[File:Decorator-Pattern.jpg|200 px|thumb|right|UML Diagram for Decorator-Pattern]]&lt;br /&gt;
==== Implementation of Decorator Pattern ====&lt;br /&gt;
First start with the interface which will be used by the class having the decoration design.&lt;br /&gt;
The example below explains how you first create a base ice cream and then decorate that ice cream by adding new toppings. The added topping changes the behavior of that ice cream. i.e. it gives the ice cream more taste.&lt;br /&gt;
&lt;br /&gt;
The interface below contains an makeIcecream() method which has not been implemented yet.&lt;br /&gt;
 public interface Icecream {&lt;br /&gt;
  public String makeIcecream();&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
The SimpleIceCream Class provides an implementation to the makeIcecream() method. This is the class which acts as a base class on which decorations will be added.&lt;br /&gt;
&lt;br /&gt;
 public class SimpleIcecream implements Icecream {&lt;br /&gt;
 &lt;br /&gt;
  @Override&lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return &amp;quot;Base Icecream&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
The class below is the crux of the design pattern. Here is where all the Decoration is actually provided to an instance of the object.It contains an attribute of type Icecream. Once the instance is assigned using the constructor the instance method will be invoked.&lt;br /&gt;
&lt;br /&gt;
 abstract class IcecreamDecorator implements Icecream {&lt;br /&gt;
 &lt;br /&gt;
  protected Icecream specialIcecream;&lt;br /&gt;
 &lt;br /&gt;
  public IcecreamDecorator(Icecream specialIcecream) {&lt;br /&gt;
    this.specialIcecream = specialIcecream;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The NuttyDecorator and HoneyDecorator are the two concrete classes that implement the abstract decorator class IcecreamDecorator.&lt;br /&gt;
&lt;br /&gt;
 public class NuttyDecorator extends IcecreamDecorator {&lt;br /&gt;
 &lt;br /&gt;
  public NuttyDecorator(Icecream specialIcecream) {&lt;br /&gt;
    super(specialIcecream);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream() + addNuts();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private String addNuts() {&lt;br /&gt;
    return &amp;quot; + cruncy nuts&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HoneyDecorator extends IcecreamDecorator {&lt;br /&gt;
 &lt;br /&gt;
  public HoneyDecorator(Icecream specialIcecream) {&lt;br /&gt;
    super(specialIcecream);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream() + addHoney();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private String addHoney() {&lt;br /&gt;
    return &amp;quot; + sweet honey&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Decorator pattern in Java Library=====&lt;br /&gt;
* java.io.BufferedReader;&lt;br /&gt;
* java.io.FileReader;&lt;br /&gt;
* java.io.Reader;&lt;br /&gt;
&lt;br /&gt;
==== Adaptor Pattern vs Decorator Pattern====&lt;br /&gt;
* Adapter provides a different interface to its subject.&lt;br /&gt;
* Decorator provides an enhanced interface.&lt;br /&gt;
&lt;br /&gt;
http://sourcemaking.com/design_patterns/decorator&lt;br /&gt;
&lt;br /&gt;
=== Facade Pattern ===&lt;br /&gt;
&lt;br /&gt;
[[File:Wof.jpg|200 px|thumb|right|Without Facade-Pattern]]&lt;br /&gt;
[[File:Wf.jpg|200 px|thumb|right|With Facade-Pattern]]&lt;br /&gt;
[[File:FacadeUml.jpg|200 px|thumb|left|Microwave Design using Facade Pattern]]&lt;br /&gt;
GoF defines a facade design pattern as a pattern which , “Provides a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.”&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider a software component. In order to get the job done this component might have to expose a of interfaces so that it can interact with other components in a process flow. Facade pattern simplifies this process by providing an unified interface. It builds an additional layer of abstraction over the existing abstract layer to simplify the process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A microwave oven is made up of components like trasnformer, capacitor, magnetron, waveguide and some small other components. For a microwave to work all these different components needs to be executed in a sequence.  If all these components had separate interfaces it would have been hard and complicated.Hence  oven provides you buttons which can be considered as a facade. When you click on single button the job is done. That single button works as an abstraction layer between the user and the internal components.&lt;br /&gt;
&lt;br /&gt;
==== Facade Pattern in java library ====&lt;br /&gt;
ExternalContext Class in java performs cookie management by using an facade pattern. It makes use of components such as HttpSession, ServletContext, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse.&lt;br /&gt;
&lt;br /&gt;
==== Facade Pattern vs Adapter Pattern ====&lt;br /&gt;
* Adapter pattern is to make an incompatible class work with other class where as facade provides programming comfort by providing an unified interface.&lt;br /&gt;
* Adapter Pattern using inheritance increases inter dependencies whereas facade pattern provides loose coupling&lt;br /&gt;
* Changing data to suit the interface of a subsystem is done by facade whereas changing the structure of a system is done by adapter pattern&lt;br /&gt;
*  Facade increases subsystem independence and portability. This is not possible using the adapter pattern.&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w37_ms&amp;diff=70160</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w37 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w37_ms&amp;diff=70160"/>
		<updated>2012-11-18T15:04:38Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;''' Adapter pattern and the related patterns (Bridge, Decorator, Facade)'''&amp;lt;/big&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The adapter design pattern allows the user to make changes to the existing class with other class libraries without changing the code for the existing class. The Bridge, Decorator and the Facade pattern look somewhat similar to the adapter pattern but their intent is different and that intent is what separates the above patterns from each other.&lt;br /&gt;
&lt;br /&gt;
=== Adapter Pattern ===&lt;br /&gt;
&lt;br /&gt;
Adapter pattern plays an important role when you want to incompatible interfaces to work together.The real world example for an adapter pattern is the travel power adapter.Different countries have different socket and plug configuration. So you can use an adapter to fit a plug into a socket that initially was not possible due to different interface designs.&lt;br /&gt;
&lt;br /&gt;
==== Implementattion ====&lt;br /&gt;
&lt;br /&gt;
===== Adapter implementation using inheritance =====&lt;br /&gt;
&lt;br /&gt;
This method can be used when you have incompatible method that needs to be used in other class. Using inheritance a &amp;quot;is-a&amp;quot; relationship is established between the base class and super class. The new compatible methods will be contained in the inherited adapter class.&lt;br /&gt;
&lt;br /&gt;
 public class CylindricalSocket {&lt;br /&gt;
   public String supply(String cylinStem1, String cylinStem1) {&lt;br /&gt;
     System.out.println(&amp;quot;Power power power...&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularAdapter extends CylindricalSocket {&lt;br /&gt;
   public String adapt(String rectaStem1, Sting rectaStem2) {&lt;br /&gt;
     //some conversion logic&lt;br /&gt;
     String cylinStem1 = rectaStem1;&lt;br /&gt;
     String cylinStem2 = rectaStem2;&lt;br /&gt;
     return supply(cylinStem1, cylinStem2);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularPlug {&lt;br /&gt;
   private String rectaStem1;&lt;br /&gt;
   private String rectaStem2;&lt;br /&gt;
   public getPower() {&lt;br /&gt;
     RectangulrAdapter adapter = new RectangulrAdapter();&lt;br /&gt;
     String power = adapter.adapt(rectaStem1, rectaStem2);&lt;br /&gt;
     System.out.println(power);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===== Adapter implementation using composition =====&lt;br /&gt;
&lt;br /&gt;
The other approach is to have a base class as an attribute in the adapter class. This creates an &amp;quot;has-a&amp;quot;relationship between the base class and the sub class.&lt;br /&gt;
&lt;br /&gt;
 public class CylindricalSocket {&lt;br /&gt;
   public String supply(String cylinStem1, String cylinStem1) {&lt;br /&gt;
     System.out.println(&amp;quot;Power power power...&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularAdapter {&lt;br /&gt;
   private CylindricalSocket socket;&lt;br /&gt;
 &lt;br /&gt;
   public String adapt(String rectaStem1, Sting rectaStem2) {&lt;br /&gt;
     //some conversion logic&lt;br /&gt;
     socket = new CylindricalSocket();&lt;br /&gt;
     String cylinStem1 = rectaStem1;&lt;br /&gt;
     String cylinStem2 = rectaStem2;&lt;br /&gt;
     return socket.supply(cylinStem1, cylinStem2);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 public class RectangularPlug {&lt;br /&gt;
   private String rectaStem1;&lt;br /&gt;
   private String rectaStem2;&lt;br /&gt;
   public getPower() {&lt;br /&gt;
     RectangulrAdapter adapter = new RectangulrAdapter();&lt;br /&gt;
     String power = adapter.adapt(rectaStem1, rectaStem2);&lt;br /&gt;
     System.out.println(power);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===== Composition or inheritance to implement Adapter Pattern? =====&lt;br /&gt;
&lt;br /&gt;
* Composition is preferred over inheritance since using composition it is easy to change the behavior of a class.&lt;br /&gt;
&lt;br /&gt;
===== Adapter pattern in Java Library=====&lt;br /&gt;
* java.io.InputStreamReader(InputStream)&lt;br /&gt;
* java.io.OutputStreamWriter(OutputStream)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Bridge Pattern ===&lt;br /&gt;
As stated by GoF a bridge design patterns intent is to “Decouple an abstraction from its implementation so that the two can vary independently”.&lt;br /&gt;
&lt;br /&gt;
===== Elements of Bridge Design Pattern =====&lt;br /&gt;
* Abstraction: This is where the abstraction interface is defined.&lt;br /&gt;
&lt;br /&gt;
For example: A  Vehicle class which has a manufacture method.&lt;br /&gt;
 &lt;br /&gt;
* Refined Abstraction: Refined Abstraction extends the interface defined by Abstraction.&lt;br /&gt;
&lt;br /&gt;
For example, a Car class or a Bike class which has a manufacture method which is more refined than the Vehicle class.&lt;br /&gt;
 &lt;br /&gt;
* Implementor: It defines the interface for the implementation classes. It defines the basic operation.&lt;br /&gt;
&lt;br /&gt;
For example, the Workshop class acts as an implementor with the work() method.&lt;br /&gt;
&lt;br /&gt;
* Concrete Implementation: This implements the Implementor interface and gives it a more concrete implementation.&lt;br /&gt;
 &lt;br /&gt;
For example the Produce and the Assemble class with the work() method provides the concrete implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Bridge-Pattern.jpg|350 px|thumb|right|UML Diagram for Bridge-Pattern]]&lt;br /&gt;
[[File:WBP.jpg|350 px|thumb|left|Without Bridge-Pattern]][[File:bp.jpg|350 px|thumb|center|With Bridge-Pattern]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Example code for Bridge pattern =====&lt;br /&gt;
Vehicle Interface&lt;br /&gt;
&lt;br /&gt;
 // abstraction in bridge pattern&lt;br /&gt;
 abstract class Vehicle {&lt;br /&gt;
   protected Workshop workShop1;&lt;br /&gt;
   protected Workshop workShop2;&lt;br /&gt;
 &lt;br /&gt;
   protected Vehicle(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     this.workShop1 = workShop1;&lt;br /&gt;
     this.workShop2 = workShop2;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   abstract public void manufacture();&lt;br /&gt;
 }&lt;br /&gt;
Car class &lt;br /&gt;
&lt;br /&gt;
 //Refine abstraction 1 in bridge pattern&lt;br /&gt;
 public class Car extends Vehicle {&lt;br /&gt;
 &lt;br /&gt;
   public Car(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     super(workShop1, workShop2);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void manufacture() {&lt;br /&gt;
     System.out.print(&amp;quot;Car &amp;quot;);&lt;br /&gt;
     workShop1.work();&lt;br /&gt;
     workShop2.work();&lt;br /&gt;
 &lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Bike Class &lt;br /&gt;
&lt;br /&gt;
 // Refine abstraction 2 in bridge pattern&lt;br /&gt;
 public class Bike extends Vehicle {&lt;br /&gt;
 &lt;br /&gt;
   public Bike(Workshop workShop1, Workshop workShop2) {&lt;br /&gt;
     super(workShop1, workShop2);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void manufacture() {&lt;br /&gt;
     System.out.print(&amp;quot;Bike &amp;quot;);&lt;br /&gt;
     workShop1.work();&lt;br /&gt;
     workShop2.work();&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
Workshop Interface &lt;br /&gt;
 &lt;br /&gt;
  // Implementor for bridge pattern&lt;br /&gt;
 public interface Workshop {&lt;br /&gt;
   abstract public void work();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Produce Class &lt;br /&gt;
&lt;br /&gt;
 // Concrete implementation 1 for bridge pattern&lt;br /&gt;
 public class Produce implements Workshop {&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void work() {&lt;br /&gt;
     System.out.print(&amp;quot;Produced&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Assemble Class&lt;br /&gt;
   //Concrete implementation 2 for bridge pattern&lt;br /&gt;
&lt;br /&gt;
 public class Assemble implements Workshop {&lt;br /&gt;
 &lt;br /&gt;
   @Override&lt;br /&gt;
   public void work() {&lt;br /&gt;
     System.out.println(&amp;quot; Assembled.&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Demonstration of bridge design pattern&lt;br /&gt;
&lt;br /&gt;
 public class BridgePattern {&lt;br /&gt;
 &lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
 &lt;br /&gt;
     Vehicle vehicle1 = new Car(new Produce(), new Assemble());&lt;br /&gt;
     vehicle1.manufacture();&lt;br /&gt;
     Vehicle vehicle2 = new Bike(new Produce(), new Assemble());&lt;br /&gt;
     vehicle2.manufacture();&lt;br /&gt;
 &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
Output:&lt;br /&gt;
 Car Produced Assembled.&lt;br /&gt;
 Bike Produced Assembled.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Bridge vs Adapter pattern =====&lt;br /&gt;
* Two incompatable classes can be made to work together using adapter pattern.&lt;br /&gt;
* Bridge pattern creates two separate hierarchies by separating the abstraction from the implementation.&lt;br /&gt;
&lt;br /&gt;
While the two patterns may seem similar according to the GoF- &amp;quot;Adapter makes things work after they're designed; Bridge makes them work before they are. [GoF, p219]&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The adapter pattern is useful when we have code that is either third party or maybe in house, over which we do not have any control and such code do not fit with the interface that we wish to use. In such a case we use either of two (delegation or inheritance) model of the adapter pattern to change the existing classes (which do not fit our interface) and cast them to our desired interface.  &lt;br /&gt;
&lt;br /&gt;
The bridge pattern on the other hand is something we implement upfront on classes that have orthogonal hierarchies. It provides a way to decouple the interface and the implementation in such a way that you don't get an insane number of classes. In bridge pattern we usually deal with classes that vary in their implementations a lot and we want to incorporate the functionality of the implementation classes as much as possible. Bridge pattern is many times implemented with the help adapter pattern to give the orthogonal hierarchies  a common interface. &lt;br /&gt;
&lt;br /&gt;
For example &lt;br /&gt;
&lt;br /&gt;
We have a FileReader interface with two implementations. One that reads file from windows system and one that read Files from Linux systems. Now there are two more interface say MemoryMappedFiles and RADFiles. Now bridge pattern will help you in not having to write four classes  MemoryMappedWindowsFileReader, MemoryMappedLinuxFileReader, RADWindowsFileReader, RADWindowsFileReader. Here is how&lt;br /&gt;
&lt;br /&gt;
Use adapter pattern to adapt MemoryMappedFiles and RADFiles to a common interface (assuming both implementations have SpecialFile interface in common). Also WindowsFileReader and LinuxFileReader are two implementations of the FileReader interface.&lt;br /&gt;
&lt;br /&gt;
public interface CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   public void readFile(); // used for adapting&lt;br /&gt;
  &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SpecialWindowsFileReader extends WindowsFileReader implements CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   SpecialFile specialFile;&lt;br /&gt;
&lt;br /&gt;
   public SpecialWindowsFileReader(SpecialFile specialFile){&lt;br /&gt;
      this.specialFile = specialFile;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   public void readFile(){&lt;br /&gt;
      byte fileBytes[] = this.read(); // read platform specific and convert to bytes&lt;br /&gt;
      this.specialFile(fileBytes) // decouples from the filesystem specifics.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
SpecialLinuxFileReader extends LinuxFileReader implements CrossPlatformSpecialPurposeFileReader{&lt;br /&gt;
   SpecialFile specialFile;&lt;br /&gt;
&lt;br /&gt;
   public SpecialLinuxFileReader(SpecialFile specialFile){&lt;br /&gt;
      this.specialFile = specialFile;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
   public void readFile(){&lt;br /&gt;
      byte fileBytes[] = this.read(); // read platform specific and convert to bytes&lt;br /&gt;
      this.specialFile(fileBytes) // decouples from the filesystem specifics.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// bridge pattern&lt;br /&gt;
&lt;br /&gt;
public class Bridge{&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
     CrossPlatformSpecialPurposeFileReader specialFiles[] = new CrossPlatformSpecialPurposeFileReader[2];&lt;br /&gt;
     specialFiles[0] = new SpecialWindowsFileReader(new MemoryMappedFiles());&lt;br /&gt;
     specialFiles[1] = new SpecialLinuxFileReader(new RADFiles());&lt;br /&gt;
     // deouples two orthogonal implementations under one interface.&lt;br /&gt;
     for(CrossPlatformSpecialPurposeFileReader reader : specialFiles){&lt;br /&gt;
            reader.readFile();     &lt;br /&gt;
     }&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Decorator Pattern ===&lt;br /&gt;
&lt;br /&gt;
The decorator pattern adds responsibility to an object dynamically. The decorator design pattern is used to extend the behavior of an object dynamically. In order to extend the behavior we have to construct an wrapper around the object. Inheritance is not feasible since it is applied to an entire class. With the decorator pattern it is possible to select any particular instance and modify its behavior leaving the other instances unmodified.&lt;br /&gt;
&lt;br /&gt;
[[File:Decorator-Pattern.jpg|200 px|thumb|right|UML Diagram for Decorator-Pattern]]&lt;br /&gt;
==== Implementation of Decorator Pattern ====&lt;br /&gt;
First start with the interface which will be used by the class having the decoration design.&lt;br /&gt;
The example below explains how you first create a base ice cream and then decorate that ice cream by adding new toppings. The added topping changes the behavior of that ice cream. i.e. it gives the ice cream more taste.&lt;br /&gt;
&lt;br /&gt;
The interface below contains an makeIcecream() method which has not been implemented yet.&lt;br /&gt;
 public interface Icecream {&lt;br /&gt;
  public String makeIcecream();&lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
The SimpleIceCream Class provides an implementation to the makeIcecream() method. This is the class which acts as a base class on which decorations will be added.&lt;br /&gt;
&lt;br /&gt;
 public class SimpleIcecream implements Icecream {&lt;br /&gt;
 &lt;br /&gt;
  @Override&lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return &amp;quot;Base Icecream&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
The class below is the crux of the design pattern. Here is where all the Decoration is actually provided to an instance of the object.It contains an attribute of type Icecream. Once the instance is assigned using the constructor the instance method will be invoked.&lt;br /&gt;
&lt;br /&gt;
 abstract class IcecreamDecorator implements Icecream {&lt;br /&gt;
 &lt;br /&gt;
  protected Icecream specialIcecream;&lt;br /&gt;
 &lt;br /&gt;
  public IcecreamDecorator(Icecream specialIcecream) {&lt;br /&gt;
    this.specialIcecream = specialIcecream;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The NuttyDecorator and HoneyDecorator are the two concrete classes that implement the abstract decorator class IcecreamDecorator.&lt;br /&gt;
&lt;br /&gt;
 public class NuttyDecorator extends IcecreamDecorator {&lt;br /&gt;
 &lt;br /&gt;
  public NuttyDecorator(Icecream specialIcecream) {&lt;br /&gt;
    super(specialIcecream);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream() + addNuts();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private String addNuts() {&lt;br /&gt;
    return &amp;quot; + cruncy nuts&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 public class HoneyDecorator extends IcecreamDecorator {&lt;br /&gt;
 &lt;br /&gt;
  public HoneyDecorator(Icecream specialIcecream) {&lt;br /&gt;
    super(specialIcecream);&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  public String makeIcecream() {&lt;br /&gt;
    return specialIcecream.makeIcecream() + addHoney();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  private String addHoney() {&lt;br /&gt;
    return &amp;quot; + sweet honey&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Decorator pattern in Java Library=====&lt;br /&gt;
* java.io.BufferedReader;&lt;br /&gt;
* java.io.FileReader;&lt;br /&gt;
* java.io.Reader;&lt;br /&gt;
&lt;br /&gt;
==== Adaptor Pattern vs Decorator Pattern====&lt;br /&gt;
* Adapter provides a different interface to its subject.&lt;br /&gt;
* Decorator provides an enhanced interface.&lt;br /&gt;
&lt;br /&gt;
http://sourcemaking.com/design_patterns/decorator&lt;br /&gt;
&lt;br /&gt;
=== Facade Pattern ===&lt;br /&gt;
&lt;br /&gt;
[[File:Wof.jpg|200 px|thumb|right|Without Facade-Pattern]]&lt;br /&gt;
[[File:Wf.jpg|200 px|thumb|right|With Facade-Pattern]]&lt;br /&gt;
[[File:FacadeUml.jpg|200 px|thumb|left|Microwave Design using Facade Pattern]]&lt;br /&gt;
GoF defines a facade design pattern as a pattern which , “Provides a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.”&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider a software component. In order to get the job done this component might have to expose a of interfaces so that it can interact with other components in a process flow. Facade pattern simplifies this process by providing an unified interface. It builds an additional layer of abstraction over the existing abstract layer to simplify the process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A microwave oven is made up of components like trasnformer, capacitor, magnetron, waveguide and some small other components. For a microwave to work all these different components needs to be executed in a sequence.  If all these components had separate interfaces it would have been hard and complicated.Hence  oven provides you buttons which can be considered as a facade. When you click on single button the job is done. That single button works as an abstraction layer between the user and the internal components.&lt;br /&gt;
&lt;br /&gt;
==== Facade Pattern in java library ====&lt;br /&gt;
ExternalContext Class in java performs cookie management by using an facade pattern. It makes use of components such as HttpSession, ServletContext, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse.&lt;br /&gt;
&lt;br /&gt;
==== Facade Pattern vs Adapter Pattern ====&lt;br /&gt;
* Adapter pattern is to make an incompatible class work with other class where as facade provides programming comfort by providing an unified interface.&lt;br /&gt;
* Adapter Pattern using inheritance increases inter dependencies whereas facade pattern provides loose coupling&lt;br /&gt;
* Changing data to suit the interface of a subsystem is done by facade whereas changing the structure of a system is done by adapter pattern&lt;br /&gt;
*  Facade increases subsystem independence and portability. This is not possible using the adapter pattern.&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64951</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w19 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64951"/>
		<updated>2012-09-15T00:33:43Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
'''Rails''' is a MVC (Model-View-Controller) based architecture for web development based on the Ruby language. Rails 2[http://guides.rubyonrails.org/2_3_release_notes.html] was released in December 2007. Initially Rails 2 supported Ruby 1.8[http://www.ruby-lang.org/en/news/2008/05/31/ruby-1-8-7-has-been-released/]. It now has been updated to support Ruby 1.9.1[http://www.ruby-lang.org/en/news/2009/01/30/ruby-1-9-1-released/].  Rails 3 which brings in many improvements over Rails 2 supports Ruby 1.9 and above. The current stable version of Rails is 3.2.8[http://rubyonrails.org/download] released on Aug 9, 2012.&lt;br /&gt;
This wikibook chapter focuses mainly on the differences of Rails 2 and Rails 3.&lt;br /&gt;
&lt;br /&gt;
==Differences between Rails 2 and Rails 3==&lt;br /&gt;
==='''Unobtrusive Javascript'''[http://en.wikipedia.org/wiki/Unobtrusive_JavaScript]===&lt;br /&gt;
In previous versions of Rails, JavaScript was generated inline with HTML, causing ugly and somewhat brittle code.&lt;br /&gt;
As an example, Rails allows you to use its link_to method to generate a delete link for some object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;%= link_to &amp;quot;Delete this Post&amp;quot;, @post, :confirm =&amp;gt; &amp;quot;Do you really want to delete this post?&amp;quot;, :method =&amp;gt; :delete %&amp;gt;  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using this method in your view would generate the following in Rails 2:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;a href=&amp;quot;/posts/6&amp;quot; onclick=&amp;quot;if (confirm('Do you really want to delete this post?')) { var f = document.createElement('form');&lt;br /&gt;
       f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;&lt;br /&gt;
       var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');&lt;br /&gt;
       m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Rails 3 would generate something much simpler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;a href='/posts/6&amp;quot;' rel=&amp;quot;nofollow&amp;quot; data-method=&amp;quot;delete&amp;quot; data-confirm=&amp;quot;Do you really want to delete this post?&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Rails 3 replaces all of the inline JavaScript with a couple of HTML5 attributes. All of the JavaScript event handlers to handle the actual confirmation box and deletion are stored in one central JavaScript file that is included with every rails project.&lt;br /&gt;
&lt;br /&gt;
==='''No more dependency on only Prototype Javascript library'''===&lt;br /&gt;
One big advantage to this new method is that the JavaScript helpers are framework agnostic. Instead of being tied to the Prototype library like you were in Rails 2, you can now choose whatever JavaScript framework you like (Rails apps come with Prototype by default, but jQuery is now officially supported[https://github.com/rails/jquery-ujs].&lt;br /&gt;
&lt;br /&gt;
==='''Improved Security'''=== &lt;br /&gt;
Another awesome new feature of Rails 3 is that XSS[http://en.wikipedia.org/wiki/Cross-site_scripting] protection is now enabled by default. Rails 2 supported XSS protection through the use of the h method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;%= h @comment.text %&amp;gt;  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The h method would escape html and JavaScript to ensure that no malicious client-side code was executed. This method worked great, but there was one problem: you had to actually remember to use the h method everywhere the user entered input was displayed. If you forgot even one place, then you were vulnerable to an XSS attack.&lt;br /&gt;
In Rails 3, all input is escaped by default, taking the burden off of the developer of having to remember to escape everywhere that malicious code might be present. For those times that you do want to allow unescaped data to appear in your view, you can use the raw method to tell Rails 3 not to escape the data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= raw @comment.text %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==='''New Query Engine'''===&lt;br /&gt;
Rails 3 includes a cool new query engine that makes it easier to get back the data you want and gives you more flexibility in your controller code. These changes show up in various places, but the most common case is fetching data in your controller. In Rails 2, you could use the find method to retrieve the data you were looking for, passing in arguments to specify conditions, grouping, limits, and any other query information. For example:&lt;br /&gt;
&lt;br /&gt;
@posts = Post.find(:all, :conditions =&amp;gt; [ &amp;quot;category IN (?)&amp;quot;, categories], :limit =&amp;gt; 10, :order =&amp;gt; &amp;quot;created_on DESC&amp;quot;)  &lt;br /&gt;
&lt;br /&gt;
finds the first ten posts within some specified categories ordered by the creation time.&lt;br /&gt;
&lt;br /&gt;
In Rails 3, each of the passed in parameters has its own method, which can be chained together to get the same results.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories]).order(&amp;quot;created_on DESC&amp;quot;).limit(10)&lt;br /&gt;
&lt;br /&gt;
The query is not actually executed until the data is needed; so these methods can even be used across multiple statements.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories])&lt;br /&gt;
if(condition_a)&lt;br /&gt;
 @posts = @posts.where(['approved=?', true])&lt;br /&gt;
else&lt;br /&gt;
 @posts = @posts.where(['approved=?', false])&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
This is only a simple example, but should provide you with an idea of some of the ways this new syntax can be more useful.&lt;br /&gt;
&lt;br /&gt;
==='''Easier Email'''===&lt;br /&gt;
The ActionMailer module has been rewritten to make it a lot easier for your application to send email in Rails 3. &lt;br /&gt;
&lt;br /&gt;
'''1-Default Setting'''&lt;br /&gt;
In Rails, a Mailer is a class that can have many methods, each of which generally configure and send an email. Previously, you had to set all of the parameters for each email separately in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
def welcome_email(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;    # other paramters&lt;br /&gt;
end&lt;br /&gt;
 def password_reset(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;&lt;br /&gt;
    # other parameters&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
In Rails 3, you can specify defaults that can be optionally overwritten in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default :from =&amp;gt; 'no-reply@example.com',&lt;br /&gt;
           :return_path =&amp;gt; 'system@example.com'&lt;br /&gt;
&lt;br /&gt;
 def welcome_email(user)&lt;br /&gt;
    # no need to specify from parameter&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
'''2-Cleaner API'''&lt;br /&gt;
Previous versions of Rails required you to send email using special methods that were dynamically created by ActionMailer. For instance, if you wanted to deliver the welcome email in the example above, you would need to call:&lt;br /&gt;
&lt;br /&gt;
UserMailer.deliver_welcome_email(@user)  &lt;br /&gt;
&lt;br /&gt;
ln Rails 3, you can just call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;UserMailer.welcome_email(@user).deliver  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This makes more sense semantically, and additionally allows you to retrieve and manipulate the Mail object before delivering the email.&lt;br /&gt;
&lt;br /&gt;
==='''Dependency Management'''===&lt;br /&gt;
One of the strengths of the Ruby on Rails framework is the plethora of gems available for use by developers. Whether it's authentication[https://github.com/binarylogic/authlogic], &lt;br /&gt;
handling financial[http://activemerchant.org/] transactions[https://github.com/thoughtbot/paperclip], handling file uploads, or nearly anything else, chances are a gem exists to help with your problem.&lt;br /&gt;
&lt;br /&gt;
Issues can arise, however, if your gems require other gems, or developers are on different environments, among other things. To help solve these types of &lt;br /&gt;
situations, Rails 3 adds the Bundler[http://gembundler.com/] gem to help manage your dependencies. Using Bundler in Rails 3 is extremely simple; add a line for each gem you &lt;br /&gt;
require in your Gemfile, a file included in the root of each of your applications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;gem 'authlogic'&amp;lt;/b&amp;gt;&lt;br /&gt;
Once you've included all your gems, run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;bundle install&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and Bundler will download and configure all of the gems and their dependencies that you need for the project.&lt;br /&gt;
Bundler also allows you to specify certain gems to only be configured in certain environments (development vs production vs testing).&lt;br /&gt;
These are only a few of the many changes included in Ruby on Rails 3. Many of the old APIs still work in Rails, even if they've been deprecated, to make it easier to update. &lt;br /&gt;
&lt;br /&gt;
==='''ActiveRecord finder methods'''===&lt;br /&gt;
&amp;lt;b&amp;gt;In Rails 2&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.find(:all, : order =&amp;gt; &amp;quot;created_at desc&amp;quot;, :limit =&amp;gt; 5) &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 looks at the hash of options that is being passed to find(:all, : order, and :limit) and replace each item in the hash with an equivalent method.&lt;br /&gt;
Rails 3 has a &amp;lt;b&amp;gt;new API&amp;lt;/b&amp;gt; for the dynamic fiders&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;where, having, select, group, order, limit, offset, joins, includes, ...&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in Rails 3 it becomes&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.order(&amp;quot;created_at desc&amp;quot;).limit(5)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Active Record Validations'''===&lt;br /&gt;
There sre separate validations in Rails 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates_presence_of :username&lt;br /&gt;
&lt;br /&gt;
validates_uniqueness_of :username&lt;br /&gt;
&lt;br /&gt;
validates_length_of :email, :maximum =&amp;gt; 40 &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails 3 these can be clubbed together&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates :email, :presence =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:uniqueness =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:length =&amp;gt; {:maximum =&amp;gt; 30}&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html Rails 3 Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/2_3_release_notes.html Rails 2 Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://api.rubyonrails.org/ Rails API]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html#mail-extraction  Rails Mailer]&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64950</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w19 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64950"/>
		<updated>2012-09-15T00:32:51Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
'''Rails''' is a MVC (Model-View-Controller) based architecture for web development based on the Ruby language. Rails 2[http://guides.rubyonrails.org/2_3_release_notes.html] was released in December 2007. Initially Rails 2 supported Ruby 1.8[http://www.ruby-lang.org/en/news/2008/05/31/ruby-1-8-7-has-been-released/]. It now has been updated to support Ruby 1.9.1[http://www.ruby-lang.org/en/news/2009/01/30/ruby-1-9-1-released/].  Rails 3 which brings in many improvements over Rails 2 supports Ruby 1.9 and above. The current stable version of Rails is 3.2.8[http://rubyonrails.org/download] released on Aug 9, 2012.&lt;br /&gt;
This wikibook chapter focuses mainly on the differences of Rails 2 and Rails 3.&lt;br /&gt;
&lt;br /&gt;
==Differences between Rails 2 and Rails 3==&lt;br /&gt;
==='''Unobtrusive Javascript'''[http://en.wikipedia.org/wiki/Unobtrusive_JavaScript]===&lt;br /&gt;
In previous versions of Rails, JavaScript was generated inline with HTML, causing ugly and somewhat brittle code.&lt;br /&gt;
As an example, Rails allows you to use its link_to method to generate a delete link for some object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;%= link_to &amp;quot;Delete this Post&amp;quot;, @post, :confirm =&amp;gt; &amp;quot;Do you really want to delete this post?&amp;quot;, :method =&amp;gt; :delete %&amp;gt;  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using this method in your view would generate the following in Rails 2:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;a href=&amp;quot;/posts/6&amp;quot; onclick=&amp;quot;if (confirm('Do you really want to delete this post?')) { var f = document.createElement('form');&lt;br /&gt;
       f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;&lt;br /&gt;
       var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');&lt;br /&gt;
       m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Rails 3 would generate something much simpler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;a href='/posts/6&amp;quot;' rel=&amp;quot;nofollow&amp;quot; data-method=&amp;quot;delete&amp;quot; data-confirm=&amp;quot;Do you really want to delete this post?&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &amp;lt;/b&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Rails 3 replaces all of the inline JavaScript with a couple of HTML5 attributes. All of the JavaScript event handlers to handle the actual confirmation box and deletion are stored in one central JavaScript file that is included with every rails project.&lt;br /&gt;
&lt;br /&gt;
==='''No more dependency on only Prototype Javascript library'''===&lt;br /&gt;
One big advantage to this new method is that the JavaScript helpers are framework agnostic. Instead of being tied to the Prototype library like you were in Rails 2, you can now choose whatever JavaScript framework you like (Rails apps come with Prototype by default, but jQuery is now officially supported[https://github.com/rails/jquery-ujs].&lt;br /&gt;
&lt;br /&gt;
==='''Improved Security'''=== &lt;br /&gt;
Another awesome new feature of Rails 3 is that XSS[http://en.wikipedia.org/wiki/Cross-site_scripting] protection is now enabled by default. Rails 2 supported XSS protection through the use of the h method.&lt;br /&gt;
&amp;lt;%= h @comment.text %&amp;gt;  &lt;br /&gt;
The h method would escape html and JavaScript to ensure that no malicious client-side code was executed. This method worked great, but there was one problem: you had to actually remember to use the h method everywhere the user entered input was displayed. If you forgot even one place, then you were vulnerable to an XSS attack.&lt;br /&gt;
In Rails 3, all input is escaped by default, taking the burden off of the developer of having to remember to escape everywhere that malicious code might be present. For those times that you do want to allow unescaped data to appear in your view, you can use the raw method to tell Rails 3 not to escape the data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= raw @comment.text %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==='''New Query Engine'''===&lt;br /&gt;
Rails 3 includes a cool new query engine that makes it easier to get back the data you want and gives you more flexibility in your controller code. These changes show up in various places, but the most common case is fetching data in your controller. In Rails 2, you could use the find method to retrieve the data you were looking for, passing in arguments to specify conditions, grouping, limits, and any other query information. For example:&lt;br /&gt;
&lt;br /&gt;
@posts = Post.find(:all, :conditions =&amp;gt; [ &amp;quot;category IN (?)&amp;quot;, categories], :limit =&amp;gt; 10, :order =&amp;gt; &amp;quot;created_on DESC&amp;quot;)  &lt;br /&gt;
&lt;br /&gt;
finds the first ten posts within some specified categories ordered by the creation time.&lt;br /&gt;
&lt;br /&gt;
In Rails 3, each of the passed in parameters has its own method, which can be chained together to get the same results.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories]).order(&amp;quot;created_on DESC&amp;quot;).limit(10)&lt;br /&gt;
&lt;br /&gt;
The query is not actually executed until the data is needed; so these methods can even be used across multiple statements.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories])&lt;br /&gt;
if(condition_a)&lt;br /&gt;
 @posts = @posts.where(['approved=?', true])&lt;br /&gt;
else&lt;br /&gt;
 @posts = @posts.where(['approved=?', false])&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
This is only a simple example, but should provide you with an idea of some of the ways this new syntax can be more useful.&lt;br /&gt;
&lt;br /&gt;
==='''Easier Email'''===&lt;br /&gt;
The ActionMailer module has been rewritten to make it a lot easier for your application to send email in Rails 3. &lt;br /&gt;
&lt;br /&gt;
'''1-Default Setting'''&lt;br /&gt;
In Rails, a Mailer is a class that can have many methods, each of which generally configure and send an email. Previously, you had to set all of the parameters for each email separately in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
def welcome_email(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;    # other paramters&lt;br /&gt;
end&lt;br /&gt;
 def password_reset(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;&lt;br /&gt;
    # other parameters&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
In Rails 3, you can specify defaults that can be optionally overwritten in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default :from =&amp;gt; 'no-reply@example.com',&lt;br /&gt;
           :return_path =&amp;gt; 'system@example.com'&lt;br /&gt;
&lt;br /&gt;
 def welcome_email(user)&lt;br /&gt;
    # no need to specify from parameter&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
'''2-Cleaner API'''&lt;br /&gt;
Previous versions of Rails required you to send email using special methods that were dynamically created by ActionMailer. For instance, if you wanted to deliver the welcome email in the example above, you would need to call:&lt;br /&gt;
&lt;br /&gt;
UserMailer.deliver_welcome_email(@user)  &lt;br /&gt;
&lt;br /&gt;
ln Rails 3, you can just call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;UserMailer.welcome_email(@user).deliver  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This makes more sense semantically, and additionally allows you to retrieve and manipulate the Mail object before delivering the email.&lt;br /&gt;
&lt;br /&gt;
==='''Dependency Management'''===&lt;br /&gt;
One of the strengths of the Ruby on Rails framework is the plethora of gems available for use by developers. Whether it's authentication[https://github.com/binarylogic/authlogic], &lt;br /&gt;
handling financial[http://activemerchant.org/] transactions[https://github.com/thoughtbot/paperclip], handling file uploads, or nearly anything else, chances are a gem exists to help with your problem.&lt;br /&gt;
&lt;br /&gt;
Issues can arise, however, if your gems require other gems, or developers are on different environments, among other things. To help solve these types of &lt;br /&gt;
situations, Rails 3 adds the Bundler[http://gembundler.com/] gem to help manage your dependencies. Using Bundler in Rails 3 is extremely simple; add a line for each gem you &lt;br /&gt;
require in your Gemfile, a file included in the root of each of your applications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;gem 'authlogic'&amp;lt;/b&amp;gt;&lt;br /&gt;
Once you've included all your gems, run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;bundle install&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and Bundler will download and configure all of the gems and their dependencies that you need for the project.&lt;br /&gt;
Bundler also allows you to specify certain gems to only be configured in certain environments (development vs production vs testing).&lt;br /&gt;
These are only a few of the many changes included in Ruby on Rails 3. Many of the old APIs still work in Rails, even if they've been deprecated, to make it easier to update. &lt;br /&gt;
&lt;br /&gt;
==='''ActiveRecord finder methods'''===&lt;br /&gt;
&amp;lt;b&amp;gt;In Rails 2&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.find(:all, : order =&amp;gt; &amp;quot;created_at desc&amp;quot;, :limit =&amp;gt; 5) &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 looks at the hash of options that is being passed to find(:all, : order, and :limit) and replace each item in the hash with an equivalent method.&lt;br /&gt;
Rails 3 has a &amp;lt;b&amp;gt;new API&amp;lt;/b&amp;gt; for the dynamic fiders&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;where, having, select, group, order, limit, offset, joins, includes, ...&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in Rails 3 it becomes&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.order(&amp;quot;created_at desc&amp;quot;).limit(5)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Active Record Validations'''===&lt;br /&gt;
There sre separate validations in Rails 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates_presence_of :username&lt;br /&gt;
&lt;br /&gt;
validates_uniqueness_of :username&lt;br /&gt;
&lt;br /&gt;
validates_length_of :email, :maximum =&amp;gt; 40 &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails 3 these can be clubbed together&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates :email, :presence =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:uniqueness =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:length =&amp;gt; {:maximum =&amp;gt; 30}&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html Rails 3 Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/2_3_release_notes.html Rails 2 Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://api.rubyonrails.org/ Rails API]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html#mail-extraction  Rails Mailer]&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64945</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w19 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64945"/>
		<updated>2012-09-15T00:30:39Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
'''Rails''' is a MVC (Model-View-Controller) based architecture for web development based on the Ruby language. Rails 2[http://guides.rubyonrails.org/2_3_release_notes.html] was released in December 2007. Initially Rails 2 supported Ruby 1.8[http://www.ruby-lang.org/en/news/2008/05/31/ruby-1-8-7-has-been-released/]. It now has been updated to support Ruby 1.9.1[http://www.ruby-lang.org/en/news/2009/01/30/ruby-1-9-1-released/].  Rails 3 which brings in many improvements over Rails 2 supports Ruby 1.9 and above. The current stable version of Rails is 3.2.8[http://rubyonrails.org/download] released on Aug 9, 2012.&lt;br /&gt;
This wikibook chapter focuses mainly on the differences of Rails 2 and Rails 3.&lt;br /&gt;
&lt;br /&gt;
==Differences between Rails 2 and Rails 3==&lt;br /&gt;
==='''Unobtrusive Javascript'''[http://en.wikipedia.org/wiki/Unobtrusive_JavaScript]===&lt;br /&gt;
In previous versions of Rails, JavaScript was generated inline with HTML, causing ugly and somewhat brittle code.&lt;br /&gt;
As an example, Rails allows you to use its link_to method to generate a delete link for some object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Delete this Post&amp;quot;, @post, :confirm =&amp;gt; &amp;quot;Do you really want to delete this post?&amp;quot;, :method =&amp;gt; :delete %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Using this method in your view would generate the following in Rails 2:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;a href=&amp;quot;/posts/6&amp;quot; onclick=&amp;quot;if (confirm('Do you really want to delete this post?')) { var f = document.createElement('form');&lt;br /&gt;
       f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;&lt;br /&gt;
       var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');&lt;br /&gt;
       m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 would generate something much simpler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;a href='/posts/6&amp;quot;' rel=&amp;quot;nofollow&amp;quot; data-method=&amp;quot;delete&amp;quot; data-confirm=&amp;quot;Do you really want to delete this post?&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 replaces all of the inline JavaScript with a couple of HTML5 attributes. All of the JavaScript event handlers to handle the actual confirmation box and deletion are stored in one central JavaScript file that is included with every rails project.&lt;br /&gt;
&lt;br /&gt;
==='''No more dependency on only Prototype Javascript library'''===&lt;br /&gt;
One big advantage to this new method is that the JavaScript helpers are framework agnostic. Instead of being tied to the Prototype library like you were in Rails 2, you can now choose whatever JavaScript framework you like (Rails apps come with Prototype by default, but jQuery is now officially supported[https://github.com/rails/jquery-ujs].&lt;br /&gt;
&lt;br /&gt;
==='''Improved Security'''=== &lt;br /&gt;
Another awesome new feature of Rails 3 is that XSS[http://en.wikipedia.org/wiki/Cross-site_scripting] protection is now enabled by default. Rails 2 supported XSS protection through the use of the h method.&lt;br /&gt;
&amp;lt;%= h @comment.text %&amp;gt;  &lt;br /&gt;
The h method would escape html and JavaScript to ensure that no malicious client-side code was executed. This method worked great, but there was one problem: you had to actually remember to use the h method everywhere the user entered input was displayed. If you forgot even one place, then you were vulnerable to an XSS attack.&lt;br /&gt;
In Rails 3, all input is escaped by default, taking the burden off of the developer of having to remember to escape everywhere that malicious code might be present. For those times that you do want to allow unescaped data to appear in your view, you can use the raw method to tell Rails 3 not to escape the data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= raw @comment.text %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==='''New Query Engine'''===&lt;br /&gt;
Rails 3 includes a cool new query engine that makes it easier to get back the data you want and gives you more flexibility in your controller code. These changes show up in various places, but the most common case is fetching data in your controller. In Rails 2, you could use the find method to retrieve the data you were looking for, passing in arguments to specify conditions, grouping, limits, and any other query information. For example:&lt;br /&gt;
&lt;br /&gt;
@posts = Post.find(:all, :conditions =&amp;gt; [ &amp;quot;category IN (?)&amp;quot;, categories], :limit =&amp;gt; 10, :order =&amp;gt; &amp;quot;created_on DESC&amp;quot;)  &lt;br /&gt;
&lt;br /&gt;
finds the first ten posts within some specified categories ordered by the creation time.&lt;br /&gt;
&lt;br /&gt;
In Rails 3, each of the passed in parameters has its own method, which can be chained together to get the same results.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories]).order(&amp;quot;created_on DESC&amp;quot;).limit(10)&lt;br /&gt;
&lt;br /&gt;
The query is not actually executed until the data is needed; so these methods can even be used across multiple statements.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories])&lt;br /&gt;
if(condition_a)&lt;br /&gt;
 @posts = @posts.where(['approved=?', true])&lt;br /&gt;
else&lt;br /&gt;
 @posts = @posts.where(['approved=?', false])&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
This is only a simple example, but should provide you with an idea of some of the ways this new syntax can be more useful.&lt;br /&gt;
&lt;br /&gt;
==='''Easier Email'''===&lt;br /&gt;
The ActionMailer module has been rewritten to make it a lot easier for your application to send email in Rails 3. &lt;br /&gt;
&lt;br /&gt;
'''1-Default Setting'''&lt;br /&gt;
In Rails, a Mailer is a class that can have many methods, each of which generally configure and send an email. Previously, you had to set all of the parameters for each email separately in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
def welcome_email(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;    # other paramters&lt;br /&gt;
end&lt;br /&gt;
 def password_reset(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;&lt;br /&gt;
    # other parameters&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
In Rails 3, you can specify defaults that can be optionally overwritten in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default :from =&amp;gt; 'no-reply@example.com',&lt;br /&gt;
           :return_path =&amp;gt; 'system@example.com'&lt;br /&gt;
&lt;br /&gt;
 def welcome_email(user)&lt;br /&gt;
    # no need to specify from parameter&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
'''2-Cleaner API'''&lt;br /&gt;
Previous versions of Rails required you to send email using special methods that were dynamically created by ActionMailer. For instance, if you wanted to deliver the welcome email in the example above, you would need to call:&lt;br /&gt;
&lt;br /&gt;
UserMailer.deliver_welcome_email(@user)  &lt;br /&gt;
&lt;br /&gt;
ln Rails 3, you can just call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;UserMailer.welcome_email(@user).deliver  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This makes more sense semantically, and additionally allows you to retrieve and manipulate the Mail object before delivering the email.&lt;br /&gt;
&lt;br /&gt;
==='''Dependency Management'''===&lt;br /&gt;
One of the strengths of the Ruby on Rails framework is the plethora of gems available for use by developers. Whether it's authentication[https://github.com/binarylogic/authlogic], &lt;br /&gt;
handling financial[http://activemerchant.org/] transactions[https://github.com/thoughtbot/paperclip], handling file uploads, or nearly anything else, chances are a gem exists to help with your problem.&lt;br /&gt;
&lt;br /&gt;
Issues can arise, however, if your gems require other gems, or developers are on different environments, among other things. To help solve these types of &lt;br /&gt;
situations, Rails 3 adds the Bundler[http://gembundler.com/] gem to help manage your dependencies. Using Bundler in Rails 3 is extremely simple; add a line for each gem you &lt;br /&gt;
require in your Gemfile, a file included in the root of each of your applications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;gem 'authlogic'&amp;lt;/b&amp;gt;&lt;br /&gt;
Once you've included all your gems, run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;bundle install&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and Bundler will download and configure all of the gems and their dependencies that you need for the project.&lt;br /&gt;
Bundler also allows you to specify certain gems to only be configured in certain environments (development vs production vs testing).&lt;br /&gt;
These are only a few of the many changes included in Ruby on Rails 3. Many of the old APIs still work in Rails, even if they've been deprecated, to make it easier to update. &lt;br /&gt;
&lt;br /&gt;
==='''ActiveRecord finder methods'''===&lt;br /&gt;
&amp;lt;b&amp;gt;In Rails 2&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.find(:all, : order =&amp;gt; &amp;quot;created_at desc&amp;quot;, :limit =&amp;gt; 5) &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 looks at the hash of options that is being passed to find(:all, : order, and :limit) and replace each item in the hash with an equivalent method.&lt;br /&gt;
Rails 3 has a &amp;lt;b&amp;gt;new API&amp;lt;/b&amp;gt; for the dynamic fiders&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;where, having, select, group, order, limit, offset, joins, includes, ...&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in Rails 3 it becomes&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.order(&amp;quot;created_at desc&amp;quot;).limit(5)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Active Record Validations'''===&lt;br /&gt;
There sre separate validations in Rails 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates_presence_of :username&lt;br /&gt;
&lt;br /&gt;
validates_uniqueness_of :username&lt;br /&gt;
&lt;br /&gt;
validates_length_of :email, :maximum =&amp;gt; 40 &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails 3 these can be clubbed together&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates :email, :presence =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:uniqueness =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:length =&amp;gt; {:maximum =&amp;gt; 30}&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html Rails 3 Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/2_3_release_notes.html Rails 2 Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://api.rubyonrails.org/ Rails API]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html#mail-extraction  Rails Mailer]&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64944</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w19 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64944"/>
		<updated>2012-09-15T00:29:59Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
'''Rails''' is a MVC (Model-View-Controller) based architecture for web development based on the Ruby language. Rails 2[http://guides.rubyonrails.org/2_3_release_notes.html] was released in December 2007. Initially Rails 2 supported Ruby 1.8[http://www.ruby-lang.org/en/news/2008/05/31/ruby-1-8-7-has-been-released/]. It now has been updated to support Ruby 1.9.1[http://www.ruby-lang.org/en/news/2009/01/30/ruby-1-9-1-released/].  Rails 3 which brings in many improvements over Rails 2 supports Ruby 1.9 and above. The current stable version of Rails is 3.2.8[http://rubyonrails.org/download] released on Aug 9, 2012.&lt;br /&gt;
This wikibook chapter focuses mainly on the differences of Rails 2 and Rails 3.&lt;br /&gt;
&lt;br /&gt;
==Differences between Rails 2 and Rails 3==&lt;br /&gt;
==='''Unobtrusive Javascript'''[http://en.wikipedia.org/wiki/Unobtrusive_JavaScript]===&lt;br /&gt;
In previous versions of Rails, JavaScript was generated inline with HTML, causing ugly and somewhat brittle code.&lt;br /&gt;
As an example, Rails allows you to use its link_to method to generate a delete link for some object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Delete this Post&amp;quot;, @post, :confirm =&amp;gt; &amp;quot;Do you really want to delete this post?&amp;quot;, :method =&amp;gt; :delete %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Using this method in your view would generate the following in Rails 2:&lt;br /&gt;
&amp;lt;a href=&amp;quot;/posts/6&amp;quot; onclick=&amp;quot;if (confirm('Do you really want to delete this post?')) { var f = document.createElement('form');&lt;br /&gt;
       f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;&lt;br /&gt;
       var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');&lt;br /&gt;
       m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 would generate something much simpler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;a href='/posts/6&amp;quot;' rel=&amp;quot;nofollow&amp;quot; data-method=&amp;quot;delete&amp;quot; data-confirm=&amp;quot;Do you really want to delete this post?&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 replaces all of the inline JavaScript with a couple of HTML5 attributes. All of the JavaScript event handlers to handle the actual confirmation box and deletion are stored in one central JavaScript file that is included with every rails project.&lt;br /&gt;
&lt;br /&gt;
==='''No more dependency on only Prototype Javascript library'''===&lt;br /&gt;
One big advantage to this new method is that the JavaScript helpers are framework agnostic. Instead of being tied to the Prototype library like you were in Rails 2, you can now choose whatever JavaScript framework you like (Rails apps come with Prototype by default, but jQuery is now officially supported[https://github.com/rails/jquery-ujs].&lt;br /&gt;
&lt;br /&gt;
==='''Improved Security'''=== &lt;br /&gt;
Another awesome new feature of Rails 3 is that XSS[http://en.wikipedia.org/wiki/Cross-site_scripting] protection is now enabled by default. Rails 2 supported XSS protection through the use of the h method.&lt;br /&gt;
&amp;lt;%= h @comment.text %&amp;gt;  &lt;br /&gt;
The h method would escape html and JavaScript to ensure that no malicious client-side code was executed. This method worked great, but there was one problem: you had to actually remember to use the h method everywhere the user entered input was displayed. If you forgot even one place, then you were vulnerable to an XSS attack.&lt;br /&gt;
In Rails 3, all input is escaped by default, taking the burden off of the developer of having to remember to escape everywhere that malicious code might be present. For those times that you do want to allow unescaped data to appear in your view, you can use the raw method to tell Rails 3 not to escape the data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= raw @comment.text %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==='''New Query Engine'''===&lt;br /&gt;
Rails 3 includes a cool new query engine that makes it easier to get back the data you want and gives you more flexibility in your controller code. These changes show up in various places, but the most common case is fetching data in your controller. In Rails 2, you could use the find method to retrieve the data you were looking for, passing in arguments to specify conditions, grouping, limits, and any other query information. For example:&lt;br /&gt;
&lt;br /&gt;
@posts = Post.find(:all, :conditions =&amp;gt; [ &amp;quot;category IN (?)&amp;quot;, categories], :limit =&amp;gt; 10, :order =&amp;gt; &amp;quot;created_on DESC&amp;quot;)  &lt;br /&gt;
&lt;br /&gt;
finds the first ten posts within some specified categories ordered by the creation time.&lt;br /&gt;
&lt;br /&gt;
In Rails 3, each of the passed in parameters has its own method, which can be chained together to get the same results.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories]).order(&amp;quot;created_on DESC&amp;quot;).limit(10)&lt;br /&gt;
&lt;br /&gt;
The query is not actually executed until the data is needed; so these methods can even be used across multiple statements.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories])&lt;br /&gt;
if(condition_a)&lt;br /&gt;
 @posts = @posts.where(['approved=?', true])&lt;br /&gt;
else&lt;br /&gt;
 @posts = @posts.where(['approved=?', false])&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
This is only a simple example, but should provide you with an idea of some of the ways this new syntax can be more useful.&lt;br /&gt;
&lt;br /&gt;
==='''Easier Email'''===&lt;br /&gt;
The ActionMailer module has been rewritten to make it a lot easier for your application to send email in Rails 3. &lt;br /&gt;
&lt;br /&gt;
'''1-Default Setting'''&lt;br /&gt;
In Rails, a Mailer is a class that can have many methods, each of which generally configure and send an email. Previously, you had to set all of the parameters for each email separately in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
def welcome_email(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;    # other paramters&lt;br /&gt;
end&lt;br /&gt;
 def password_reset(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;&lt;br /&gt;
    # other parameters&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
In Rails 3, you can specify defaults that can be optionally overwritten in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default :from =&amp;gt; 'no-reply@example.com',&lt;br /&gt;
           :return_path =&amp;gt; 'system@example.com'&lt;br /&gt;
&lt;br /&gt;
 def welcome_email(user)&lt;br /&gt;
    # no need to specify from parameter&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
'''2-Cleaner API'''&lt;br /&gt;
Previous versions of Rails required you to send email using special methods that were dynamically created by ActionMailer. For instance, if you wanted to deliver the welcome email in the example above, you would need to call:&lt;br /&gt;
&lt;br /&gt;
UserMailer.deliver_welcome_email(@user)  &lt;br /&gt;
&lt;br /&gt;
ln Rails 3, you can just call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;UserMailer.welcome_email(@user).deliver  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This makes more sense semantically, and additionally allows you to retrieve and manipulate the Mail object before delivering the email.&lt;br /&gt;
&lt;br /&gt;
==='''Dependency Management'''===&lt;br /&gt;
One of the strengths of the Ruby on Rails framework is the plethora of gems available for use by developers. Whether it's authentication[https://github.com/binarylogic/authlogic], &lt;br /&gt;
handling financial[http://activemerchant.org/] transactions[https://github.com/thoughtbot/paperclip], handling file uploads, or nearly anything else, chances are a gem exists to help with your problem.&lt;br /&gt;
&lt;br /&gt;
Issues can arise, however, if your gems require other gems, or developers are on different environments, among other things. To help solve these types of &lt;br /&gt;
situations, Rails 3 adds the Bundler[http://gembundler.com/] gem to help manage your dependencies. Using Bundler in Rails 3 is extremely simple; add a line for each gem you &lt;br /&gt;
require in your Gemfile, a file included in the root of each of your applications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;gem 'authlogic'&amp;lt;/b&amp;gt;&lt;br /&gt;
Once you've included all your gems, run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;bundle install&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and Bundler will download and configure all of the gems and their dependencies that you need for the project.&lt;br /&gt;
Bundler also allows you to specify certain gems to only be configured in certain environments (development vs production vs testing).&lt;br /&gt;
These are only a few of the many changes included in Ruby on Rails 3. Many of the old APIs still work in Rails, even if they've been deprecated, to make it easier to update. &lt;br /&gt;
&lt;br /&gt;
==='''ActiveRecord finder methods'''===&lt;br /&gt;
&amp;lt;b&amp;gt;In Rails 2&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.find(:all, : order =&amp;gt; &amp;quot;created_at desc&amp;quot;, :limit =&amp;gt; 5) &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 looks at the hash of options that is being passed to find(:all, : order, and :limit) and replace each item in the hash with an equivalent method.&lt;br /&gt;
Rails 3 has a &amp;lt;b&amp;gt;new API&amp;lt;/b&amp;gt; for the dynamic fiders&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;where, having, select, group, order, limit, offset, joins, includes, ...&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in Rails 3 it becomes&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.order(&amp;quot;created_at desc&amp;quot;).limit(5)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Active Record Validations'''===&lt;br /&gt;
There sre separate validations in Rails 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates_presence_of :username&lt;br /&gt;
&lt;br /&gt;
validates_uniqueness_of :username&lt;br /&gt;
&lt;br /&gt;
validates_length_of :email, :maximum =&amp;gt; 40 &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails 3 these can be clubbed together&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates :email, :presence =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:uniqueness =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:length =&amp;gt; {:maximum =&amp;gt; 30}&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html Rails 3 Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/2_3_release_notes.html Rails 2 Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://api.rubyonrails.org/ Rails API]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html#mail-extraction  Rails Mailer]&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64941</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w19 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64941"/>
		<updated>2012-09-15T00:28:43Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
'''Rails''' is a MVC (Model-View-Controller) based architecture for web development based on the Ruby language. Rails 2[http://guides.rubyonrails.org/2_3_release_notes.html] was released in December 2007. Initially Rails 2 supported Ruby 1.8[http://www.ruby-lang.org/en/news/2008/05/31/ruby-1-8-7-has-been-released/]. It now has been updated to support Ruby 1.9.1[http://www.ruby-lang.org/en/news/2009/01/30/ruby-1-9-1-released/].  Rails 3 which brings in many improvements over Rails 2 supports Ruby 1.9 and above. The current stable version of Rails is 3.2.8[http://rubyonrails.org/download] released on Aug 9, 2012.&lt;br /&gt;
This wikibook chapter focuses mainly on the differences of Rails 2 and Rails 3.&lt;br /&gt;
&lt;br /&gt;
==Differences between Rails 2 and Rails 3==&lt;br /&gt;
==='''Unobtrusive Javascript'''[http://en.wikipedia.org/wiki/Unobtrusive_JavaScript]===&lt;br /&gt;
In previous versions of Rails, JavaScript was generated inline with HTML, causing ugly and somewhat brittle code.&lt;br /&gt;
As an example, Rails allows you to use its link_to method to generate a delete link for some object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Delete this Post&amp;quot;, @post, :confirm =&amp;gt; &amp;quot;Do you really want to delete this post?&amp;quot;, :method =&amp;gt; :delete %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Using this method in your view would generate the following in Rails 2:&lt;br /&gt;
&amp;lt;a href=&amp;quot;/posts/6&amp;quot; onclick=&amp;quot;if (confirm('Do you really want to delete this post?')) { var f = document.createElement('form');&lt;br /&gt;
       f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;&lt;br /&gt;
       var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');&lt;br /&gt;
       m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 would generate something much simpler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;a href='/posts/6&amp;quot;' rel=&amp;quot;nofollow&amp;quot; data-method=&amp;quot;delete&amp;quot; data-confirm=&amp;quot;Do you really want to delete this post?&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 replaces all of the inline JavaScript with a couple of HTML5 attributes. All of the JavaScript event handlers to handle the actual confirmation box and deletion are stored in one central JavaScript file that is included with every rails project.&lt;br /&gt;
&lt;br /&gt;
==='''No more dependency on only Prototype Javascript library'''===&lt;br /&gt;
One big advantage to this new method is that the JavaScript helpers are framework agnostic. Instead of being tied to the Prototype library like you were in Rails 2, you can now choose whatever JavaScript framework you like (Rails apps come with Prototype by default, but jQuery is now officially supported[https://github.com/rails/jquery-ujs].&lt;br /&gt;
&lt;br /&gt;
==='''Improved Security'''=== &lt;br /&gt;
Another awesome new feature of Rails 3 is that XSS[http://en.wikipedia.org/wiki/Cross-site_scripting] protection is now enabled by default. Rails 2 supported XSS protection through the use of the h method.&lt;br /&gt;
&amp;lt;%= h @comment.text %&amp;gt;  &lt;br /&gt;
The h method would escape html and JavaScript to ensure that no malicious client-side code was executed. This method worked great, but there was one problem: you had to actually remember to use the h method everywhere the user entered input was displayed. If you forgot even one place, then you were vulnerable to an XSS attack.&lt;br /&gt;
In Rails 3, all input is escaped by default, taking the burden off of the developer of having to remember to escape everywhere that malicious code might be present. For those times that you do want to allow unescaped data to appear in your view, you can use the raw method to tell Rails 3 not to escape the data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= raw @comment.text %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==='''New Query Engine'''===&lt;br /&gt;
Rails 3 includes a cool new query engine that makes it easier to get back the data you want and gives you more flexibility in your controller code. These changes show up in various places, but the most common case is fetching data in your controller. In Rails 2, you could use the find method to retrieve the data you were looking for, passing in arguments to specify conditions, grouping, limits, and any other query information. For example:&lt;br /&gt;
&lt;br /&gt;
@posts = Post.find(:all, :conditions =&amp;gt; [ &amp;quot;category IN (?)&amp;quot;, categories], :limit =&amp;gt; 10, :order =&amp;gt; &amp;quot;created_on DESC&amp;quot;)  &lt;br /&gt;
&lt;br /&gt;
finds the first ten posts within some specified categories ordered by the creation time.&lt;br /&gt;
&lt;br /&gt;
In Rails 3, each of the passed in parameters has its own method, which can be chained together to get the same results.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories]).order(&amp;quot;created_on DESC&amp;quot;).limit(10)&lt;br /&gt;
&lt;br /&gt;
The query is not actually executed until the data is needed; so these methods can even be used across multiple statements.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories])&lt;br /&gt;
if(condition_a)&lt;br /&gt;
 @posts = @posts.where(['approved=?', true])&lt;br /&gt;
else&lt;br /&gt;
 @posts = @posts.where(['approved=?', false])&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
This is only a simple example, but should provide you with an idea of some of the ways this new syntax can be more useful.&lt;br /&gt;
&lt;br /&gt;
==='''Easier Email'''===&lt;br /&gt;
The ActionMailer module has been rewritten to make it a lot easier for your application to send email in Rails 3. &lt;br /&gt;
&lt;br /&gt;
'''1-Default Setting'''&lt;br /&gt;
In Rails, a Mailer is a class that can have many methods, each of which generally configure and send an email. Previously, you had to set all of the parameters for each email separately in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
def welcome_email(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;    # other paramters&lt;br /&gt;
end&lt;br /&gt;
 def password_reset(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;&lt;br /&gt;
    # other parameters&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
In Rails 3, you can specify defaults that can be optionally overwritten in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default :from =&amp;gt; 'no-reply@example.com',&lt;br /&gt;
           :return_path =&amp;gt; 'system@example.com'&lt;br /&gt;
&lt;br /&gt;
 def welcome_email(user)&lt;br /&gt;
    # no need to specify from parameter&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
'''2-Cleaner API'''&lt;br /&gt;
Previous versions of Rails required you to send email using special methods that were dynamically created by ActionMailer. For instance, if you wanted to deliver the welcome email in the example above, you would need to call:&lt;br /&gt;
&lt;br /&gt;
UserMailer.deliver_welcome_email(@user)  &lt;br /&gt;
&lt;br /&gt;
ln Rails 3, you can just call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;UserMailer.welcome_email(@user).deliver  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This makes more sense semantically, and additionally allows you to retrieve and manipulate the Mail object before delivering the email.&lt;br /&gt;
&lt;br /&gt;
==='''Dependency Management'''===&lt;br /&gt;
One of the strengths of the Ruby on Rails framework is the plethora of gems available for use by developers. Whether it's authentication[https://github.com/binarylogic/authlogic], &lt;br /&gt;
handling financial[http://activemerchant.org/] transactions[https://github.com/thoughtbot/paperclip], handling file uploads, or nearly anything else, chances are a gem exists to help with your problem.&lt;br /&gt;
&lt;br /&gt;
Issues can arise, however, if your gems require other gems, or developers are on different environments, among other things. To help solve these types of &lt;br /&gt;
situations, Rails 3 adds the Bundler[http://gembundler.com/] gem to help manage your dependencies. Using Bundler in Rails 3 is extremely simple; add a line for each gem you &lt;br /&gt;
require in your Gemfile, a file included in the root of each of your applications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;gem 'authlogic'&amp;lt;/b&amp;gt;&lt;br /&gt;
Once you've included all your gems, run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;bundle install&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and Bundler will download and configure all of the gems and their dependencies that you need for the project.&lt;br /&gt;
Bundler also allows you to specify certain gems to only be configured in certain environments (development vs production vs testing).&lt;br /&gt;
These are only a few of the many changes included in Ruby on Rails 3. Many of the old APIs still work in Rails, even if they've been deprecated, to make it easier to update. &lt;br /&gt;
&lt;br /&gt;
==='''ActiveRecord finder methods'''===&lt;br /&gt;
&amp;lt;b&amp;gt;In Rails 2&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.find(:all, : order =&amp;gt; &amp;quot;created_at desc&amp;quot;, :limit =&amp;gt; 5) &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 looks at the hash of options that is being passed to find(:all, : order, and :limit) and replace each item in the hash with an equivalent method.&lt;br /&gt;
Rails 3 has a &amp;lt;b&amp;gt;new API&amp;lt;/b&amp;gt; for the dynamic fiders&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;where, having, select, group, order, limit, offset, joins, includes, ...&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in Rails 3 it becomes&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.order(&amp;quot;created_at desc&amp;quot;).limit(5)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Active Record Validations'''===&lt;br /&gt;
There sre separate validations in Rails 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates_presence_of :username&lt;br /&gt;
&lt;br /&gt;
validates_uniqueness_of :username&lt;br /&gt;
&lt;br /&gt;
validates_length_of :email, :maximum =&amp;gt; 40 &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails 3 these can be clubbed together&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates :email, :presence =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:uniqueness =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:length =&amp;gt; {:maximum =&amp;gt; 30}&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html Rails 3 Documentation]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/2_3_release_notes.html]&lt;br /&gt;
&lt;br /&gt;
[http://api.rubyonrails.org/]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html#mail-extraction]&lt;br /&gt;
&lt;br /&gt;
[http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3]&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64929</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w19 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64929"/>
		<updated>2012-09-15T00:26:01Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
'''Rails''' is a MVC (Model-View-Controller) based architecture for web development based on the Ruby language. Rails 2[http://guides.rubyonrails.org/2_3_release_notes.html] was released in December 2007. Initially Rails 2 supported Ruby 1.8[http://www.ruby-lang.org/en/news/2008/05/31/ruby-1-8-7-has-been-released/]. It now has been updated to support Ruby 1.9.1[http://www.ruby-lang.org/en/news/2009/01/30/ruby-1-9-1-released/].  Rails 3 which brings in many improvements over Rails 2 supports Ruby 1.9 and above. The current stable version of Rails is 3.2.8[http://rubyonrails.org/download] released on Aug 9, 2012.&lt;br /&gt;
This wikibook chapter focuses mainly on the differences of Rails 2 and Rails 3.&lt;br /&gt;
&lt;br /&gt;
==Differences between Rails 2 and Rails 3==&lt;br /&gt;
==='''Unobtrusive Javascript'''[http://en.wikipedia.org/wiki/Unobtrusive_JavaScript]===&lt;br /&gt;
In previous versions of Rails, JavaScript was generated inline with HTML, causing ugly and somewhat brittle code.&lt;br /&gt;
As an example, Rails allows you to use its link_to method to generate a delete link for some object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Delete this Post&amp;quot;, @post, :confirm =&amp;gt; &amp;quot;Do you really want to delete this post?&amp;quot;, :method =&amp;gt; :delete %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Using this method in your view would generate the following in Rails 2:&lt;br /&gt;
&amp;lt;a href=&amp;quot;/posts/6&amp;quot; onclick=&amp;quot;if (confirm('Do you really want to delete this post?')) { var f = document.createElement('form');&lt;br /&gt;
       f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;&lt;br /&gt;
       var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');&lt;br /&gt;
       m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 would generate something much simpler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;a href='/posts/6&amp;quot;' rel=&amp;quot;nofollow&amp;quot; data-method=&amp;quot;delete&amp;quot; data-confirm=&amp;quot;Do you really want to delete this post?&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 replaces all of the inline JavaScript with a couple of HTML5 attributes. All of the JavaScript event handlers to handle the actual confirmation box and deletion are stored in one central JavaScript file that is included with every rails project.&lt;br /&gt;
&lt;br /&gt;
==='''No more dependency on only Prototype Javascript library'''===&lt;br /&gt;
One big advantage to this new method is that the JavaScript helpers are framework agnostic. Instead of being tied to the Prototype library like you were in Rails 2, you can now choose whatever JavaScript framework you like (Rails apps come with Prototype by default, but jQuery is now officially supported[https://github.com/rails/jquery-ujs].&lt;br /&gt;
&lt;br /&gt;
==='''Improved Security'''=== &lt;br /&gt;
Another awesome new feature of Rails 3 is that XSS[http://en.wikipedia.org/wiki/Cross-site_scripting] protection is now enabled by default. Rails 2 supported XSS protection through the use of the h method.&lt;br /&gt;
&amp;lt;%= h @comment.text %&amp;gt;  &lt;br /&gt;
The h method would escape html and JavaScript to ensure that no malicious client-side code was executed. This method worked great, but there was one problem: you had to actually remember to use the h method everywhere the user entered input was displayed. If you forgot even one place, then you were vulnerable to an XSS attack.&lt;br /&gt;
In Rails 3, all input is escaped by default, taking the burden off of the developer of having to remember to escape everywhere that malicious code might be present. For those times that you do want to allow unescaped data to appear in your view, you can use the raw method to tell Rails 3 not to escape the data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= raw @comment.text %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==='''New Query Engine'''===&lt;br /&gt;
Rails 3 includes a cool new query engine that makes it easier to get back the data you want and gives you more flexibility in your controller code. These changes show up in various places, but the most common case is fetching data in your controller. In Rails 2, you could use the find method to retrieve the data you were looking for, passing in arguments to specify conditions, grouping, limits, and any other query information. For example:&lt;br /&gt;
&lt;br /&gt;
@posts = Post.find(:all, :conditions =&amp;gt; [ &amp;quot;category IN (?)&amp;quot;, categories], :limit =&amp;gt; 10, :order =&amp;gt; &amp;quot;created_on DESC&amp;quot;)  &lt;br /&gt;
&lt;br /&gt;
finds the first ten posts within some specified categories ordered by the creation time.&lt;br /&gt;
&lt;br /&gt;
In Rails 3, each of the passed in parameters has its own method, which can be chained together to get the same results.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories]).order(&amp;quot;created_on DESC&amp;quot;).limit(10)&lt;br /&gt;
&lt;br /&gt;
The query is not actually executed until the data is needed; so these methods can even be used across multiple statements.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories])&lt;br /&gt;
if(condition_a)&lt;br /&gt;
 @posts = @posts.where(['approved=?', true])&lt;br /&gt;
else&lt;br /&gt;
 @posts = @posts.where(['approved=?', false])&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
This is only a simple example, but should provide you with an idea of some of the ways this new syntax can be more useful.&lt;br /&gt;
&lt;br /&gt;
==='''Easier Email'''===&lt;br /&gt;
The ActionMailer module has been rewritten to make it a lot easier for your application to send email in Rails 3. &lt;br /&gt;
&lt;br /&gt;
'''1-Default Setting'''&lt;br /&gt;
In Rails, a Mailer is a class that can have many methods, each of which generally configure and send an email. Previously, you had to set all of the parameters for each email separately in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
def welcome_email(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;    # other paramters&lt;br /&gt;
end&lt;br /&gt;
 def password_reset(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;&lt;br /&gt;
    # other parameters&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
In Rails 3, you can specify defaults that can be optionally overwritten in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default :from =&amp;gt; 'no-reply@example.com',&lt;br /&gt;
           :return_path =&amp;gt; 'system@example.com'&lt;br /&gt;
&lt;br /&gt;
 def welcome_email(user)&lt;br /&gt;
    # no need to specify from parameter&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
'''2-Cleaner API'''&lt;br /&gt;
Previous versions of Rails required you to send email using special methods that were dynamically created by ActionMailer. For instance, if you wanted to deliver the welcome email in the example above, you would need to call:&lt;br /&gt;
&lt;br /&gt;
UserMailer.deliver_welcome_email(@user)  &lt;br /&gt;
&lt;br /&gt;
ln Rails 3, you can just call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;UserMailer.welcome_email(@user).deliver  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This makes more sense semantically, and additionally allows you to retrieve and manipulate the Mail object before delivering the email.&lt;br /&gt;
&lt;br /&gt;
==='''Dependency Management'''===&lt;br /&gt;
One of the strengths of the Ruby on Rails framework is the plethora of gems available for use by developers. Whether it's authentication[https://github.com/binarylogic/authlogic], &lt;br /&gt;
handling financial[http://activemerchant.org/] transactions[https://github.com/thoughtbot/paperclip], handling file uploads, or nearly anything else, chances are a gem exists to help with your problem.&lt;br /&gt;
&lt;br /&gt;
Issues can arise, however, if your gems require other gems, or developers are on different environments, among other things. To help solve these types of &lt;br /&gt;
situations, Rails 3 adds the Bundler[http://gembundler.com/] gem to help manage your dependencies. Using Bundler in Rails 3 is extremely simple; add a line for each gem you &lt;br /&gt;
require in your Gemfile, a file included in the root of each of your applications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;gem 'authlogic'&amp;lt;/b&amp;gt;&lt;br /&gt;
Once you've included all your gems, run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;bundle install&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and Bundler will download and configure all of the gems and their dependencies that you need for the project.&lt;br /&gt;
Bundler also allows you to specify certain gems to only be configured in certain environments (development vs production vs testing).&lt;br /&gt;
These are only a few of the many changes included in Ruby on Rails 3. Many of the old APIs still work in Rails, even if they've been deprecated, to make it easier to update. &lt;br /&gt;
&lt;br /&gt;
==='''ActiveRecord finder methods'''===&lt;br /&gt;
&amp;lt;b&amp;gt;In Rails 2&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.find(:all, : order =&amp;gt; &amp;quot;created_at desc&amp;quot;, :limit =&amp;gt; 5) &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 looks at the hash of options that is being passed to find(:all, : order, and :limit) and replace each item in the hash with an equivalent method.&lt;br /&gt;
Rails 3 has a &amp;lt;b&amp;gt;new API&amp;lt;/b&amp;gt; for the dynamic fiders&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;where, having, select, group, order, limit, offset, joins, includes, ...&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in Rails 3 it becomes&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.order(&amp;quot;created_at desc&amp;quot;).limit(5)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Active Record Validations'''===&lt;br /&gt;
There sre separate validations in Rails 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates_presence_of :username&lt;br /&gt;
&lt;br /&gt;
validates_uniqueness_of :username&lt;br /&gt;
&lt;br /&gt;
validates_length_of :email, :maximum =&amp;gt; 40 &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails 3 these can be clubbed together&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates :email, :presence =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:uniqueness =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:length =&amp;gt; {:maximum =&amp;gt; 30}&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/2_3_release_notes.html]&lt;br /&gt;
&lt;br /&gt;
[http://api.rubyonrails.org/]&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/3_0_release_notes.html#mail-extraction]&lt;br /&gt;
&lt;br /&gt;
[http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3]&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64911</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w19 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64911"/>
		<updated>2012-09-15T00:20:27Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
'''Rails''' is a MVC (Model-View-Controller) based architecture for web development based on the Ruby language. Rails 2[http://guides.rubyonrails.org/2_3_release_notes.html] was released in December 2007. Initially Rails 2 supported Ruby 1.8[http://www.ruby-lang.org/en/news/2008/05/31/ruby-1-8-7-has-been-released/]. It now has been updated to support Ruby 1.9.1[http://www.ruby-lang.org/en/news/2009/01/30/ruby-1-9-1-released/].  Rails 3 which brings in many improvements over Rails 2 supports Ruby 1.9 and above. The current stable version of Rails is 3.2.8[http://rubyonrails.org/download] released on Aug 9, 2012.&lt;br /&gt;
This wikibook chapter focuses mainly on the differences of Rails 2 and Rails 3.&lt;br /&gt;
&lt;br /&gt;
==Differences between Rails 2 and Rails 3==&lt;br /&gt;
==='''Unobtrusive Javascript'''[http://en.wikipedia.org/wiki/Unobtrusive_JavaScript]===&lt;br /&gt;
In previous versions of Rails, JavaScript was generated inline with HTML, causing ugly and somewhat brittle code.&lt;br /&gt;
As an example, Rails allows you to use its link_to method to generate a delete link for some object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Delete this Post&amp;quot;, @post, :confirm =&amp;gt; &amp;quot;Do you really want to delete this post?&amp;quot;, :method =&amp;gt; :delete %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Using this method in your view would generate the following in Rails 2:&lt;br /&gt;
&amp;lt;a href=&amp;quot;/posts/6&amp;quot; onclick=&amp;quot;if (confirm('Do you really want to delete this post?')) { var f = document.createElement('form');&lt;br /&gt;
       f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;&lt;br /&gt;
       var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');&lt;br /&gt;
       m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 would generate something much simpler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;a href='/posts/6&amp;quot;' rel=&amp;quot;nofollow&amp;quot; data-method=&amp;quot;delete&amp;quot; data-confirm=&amp;quot;Do you really want to delete this post?&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 replaces all of the inline JavaScript with a couple of HTML5 attributes. All of the JavaScript event handlers to handle the actual confirmation box and deletion are stored in one central JavaScript file that is included with every rails project.&lt;br /&gt;
&lt;br /&gt;
==='''No more dependency on only Prototype Javascript library'''===&lt;br /&gt;
One big advantage to this new method is that the JavaScript helpers are framework agnostic. Instead of being tied to the Prototype library like you were in Rails 2, you can now choose whatever JavaScript framework you like (Rails apps come with Prototype by default, but jQuery is now officially supported[https://github.com/rails/jquery-ujs].&lt;br /&gt;
&lt;br /&gt;
==='''Improved Security'''=== &lt;br /&gt;
Another awesome new feature of Rails 3 is that XSS[http://en.wikipedia.org/wiki/Cross-site_scripting] protection is now enabled by default. Rails 2 supported XSS protection through the use of the h method.&lt;br /&gt;
&amp;lt;%= h @comment.text %&amp;gt;  &lt;br /&gt;
The h method would escape html and JavaScript to ensure that no malicious client-side code was executed. This method worked great, but there was one problem: you had to actually remember to use the h method everywhere the user entered input was displayed. If you forgot even one place, then you were vulnerable to an XSS attack.&lt;br /&gt;
In Rails 3, all input is escaped by default, taking the burden off of the developer of having to remember to escape everywhere that malicious code might be present. For those times that you do want to allow unescaped data to appear in your view, you can use the raw method to tell Rails 3 not to escape the data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= raw @comment.text %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==='''New Query Engine'''===&lt;br /&gt;
Rails 3 includes a cool new query engine that makes it easier to get back the data you want and gives you more flexibility in your controller code. These changes show up in various places, but the most common case is fetching data in your controller. In Rails 2, you could use the find method to retrieve the data you were looking for, passing in arguments to specify conditions, grouping, limits, and any other query information. For example:&lt;br /&gt;
&lt;br /&gt;
@posts = Post.find(:all, :conditions =&amp;gt; [ &amp;quot;category IN (?)&amp;quot;, categories], :limit =&amp;gt; 10, :order =&amp;gt; &amp;quot;created_on DESC&amp;quot;)  &lt;br /&gt;
&lt;br /&gt;
finds the first ten posts within some specified categories ordered by the creation time.&lt;br /&gt;
&lt;br /&gt;
In Rails 3, each of the passed in parameters has its own method, which can be chained together to get the same results.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories]).order(&amp;quot;created_on DESC&amp;quot;).limit(10)&lt;br /&gt;
&lt;br /&gt;
The query is not actually executed until the data is needed; so these methods can even be used across multiple statements.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories])&lt;br /&gt;
if(condition_a)&lt;br /&gt;
 @posts = @posts.where(['approved=?', true])&lt;br /&gt;
else&lt;br /&gt;
 @posts = @posts.where(['approved=?', false])&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
This is only a simple example, but should provide you with an idea of some of the ways this new syntax can be more useful.&lt;br /&gt;
&lt;br /&gt;
==='''Easier Email'''===&lt;br /&gt;
The ActionMailer module has been rewritten to make it a lot easier for your application to send email in Rails 3. &lt;br /&gt;
&lt;br /&gt;
'''1-Default Setting'''&lt;br /&gt;
In Rails, a Mailer is a class that can have many methods, each of which generally configure and send an email. Previously, you had to set all of the parameters for each email separately in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
def welcome_email(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;    # other paramters&lt;br /&gt;
end&lt;br /&gt;
 def password_reset(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;&lt;br /&gt;
    # other parameters&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
In Rails 3, you can specify defaults that can be optionally overwritten in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default :from =&amp;gt; 'no-reply@example.com',&lt;br /&gt;
           :return_path =&amp;gt; 'system@example.com'&lt;br /&gt;
&lt;br /&gt;
 def welcome_email(user)&lt;br /&gt;
    # no need to specify from parameter&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
'''2-Cleaner API'''&lt;br /&gt;
Previous versions of Rails required you to send email using special methods that were dynamically created by ActionMailer. For instance, if you wanted to deliver the welcome email in the example above, you would need to call:&lt;br /&gt;
&lt;br /&gt;
UserMailer.deliver_welcome_email(@user)  &lt;br /&gt;
&lt;br /&gt;
ln Rails 3, you can just call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;UserMailer.welcome_email(@user).deliver  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This makes more sense semantically, and additionally allows you to retrieve and manipulate the Mail object before delivering the email.&lt;br /&gt;
&lt;br /&gt;
==='''Dependency Management'''===&lt;br /&gt;
One of the strengths of the Ruby on Rails framework is the plethora of gems available for use by developers. Whether it's authentication[https://github.com/binarylogic/authlogic], &lt;br /&gt;
handling financial[http://activemerchant.org/] transactions[https://github.com/thoughtbot/paperclip], handling file uploads, or nearly anything else, chances are a gem exists to help with your problem.&lt;br /&gt;
&lt;br /&gt;
Issues can arise, however, if your gems require other gems, or developers are on different environments, among other things. To help solve these types of &lt;br /&gt;
situations, Rails 3 adds the Bundler[http://gembundler.com/] gem to help manage your dependencies. Using Bundler in Rails 3 is extremely simple; add a line for each gem you &lt;br /&gt;
require in your Gemfile, a file included in the root of each of your applications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;gem 'authlogic'&amp;lt;/b&amp;gt;&lt;br /&gt;
Once you've included all your gems, run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;bundle install&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and Bundler will download and configure all of the gems and their dependencies that you need for the project.&lt;br /&gt;
Bundler also allows you to specify certain gems to only be configured in certain environments (development vs production vs testing).&lt;br /&gt;
These are only a few of the many changes included in Ruby on Rails 3. Many of the old APIs still work in Rails, even if they've been deprecated, to make it easier to update. &lt;br /&gt;
&lt;br /&gt;
==='''ActiveRecord finder methods'''===&lt;br /&gt;
&amp;lt;b&amp;gt;In Rails 2&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.find(:all, : order =&amp;gt; &amp;quot;created_at desc&amp;quot;, :limit =&amp;gt; 5) &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 looks at the hash of options that is being passed to find(:all, : order, and :limit) and replace each item in the hash with an equivalent method.&lt;br /&gt;
Rails 3 has a &amp;lt;b&amp;gt;new API&amp;lt;/b&amp;gt; for the dynamic fiders&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;where, having, select, group, order, limit, offset, joins, includes, ...&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in Rails 3 it becomes&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.order(&amp;quot;created_at desc&amp;quot;).limit(5)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Active Record Validations'''===&lt;br /&gt;
There sre separate validations in Rails 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates_presence_of :username&lt;br /&gt;
&lt;br /&gt;
validates_uniqueness_of :username&lt;br /&gt;
&lt;br /&gt;
validates_length_of :email, :maximum =&amp;gt; 40 &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails 3 these can be clubbed together&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates :email, :presence =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:uniqueness =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:length =&amp;gt; {:maximum =&amp;gt; 30}&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64896</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w19 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w19_sa&amp;diff=64896"/>
		<updated>2012-09-15T00:11:33Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
'''Rails''' is a MVC (Model-View-Controller) based architecture for web development based on the Ruby language. Rails 2[http://guides.rubyonrails.org/2_3_release_notes.html] was released in December 2007. Initially Rails 2 supported Ruby 1.8[http://www.ruby-lang.org/en/news/2008/05/31/ruby-1-8-7-has-been-released/]. It now has been updated to support Ruby 1.9.1[http://www.ruby-lang.org/en/news/2009/01/30/ruby-1-9-1-released/].  Rails 3 which brings in many improvements over Rails 2 supports Ruby 1.9 and above. The current stable version of Rails is 3.2.8[http://rubyonrails.org/download] released on Aug 9, 2012.&lt;br /&gt;
This wikibook chapter focuses mainly on the differences of Rails 2 and Rails 3.&lt;br /&gt;
&lt;br /&gt;
==Differences between Rails 2 and Rails 3==&lt;br /&gt;
==='''Unobtrusive Javascript'''[http://en.wikipedia.org/wiki/Unobtrusive_JavaScript]===&lt;br /&gt;
In previous versions of Rails, JavaScript was generated inline with HTML, causing ugly and somewhat brittle code.&lt;br /&gt;
As an example, Rails allows you to use its link_to method to generate a delete link for some object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= link_to &amp;quot;Delete this Post&amp;quot;, @post, :confirm =&amp;gt; &amp;quot;Do you really want to delete this post?&amp;quot;, :method =&amp;gt; :delete %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Using this method in your view would generate the following in Rails 2:&lt;br /&gt;
&amp;lt;a href=&amp;quot;/posts/6&amp;quot; onclick=&amp;quot;if (confirm('Do you really want to delete this post?')) { var f = document.createElement('form');&lt;br /&gt;
       f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;&lt;br /&gt;
       var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');&lt;br /&gt;
       m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 would generate something much simpler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;a href='/posts/6&amp;quot;' rel=&amp;quot;nofollow&amp;quot; data-method=&amp;quot;delete&amp;quot; data-confirm=&amp;quot;Do you really want to delete this post?&amp;quot;&amp;gt;Delete this Post&amp;lt;/a&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
Rails 3 replaces all of the inline JavaScript with a couple of HTML5 attributes. All of the JavaScript event handlers to handle the actual confirmation box and deletion are stored in one central JavaScript file that is included with every rails project.&lt;br /&gt;
&lt;br /&gt;
==='''No more dependency on only Prototype Javascript library'''===&lt;br /&gt;
One big advantage to this new method is that the JavaScript helpers are framework agnostic. Instead of being tied to the Prototype library like you were in Rails 2, you can now choose whatever JavaScript framework you like (Rails apps come with Prototype by default, but jQuery is now officially supported[https://github.com/rails/jquery-ujs].&lt;br /&gt;
&lt;br /&gt;
==='''Improved Security'''=== &lt;br /&gt;
Another awesome new feature of Rails 3 is that XSS[http://en.wikipedia.org/wiki/Cross-site_scripting] protection is now enabled by default. Rails 2 supported XSS protection through the use of the h method.&lt;br /&gt;
&amp;lt;%= h @comment.text %&amp;gt;  &lt;br /&gt;
The h method would escape html and JavaScript to ensure that no malicious client-side code was executed. This method worked great, but there was one problem: you had to actually remember to use the h method everywhere the user entered input was displayed. If you forgot even one place, then you were vulnerable to an XSS attack.&lt;br /&gt;
In Rails 3, all input is escaped by default, taking the burden off of the developer of having to remember to escape everywhere that malicious code might be present. For those times that you do want to allow unescaped data to appear in your view, you can use the raw method to tell Rails 3 not to escape the data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= raw @comment.text %&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
==='''New Query Engine'''===&lt;br /&gt;
Rails 3 includes a cool new query engine that makes it easier to get back the data you want and gives you more flexibility in your controller code. These changes show up in various places, but the most common case is fetching data in your controller. In Rails 2, you could use the find method to retrieve the data you were looking for, passing in arguments to specify conditions, grouping, limits, and any other query information. For example:&lt;br /&gt;
&lt;br /&gt;
@posts = Post.find(:all, :conditions =&amp;gt; [ &amp;quot;category IN (?)&amp;quot;, categories], :limit =&amp;gt; 10, :order =&amp;gt; &amp;quot;created_on DESC&amp;quot;)  &lt;br /&gt;
&lt;br /&gt;
finds the first ten posts within some specified categories ordered by the creation time.&lt;br /&gt;
&lt;br /&gt;
In Rails 3, each of the passed in parameters has its own method, which can be chained together to get the same results.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories]).order(&amp;quot;created_on DESC&amp;quot;).limit(10)&lt;br /&gt;
&lt;br /&gt;
The query is not actually executed until the data is needed; so these methods can even be used across multiple statements.&lt;br /&gt;
&lt;br /&gt;
@posts = Post.where([ &amp;quot;category IN (?)&amp;quot;, categories])&lt;br /&gt;
if(condition_a)&lt;br /&gt;
 @posts = @posts.where(['approved=?', true])&lt;br /&gt;
else&lt;br /&gt;
 @posts = @posts.where(['approved=?', false])&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
This is only a simple example, but should provide you with an idea of some of the ways this new syntax can be more useful.&lt;br /&gt;
&lt;br /&gt;
==='''Easier Email'''===&lt;br /&gt;
The ActionMailer module has been rewritten to make it a lot easier for your application to send email in Rails 3. &lt;br /&gt;
&lt;br /&gt;
'''1-Default Setting'''&lt;br /&gt;
In Rails, a Mailer is a class that can have many methods, each of which generally configure and send an email. Previously, you had to set all of the parameters for each email separately in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
def welcome_email(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;    # other paramters&lt;br /&gt;
end&lt;br /&gt;
 def password_reset(user)&lt;br /&gt;
    from       &amp;quot;system@example.com&amp;quot;&lt;br /&gt;
    # other parameters&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
In Rails 3, you can specify defaults that can be optionally overwritten in each method.&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default :from =&amp;gt; 'no-reply@example.com',&lt;br /&gt;
           :return_path =&amp;gt; 'system@example.com'&lt;br /&gt;
&lt;br /&gt;
 def welcome_email(user)&lt;br /&gt;
    # no need to specify from parameter&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
'''2-Cleaner API'''&lt;br /&gt;
Previous versions of Rails required you to send email using special methods that were dynamically created by ActionMailer. For instance, if you wanted to deliver the welcome email in the example above, you would need to call:&lt;br /&gt;
&lt;br /&gt;
UserMailer.deliver_welcome_email(@user)  &lt;br /&gt;
&lt;br /&gt;
ln Rails 3, you can just call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;UserMailer.welcome_email(@user).deliver  &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This makes more sense semantically, and additionally allows you to retrieve and manipulate the Mail object before delivering the email.&lt;br /&gt;
&lt;br /&gt;
==='''Dependency Management'''===&lt;br /&gt;
One of the strengths of the Ruby on Rails framework is the plethora of gems available for use by developers. Whether it's authentication[https://github.com/binarylogic/authlogic], &lt;br /&gt;
handling financial[http://activemerchant.org/] transactions[https://github.com/thoughtbot/paperclip], handling file uploads, or nearly anything else, chances are a gem exists to help with your problem.&lt;br /&gt;
&lt;br /&gt;
Issues can arise, however, if your gems require other gems, or developers are on different environments, among other things. To help solve these types of &lt;br /&gt;
situations, Rails 3 adds the Bundler[http://gembundler.com/] gem to help manage your dependencies. Using Bundler in Rails 3 is extremely simple; add a line for each gem you &lt;br /&gt;
require in your Gemfile, a file included in the root of each of your applications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;gem 'authlogic'&amp;lt;/b&amp;gt;&lt;br /&gt;
Once you've included all your gems, run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;bundle install&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and Bundler will download and configure all of the gems and their dependencies that you need for the project.&lt;br /&gt;
Bundler also allows you to specify certain gems to only be configured in certain environments (development vs production vs testing).&lt;br /&gt;
These are only a few of the many changes included in Ruby on Rails 3. Many of the old APIs still work in Rails, even if they've been deprecated, to make it easier to update. &lt;br /&gt;
&lt;br /&gt;
==='''ActiveRecord finder methods'''===&lt;br /&gt;
&amp;lt;b&amp;gt;In Rails 2&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.find(:all, : order =&amp;gt; &amp;quot;created_at desc&amp;quot;, :limit =&amp;gt; 5) &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 looks at the hash of options that is being passed to find(:all, : order, and :limit) and replace each item in the hash with an equivalent method.&lt;br /&gt;
Rails 3 has a &amp;lt;b&amp;gt;new API&amp;lt;/b&amp;gt; for the dynamic fiders&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;where, having, select, group, order, limit, offset, joins, includes, ...&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So, in Rails 3 it becomes&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;User.order(&amp;quot;created_at desc&amp;quot;).limit(5)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==='''Active Record Validations'''===&lt;br /&gt;
There sre separate validations in Rails 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates_presence_of :username&lt;br /&gt;
&lt;br /&gt;
validates_uniqueness_of :username&lt;br /&gt;
&lt;br /&gt;
validates_length_of :email, :maximum =&amp;gt; 40 &amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails 3 these can be clubbed together&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;validates :email, :presence =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:uniqueness =&amp;gt; true,&lt;br /&gt;
&lt;br /&gt;
:length =&amp;gt; {:maximum =&amp;gt; 30}&amp;lt;/b&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=64345</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=64345"/>
		<updated>2012-09-14T20:01:07Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&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/ch1 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/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;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=64344</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=64344"/>
		<updated>2012-09-14T19:59:05Z</updated>

		<summary type="html">&lt;p&gt;Sbhatta9: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&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/ch1 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/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 op]]&lt;/div&gt;</summary>
		<author><name>Sbhatta9</name></author>
	</entry>
</feed>