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

From Expertiza_Wiki
Jump to navigation Jump to search
(Replaced content with "This page has been moved to [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4h_kp http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/c...")
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Introduction==
This page has been moved to [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4h_kp http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4h_kp]
This wiki article discusses about some of the commonly used & easy to understand Desgin patterns[http://en.wikipedia.org/wiki/Design_pattern_(computer_science)] in the software industry. Specifically, we will be studying about the Singleton, Adapter, Command & Strategy patterns.
 
==Design Pattern==
 
===Definition===
===Examples===
 
==Case Study==
===Singleton===
[http://en.wikipedia.org/wiki/Singleton_pattern Singleton] is a design pattern which imposes a restriction on the class to instantiate exactly one object of that class.
 
====Implementation====
*In Java, We can create a [http://en.wikipedia.org/wiki/Thread-safe thread-safe] & [http://en.wikipedia.org/wiki/Lazy_initialization lazy version] of Singleton as follows. The comments in the code help you understand the Lazy & thread-safe aspect of it.
<nowiki>
public class Singleton {
        private static Singleton instance;
        private Singleton() {
                //Do nothing. Initialize the object only when the first time getInstance() is called.
        }
        public static synchronized Singleton getInstance() {
 
        //The keyword "synchronized" makes it thread safe so that two threads invoking getInstance() 
        //at the same time cannot create two instances when a context switch happens so as to
        //facilitate this scenario. In general this can happen even with 'n' threads invoking getInstance().
        //"synchronized" keyword is used to eliminate such scenarios.
 
                if (null == instance) {
                        instance = new Singleton();
                }
                return instance;
        }
}
</nowiki>
 
 
*In Ruby, we can achieve singleton classes by mixing-in the 'Singleton' module as follows. Here, "SingletonClass" is the name of the singleton class that is being created.
<nowiki>
require 'singleton'
class SingletonClass
    include Singleton
    ...
end
</nowiki>
 
====When should we use Singleton ? <ref>[http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton When should we use Singleton ?]</ref> ====
There is a lot of criticism to the use of singleton pattern. So, how do we decide whether an object should be singleton or not ?
For an object to be considered as a singleton object, they must satisfy three requirements:
#controls concurrent access to a shared resource.
#access to the resource will be requested from multiple, disparate parts of the system.
#there can be only one object.  
 
====Applications====
====Advantages====
*Singletons are often preferred to global variables as they don't pollute the global [http://en.wikipedia.org/wiki/Namespace namespace] with unnecessary variables & also can be instantiated only when needed unlike global variables which always consume resources.
 
====Drawbacks====
*Introduces a global state into the application.
*Can make unit testing classes in isolation difficult.
Checkout the [http://www.youtube.com/watch?v=-FRm3VPhseI this] video from Misko Hevery which explains in detail when the usage of singletons is not exercised.<ref>[http://www.youtube.com/watch?v=-FRm3VPhseI Why Singletons are bad]</ref>
 
===Adapter===
 
===Command===
 
===Strategy===
 
==Conclusion==
 
==References==
<references/>
 
==External Links==

Latest revision as of 04:54, 21 October 2011