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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 11: Line 11:
[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.
[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 thread-safe & lazy version of Singleton as follows.
<nowiki>
public class Singleton {
        private static Singleton _instance;
        private Singleton() {  }
        public static synchronized Singleton getInstance() {
                if (null == _instance) {
                        _instance = new Singleton();
                }
                return _instance;
        }
}
</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> ====  
====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> ====  

Revision as of 02:58, 21 October 2011

Introduction

This wiki article discusses about some of the commonly used & easy to understand Desgin patterns[1] in the software industry. Specifically, we will be studying about the Singleton, Adapter, Command & Strategy patterns.

Design Pattern

Definition

Examples

Case Study

Singleton

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 thread-safe & lazy version of Singleton as follows.
public class Singleton {
        private static Singleton _instance;
 
        private Singleton() {  }
 
        public static synchronized Singleton getInstance() {
                if (null == _instance) {
                        _instance = new Singleton();
                }
                return _instance;
        }
}

When should we use Singleton ? <ref>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:

  1. controls concurrent access to a shared resource.
  2. access to the resource will be requested from multiple, disparate parts of the system.
  3. there can be only one object.

Applications

Advantages

  • Singletons are often preferred to global variables as they don't pollute the global 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 this video from Misko Hevery which explains in detail when the usage of singletons is not exercised.<ref>Why Singletons are bad</ref>

Adapter

Command

Strategy

Conclusion

References

<references/>

External Links