CSC/ECE 517 Fall 2011/ch4 4h sv: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 74: Line 74:
  end
  end
  >Initialised
  >Initialised
 
<br>


  The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a ".instance" method is defined. One more thing to notice is that, "new" method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.
  The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a ".instance" method is defined. One more thing to notice is that, "new" method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.
 
<br>
  Single_ton.instance
  Single_ton.instance
  => #<Single_ton:0x94483d4>  
  => #<Single_ton:0x94483d4>  

Revision as of 04:41, 19 October 2011

Design Patterns in Ruby

"Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice" - Christopher Alexander

"In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design".

A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.

Overview

A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.

The description of a pattern consists of:

  • The Pattern name, describes the design pattern, its solutions and consequences.
  • The problem the pattern solves, describes when to apply the pattern.
  • The solution, describes the elements that make up the design, their relationships, responsibilities, and collaborations.
  • The consequences, describes the results of applying the pattern to the problem.

The different types of design patterns are listed below:

  • Singleton Design Pattern, which is used to have only one instance of a class.
  • Adapter Design Pattern, which enables classes with incompatible interfaces to work together.
  • Observer Design Pattern, which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.
  • Command Design Pattern, which enables to pass around the code that needs to be executed later.
  • Algorithm Strategy Pattern, which helps choose an algorithm to fulfill a task based on some "parameter" of the situation.

Singleton Pattern

The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object.

Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:

require 'singleton'
class Example
    include Singleton
    ...
end

It could be noticed that one must 'require' a module called 'singleton' and include the module within the class. This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,

 raisesError = Example.new
 ruby-1.9.2-p290 :026 > raisesError = Example.new
 NoMethodError: private method `new' called for Example:Class

We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'

a = Example.instance
=> #<Example:0x9361cb8> 
b = Example.instance
=> #<Example:0x9361cb8>

The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. This further proves that only one object can be created for a Singleton.

       That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.
class Single_ton
def initialize
   puts "Initialised"
end

@@instance = Single_ton.new
 
def self.instance
   return @@instance
end
 
def print_something
  puts "This prints something"
end
  
private_class_method :new
end
>Initialised

The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a ".instance" method is defined. One more thing to notice is that, "new" method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.


Single_ton.instance
=> #<Single_ton:0x94483d4> 
Single_ton.instance.print_something
This prints something
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.
The method could be accessed by accessing the lone object inside the class.

The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.

Adapter Pattern

An Adapter Design Pattern, also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:

  • “Wrapping” its own interface around the interface of a pre-existing class.
  • Besides, it may also translate data formats from the caller to a form needed by the callee. Example: If the caller stores boolean value in terms of integers but the callee needs the values as 'true/false', the adapter pattern would extract the right value from the caller and pass it on to the callee. This ensures that the caller and callee can work together.

The purpose of an adapter is “to convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”

Below is an illustration of the Adapter Pattern:


Types of Adapter Patterns

Based on the structure, there are two types of Adapter Patterns:

Object Adapter Pattern

The Adapter Pattern has an instance of the classit "wraps" and makes calls to this wrapped object alone.

Class Adapter Pattern

Polymorphic Interfaces are used by this type of pattern, where the interface that is expected and the interface that is available are both inherited or implemented. This is used in situations where the class providing the services you need does not provide the interface required.

Observer Pattern

The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically.

Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.

Command Pattern

At times, it is needed to pass around code that needs to be executed later. This is achieved by employing the Command Design Pattern which is used to factor out the action code of a class into its own object. It encapsulates a request as an object, allowing clients with different requests to be parameterized. The Command Pattern is implemented using Closures. A Command class holds an object, method along with the information needed to call a method at a later time. This information includes the method name with the values for the method parameters. The 'Call' method of Ruby brings the different parts together when needed.

This is illustrated using the following example:

One example is a check at a restaurant.

  • The waiter/waitress takes an order from a customer and writes it on a check.
  • The check is then queued for the cook, who prepares the food as requested by the customer.
  • The check is later returned to the server, who uses it to bill the customer.

In this example, the check has nothing to do with the menu. In principle, the same checks could be used at any restaurant.

The command pattern is made up of a client, invoker and receiver. A client creates an object of the Command class and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is the class object which contains the code of the method. The usage of command objects makes it easier to construct components which need to execute methods at a later time without having knowledge about the method's owner or parameters.

This Pattern can be implemented using the Proc objects, which is a callable block of code that closes over the variables in scope when it was created. This gives us a concise implementation of Command Pattern.

Below is an illustration of the Command Design Pattern, using Proc Objects:

count = 0
commands = []
(1..10).each do |i|
commands << proc { count += i }
end
puts "Count is initially #{count}"
commands.each { |cmd| cmd.call }
puts "Performed all commands. count is #{count}"

Another important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method. The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.

Algorithm Strategy Pattern

The Strategy Pattern helps choose an algorithm to accomplish a task based on some "parameters" of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.

The strategy pattern "defines a family of algorithms, encapsulates each one, and makes them interchangeable".

Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.

A Strategy Pattern is best implemented using Proc Objects. Below is an illustration of it:

class RoutePlanner
    attr_accessor :strategy
    def go(pointA, pointB)
         strategy.call(*args)
    end
    def fastest … end
    def shortest … end
end

Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.

ctx = RoutePlanner.new
if on_foot
   ctx.strategy = RoutePlanner.method(:shortest)
else 
   ctx.strategy = RoutePlanner.method(:fastest)
ctx.go(pointA, pointB)

Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.

Differences between Command and Strategy Pattern

  • A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it.
  • A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.

References

http://designpatternsinruby.com/section01/article.html http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf