CSC/ECE 517 Fall 2007/wiki1b 6 aa: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:
Singleton Pattern
Singleton Pattern
----
----
--[[User:Xgao2|Xgao2]] 17:40, 30 September 2007 (EDT)
Example
Example
Let's take the example of the Earth. There is only one earth object for the class of Earth, because we only have one earth. The currentCondition describe the earth's current condition.  
Let's take the example of the Earth. There is only one earth object for the class of Earth, because we only have one earth. The currentCondition describe the earth's current condition.  
Line 12: Line 11:
Ruby Implementation
Ruby Implementation


<pre>require 'Singleton'
<pre>
require 'Singleton'


class Earth
class Earth
include Singleton
  include Singleton # include singleton mixin
attr_accessor :currentCondition
  attr_accessor :currentCondition
end
end
</pre>
Java Implementation
<pre>
public class Earth {
  private Earth()
  {
  // override the contructor to make it private
  }
  private static Earth ref;
  // make the method thread safe
  public synchronized static Earth getEarth()
  {
    if (ref == null)
        // lazy initialization
        ref = new Earth();
    return ref;
  }
  public Object clone()
throws CloneNotSupportedException
  {
    throw new CloneNotSupportedException();
  }
  private String currentCondition="I was beatiful and clean";
 
  public String getCurrentCondition(){
  return currentCondition;
  }
 
  public void setCurrentCondition(String str){
  currentCondition=str;
  }
 
}
</pre>
</pre>

Revision as of 22:54, 30 September 2007

Take a case of the Singleton pattern and implement it as succinctly as possible in Ruby and Java.Compare the two implementations in terms of clarity and succinctness.The example should be a "real-world" example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).

http://www.beginner-java-tutorial.com/singleton.html http://www.ruby-doc.org/stdlib/libdoc/singleton/rdoc/index.html

Singleton Pattern


Example Let's take the example of the Earth. There is only one earth object for the class of Earth, because we only have one earth. The currentCondition describe the earth's current condition.

Ruby Implementation

require 'Singleton'

class Earth
  include Singleton  # include singleton mixin
  attr_accessor :currentCondition
end

Java Implementation


public class Earth {
	  private Earth()
	  {
	   // override the contructor to make it private
	  }
	  private static Earth ref;

	  // make the method thread safe
	  public synchronized static Earth getEarth()
	  {
	    if (ref == null)
	        // lazy initialization
	        ref = new Earth();		
	    return ref;
	  }

	  public Object clone()
		throws CloneNotSupportedException
	  {
	    throw new CloneNotSupportedException(); 
	  }

	  private String currentCondition="I was beatiful and clean";
	  
	  public String getCurrentCondition(){
		  return currentCondition;
	  }
	  
	  public void setCurrentCondition(String str){
		  currentCondition=str;
	  }
	  
	}