CSC/ECE 517 Fall 2011/ch2 2c ac: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 58: Line 58:
== Singleton==
== Singleton==
The <tt>Singleton</tt> design pattern ensures that only one instance of a particular class may be created for the lifetime of a program.
=== In Ruby ===
=== In Ruby ===
You just include the Singleton module and your class becomes a singleton.
The <tt>singleton</tt> library contains the <tt>Singleton</tt> module that if mixed into class will make that class a singleton.
The <tt>Singleton</tt> module makes the mixee's <tt>new</tt> method private and replace it with the <tt>instance</tt> method that when called returns a singleton instance of the mixee.  
 
You do not have to code the singleton functionality inside each class that needs to be a singleton.
You do not have to code the singleton functionality inside each class that needs to be a singleton.



Revision as of 00:18, 18 September 2011

Mixin versus Interface

Introduction

[To be changed] Functionality such as Comparable is done in Java with interfaces and in Ruby with mixins. Is there an advantage in using one or another? Consider other behaviors achieved with mixins in Ruby: Singleton, Enumerable, and DataMapper, for example. Could you accomplish these with interfaces in Java? Does this mean that mixins are more powerful? Or can interfaces achieve some purposes that mixins can't?

Concepts to explore

Definition

Multiple Inheritance

Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass. (From Wikipedia, http://en.wikipedia.org/wiki/Multiple_inheritance)

Inheritance vs Composition

"Inheritance represents an incredibly tight coupling of two components. Change a parent class and you risk breaking the child class. But even worse, if code that uses objects of the child class relies on those objects also having methods defined in the parent, then all that code will brake too." (Thomas 84)


Interfaces in Java

http://download.oracle.com/javase/tutorial/java/concepts/interface.html

Modules in Ruby

Modules in Ruby are a way to group together methods, classes and constants. They are similar to namespaces in languages such as C++. (From lecture 6 note)

Mixin using Modules

The most interesting use of modules is to define mixins. When you include a module within a class, all its functionality becomes available to the class. Not only can modules contain class methods; they can also contain instance methods. (From lecture 6 note)

Advantage and Disadvantage

Ruby mixins vs Java interfaces feature comparison
Features Ruby Mixins Java Interfaces
Add functionality/behavior to a class Yes. When a Ruby module gets mixed in a Ruby class, the class receives the implementation (behavior) of the methods defined in the module. No. A Java interface "can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces." (Java Interface)
Favor composition over inheritance Yes. The class that includes a module establishes a "use" relationship with the module. This is composition. No. The class that implements an interface establishes a "is-a" relationship with the interface. This is inheritance.
The code of the mixin/interface interacts with the code of the class that includes/implements it Yes. A mixed-in module can implement methods in terms of the host's class methods. See the Enumerable example. No. An interface provides a set of method signatures that the host class must implement. An interface provides no implementation of methods.

Mixin

Mixin Advantage

Mixin Disadvantage

"A mixed-in module's instance variables can clash with the ones of the host class or with the ones of other mixins."(Thomas 82) There are programming practices to avoid this potential problem.

Interfact

Interface Advantage

Interface Disadvantage

Examples

Comparable

Ruby Implementation

Java Implementation

Singleton

The Singleton design pattern ensures that only one instance of a particular class may be created for the lifetime of a program.

In Ruby

The singleton library contains the Singleton module that if mixed into class will make that class a singleton. The Singleton module makes the mixee's new method private and replace it with the instance method that when called returns a singleton instance of the mixee.

You do not have to code the singleton functionality inside each class that needs to be a singleton.

require 'singleton'

class Klass
      include Singleton
end

a,b  = Klass.instance, Klass.instance
=> [#<Klass:0x007fa7a28798e8>, #<Klass:0x007fa7a28798e8>]

a == b
=> true

Klass.new
NoMethodError: private method `new' called for Klass:Class

Java Implementation

Each class that needs to be a singleton must implement the singleton functionality itself like in the following example.

public class Singleton  {
    private static final Singleton instance = new Singleton();
 
/**
Private constructor prevents instantiation from other classes
**/
    private Singleton() {
    }
 
    public static Singleton getInstance() {
        return instance;
    }
}

Enumerable

Ruby Implementation

Java Implementation

DataMapper

Ruby Implementation

Java Implementation

Unique Use Cases

Mixin Only Use Cases

Interface Only Use Cases

Reference

Thomas Dave, "Programming Ruby 1.9" textbook

Java Interface