CSC/ECE 517 Fall 2009/wiki2 11 zv

From Expertiza_Wiki
Jump to navigation Jump to search

What facilities does Ruby offer that makes it easier to realize other GoF (and other) patterns that we did not cover in class? The key idea here is to explore how Ruby can implement these patterns more efficiently or transparently than static (or other dynamic) o-o languages.

Introduction

Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as "a general reusable solution to a commonly occurring problem in software design." [1] "The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past" [2]. A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem. Design patterns can be classified into 3 parts Creational, Structural, Behavioral [3].

Factory Design Pattern

What is Factory Design Pattern?

The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects. The main goal of this implementation is to isolate the code that creates the class form the concrete implementation of that class. Ruby example for the same is given below.

Implementation in Ruby

The given code explains how to create a factory in Ruby. In this it initially creates a factory class called GearFactory and overrides the new function. When an object is instantiated the code does not need to know which kind of object it is. It is a collection (hence the word factory) of objects that are clubbed together.

   class GearFactory
     def new() 
       if ( ... some condition )
          return Sprocket.new()
       else
          return Cog().new()
       end
     end
   end

Our client class now becomes:

   class GearUser 
     def doSomething(factory )
       ...
     my_gear = factory.new()
       ...
     end
   end

The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the following code.

   client.doSomething(GearFactory.new)          #Use the factory
   client.doSomething(Cog)                      #Use the Cog class
   client.doSomething(Sprocket)                 #Use the Sprocket class

Implementation in Java

In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application. A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.

   interface ScrollBar { ... }
   interface MenuBar   { ... }
   ...

And associated classes implementing them on different windowing systems:

   class MotifScrollBar implements ScrollBar { ... }
   class Win95ScrollBar implements ScrollBar { ... }
   ...

And a factory interface that also doesn't commit to representation:

   interface Factory {
     public abstract ScrollBar newScrollBar();
     public abstract MenuBar   newMenuBar();
     ... 
   }

But implementation classes that do:

   class MotifFactory implements Factory {
     public ScrollBar newScrollBar() { return new MotifScrollBar(...); }
     ...
   }

Comparison of Implementations

Both the languages implement the factory design pattern. Java implements the factory design pattern using the interfaces while Ruby uses classes to create a factory. An abstract base class can be defined in Java to hold all the interfaces needed or there could be a parameterised method which takes the kind of object as a parameter to instantiate the necessary object. In Ruby 'new' is just a method on a class object, it's always free to return anything it likes. The nice thing about this is that, in ruby, every call to new is, by definition, the factory pattern - it just so happens that there is a default implementation inherited by class 'Class'. The factory implementation of Ruby is a lot simpler than the factory implementation of Java.


Abstract Factory Pattern

What is Abstract Factory Pattern?

Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [4] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [5]

Implementation in Ruby

Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.

   Class Foo; end
   Class Bar, end


Here is the use of Abstract Factory Pattern

   def create_something(factory)

new_object = factory.new puts "created a new #{new_object.class} with a factory"

   end

Here we select a factory to use

   Create_something(Foo)
   Create_something(Bar)

Output of the code:
Created a Foo with a factory
Created a Bar with a factory

The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.

Implementation in Java

In Java, implementing Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class [6]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java.


  public class FactoryFmProto {
 static class Expression {
  protected String str;
  public Expression( String s ) { str = s; }
  public Expression cloan()     { return null; }
  public String     toString()  { return str; }
 }
 static abstract class Factory {
  protected Expression prototype = null;
  public Expression makePhrase() { return prototype.cloan(); }
  public abstract Expression makeCompromise();
  public abstract Expression makeGrade();
 }
 static class PCFactory extends Factory {
  public PCFactory() { prototype = new PCPhrase(); }
  public Expression makeCompromise() {
     return new Expression( "\"do it your way, any way, or no way\"" ); }
  public Expression makeGrade() {
     return new Expression( "\"you pass, self-esteem intact\"" ); }
 }
  static class NotPCFactory extends Factory {
  public NotPCFactory() { prototype = new NotPCPhrase(); }
  public Expression makeCompromise() {
     return new Expression( "\"my way, or the highway\"" ); }
  public Expression makeGrade() {
     return new Expression( "\"take test, deal with the results\"" ); }
 }
  public static void main( String[] args ) {
  Factory factory;
  if (args.length > 0) factory = new PCFactory();
  else                 factory = new NotPCFactory();
  for (int i=0; i < 3; i++) System.out.print( factory.makePhrase() + "  " );
  System.out.println();
  System.out.println( factory.makeCompromise() );
  System.out.println( factory.makeGrade() );
 }
 }

Comparison of Implementations

Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.

Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [7].

Iterator Design Pattern

What is Iterator Design Pattern?

The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [8]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [9]

Implementation in Ruby

Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;

   def print_element(container)
   	Container.each {|o| puts o.inspect }
   end
   list = [1,2,2,3]
   hash = {“a”=>1, “b”=>2,”c”=>3, “d”=>4 }
   print_elements list
   print_elements hash

The output of the code is,

   1
   2
   3
   4
   [“a”,1]
   [“b”,2]
   [“c”,3]
   [“d”,4]

Implementation in Java

In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class. Thus a class who needs to access the list will need to call the interface class [10]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.

   ...
   List list = new List();
   ...
   ListIterator iterator = new ListIterator(list);
   iterator.First();
   while (!iterator.IsDone()) {
   Object item = iterator.CurrentItem();
   // Code here to process item.
   iterator.Next();
   }
   ...

Comparison of Implementations

Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [11].

In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [12].

Decorator Pattern

This pattern is used to increase the functionality of the existing object dynamically. Suppose we have a program that uses eight objects, but three of them need an additional feature. We could create derived class for these objects having the additional features but then we can create a Decorator class which will add any specific kind of feature required [13]. It is a type of behavioral pattern.

Implementation in Ruby

Generic decorators can be implmented by using the method_missing method. This method is called when an object receives a message that it does not have a method fo. The method_missing method can forward the message on to other object and wrap additional behavior around the call. In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9. The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. The need for this kind of pattern is so as to increase the functionality of a particular class [14].

   module Decorator
     def initialize(decorated)
       @decorated = decorated
     end
   def method_missing(method, *args)
       args.empty? ? @decorated.send(method) : @decorated.send(method, args)
   end
  end
   class Whip
     include Decorator
     def cost 
       @decorated.cost + 0.2
     end
   end
   class Sprinkles
     include Decorator
     def cost
       @decorated.cost + 0.3
     end
   end
   Whip.new(Coffee.new).cost
   #=> 2.2
   Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost 
   #=> 2.9

There are various other options to implement this design pattern. Other way to implememt the pattern is by using singleton methods and method aliases. Delegation can also be used, and it overcomes one of the most important disadvantage. We can remove add features if we are using delegation but this is not possible if we are using singleton methods and method aliases. Example for this can be found here [15].

Implementation in Java

In Java, this pattern is easy to implement by dividing the various responsibilities into classes and interfaces. The below example consider all these responsibilities and have four components which are described below

  • Component: Defines the interface for objects that can have responsibilities added to them dynamically.
  • ConcreteComponent: Defines an object to which additional responsibilities can be attached.
  • Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.
  • ConcreteDecorator: adds responsibilities to the component.


   package decorator;
   public interface IComponent {
   public void doStuff();
   }

The Concrete component

   package decorator;
   public class Component implements IComponent{
   public void doStuff() {
   System.out.println("Do Suff");
   }
   }

The Decorator

   package decorator;
   public interface Decorator extends IComponent {
   public void addedBehavior();
   }

The Concrete Decorator (Extends IComponent)

   package decorator;
   public class ConcreteDecorator implements Decorator {
   IComponent component;
   public ConcreteDecorator(IComponent component) {
   super();
   this.component = component;
   }
   public void addedBehavior() {
   System.out.println("Decorator does some stuff too");
   }
   public void doStuff() {
   component.doStuff();
   addedBehavior();
   }
  }

The Client

   import decorator.*;
   public class Client {
   public static void main(String[] args) {
   IComponent comp = new Component();
   Decorator decorator = new ConcreteDecorator(comp);
   decorator.doStuff();
   }
   }

Comparison of implementations

Ruby provides different options to implement Decorator pattern like method_missing, delegation and alias methods. This provides flexibility to the user to implement any possible way depending on the requirements and features needed. But in Java, there is more clearer approach using the components provided in the example. It is a straight forward approach. But different options provided by Ruby make it much better than Java.

In dynamic languages like Javascript, decorator pattern can be implemented with no interfaces or traditional OOP inheritance. With simple overriding but this approach is difficult to implement [16].

Conclusion

We have seen four design patterns Factory design pattern, Abstract Factory design pattern, Iterator design pattern and Decorator Design Pattern. We have chosen these patterns as they are widely used and Ruby has in-built implementations of these. We have mainly tried to compare Ruby and Java but also specified tips about other languages like PHP, C# and Javascript. In Ruby implementing these design patterns is simple and easy because of its main feature of private class objects, unbounded polymorphism and duck typing [17]. In Java all the features are mainly implemented through interfaces and classes. This has several disadvantages like increase in code complexity, size and is generally confusing and tedious to trace or understand the code.

References

  1. Design Patterns
  2. Design Patterns Java Companion James W Cooper
  3. Wiki Design Patterns
  4. Abstract Factory Method
  5. SourceMaking explains Abstract Factory
  6. Abstract Factory Example in Java
  7. Abstract Factory explained
  8. Iterator Pattern
  9. Sourcemaking explains Iterator method
  10. Iterator method explained
  11. DoFactory explains Iterator pattern
  12. Iterator pattern for PHP
  13. Decorator Pattern
  14. Wiki Decorator Pattern
  15. Design Patterns in Ruby
  16. Design Patterns Javascript