View more presentations from Gopal Ji Singh
Friday, March 9, 2012
Singleton pattern
Singleton pattern
In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object.
This is useful when exactly one object is needed to coordinate actions
across the system. The concept is sometimes generalized to systems that
operate more efficiently when only one object exists, or that restrict
the instantiation to a certain number of objects.
There is criticism of the use of the singleton pattern, as some consider it an anti-pattern,
judging that it is overused, introduces unnecessary restrictions in
situations where a sole instance of a class is not actually required,
and introduces global state into an application.[1][2][3][4][5][6]
In C++ it also serves to isolate from the unpredictability of the
order of dynamic initialization, returning control to the programmer.
Common uses
- The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation.
- Facade Objects are often Singletons because only one Facade object is required.
- State objects are often Singletons.
- Singletons are often preferred to global variables because:
- They don't pollute the global name space (or, in languages with namespaces, their containing namespace) with unnecessary variables.[7]
- They permit lazy allocation and initialization, whereas global variables in many languages will always consume resources.
Structure
Implementation
Implementation of a singleton pattern must satisfy the single instance and global access principles. It requires a mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among class objects. The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made private. Note the distinction between a simple static instance of a class and a singleton: although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed. Another notable difference is that static member classes cannot implement an interface, unless that interface is simply a marker. So if the class has to realize a contract expressed by an interface, it really has to be a singleton.
The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one. If the programming language has concurrent processing capabilities the method should be constructed to execute as a mutually exclusive operation.
The classic solution to this problem is to use mutual exclusion on the class that indicates that the object is being instantiated.
Example
The Java programming language solutions provided here are all thread-safe but differ in supported language versions and lazy-loading. Since Java 5.0, the easiest way to create a Singleton is the enum type approach, given at the end of this section.
Lazy initialization
public class Singleton { private static Singleton _instance = null; private Singleton() { } public static synchronized Singleton getInstance() { if (_instance == null) { _instance = new Singleton(); } return _instance; } }
Traditional simple way
This solution is thread-safe without requiring special language constructs, but it may lack the laziness of the one above. The INSTANCE is created as soon as the Singleton class is initialized. That might even be long before getInstance() is called. It might be (for example) when some static method of the class is used. If laziness is not needed or the instance needs to be created early in the application's execution, or your class has no other static members or methods that could prompt early initialization (and thus creation of the instance), this (slightly) simpler solution can be used:
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; } }
For More Details Please Visit:
http://en.wikipedia.org/wiki/Singleton_pattern
Tuesday, March 6, 2012
Understanding the Microsoft Windows DNA Architecture
Central
to Windows DNA is the concept that applications should be logically
separated into partitions, called tiers. According to Avalani, this
benefits developers in several ways.
"Partitioning an application increases its scalability -- in other
words, the software's ability to support a large number of simultaneous
users,"
Avalani says.
"
It also makes the application more manageable and easier to update.
The three tiers of Windows DNA are:
• |
Presentation, or user interface
|
• |
Business logic
|
• |
Data storage
|
It's
important to note that these three tiers are separations within the
application. The deployment of the application can span any number of
computers. Avalani cites the example of a mobile worker using a laptop
computer. A Windows DNA-based application can run on that single
computer, providing the benefit of access to the application at any time
or any place. In the case of a large, electronic commerce Web site, the
Windows DNA-based application might be distributed across many servers
to meet that particular company's scalability requirements.
"
This explains why people sometimes talk about Windows DNA as an n-tier
or multi-tier architecture,
"Avalani points out."
They are referring to the ability to deploy a Windows DNA-based application over any number of physical computers.
"
COM: The Cornerstone of Windows DNA
Avalani
notes that Windows DNA is based on a programming model called COM
(Component Object Model). The COM model has come into widespread use
since its introduction by Microsoft and it is an integral part of many
Microsoft applications and technologies, including Internet Explorer and
the Office suite of applications.
Unlike
traditional software development, which required each application to be
built from scratch, COM allows developers to create complex applications
using a series of small software objects. Much like cars or houses are
built with standardized
"parts,"
COM lets developers make portions of their applications using
components. For example, Avalani says, a component might be a tax
calculation engine or the business rules for a price list. A growing
number of third-party vendors sell COM components.
This
approach speeds up the development process by allowing several teams to
work on separate parts at the same time. Developers can also reuse
components from one project to the next, and they can easily swap out or
update a particular component without affecting other portions of the
application. COM also offers the advantage of programming language
independence. That means developers can create COM components using the
tools and languages they're familiar with, such as Visual Basic, C, C++
and Java.
"An easy way to look at it is that
COM serves as the glue between the tiers of the architecture, allowing
Windows DNA applications to communicate in a highly distributed
environment,"
Avalani explains.
For More Details Please Visit: http://www.microsoft.com/presspass/features/1999/02-23dna2.mspx
Saturday, March 3, 2012
Making great Metro style apps
196 out of 223 rated this helpful
[This documentation is preliminary and is subject to change.]
Metro style apps are the focal point of the user experience on Windows 8 Consumer Preview, and great Metro style apps share an important set of traits that provide a consistent, elegant, and compelling user experience.
At this point, you might be asking, "OK, so what are Metro style apps and how do they differ from desktop apps?" Metro style apps are immersive and chromeless, filling the entire screen so there are no distractions. Metro style apps work together, making it easy to search, share, and send content between them. When users are connected to the internet, their apps show them the latest content so that they can stay up to date. With a connected account, users can download apps and use them on any Windows device.
You can create Metro style apps using the languages you're most familiar with, like JavaScript, C#, Visual Basic, or C++. And Windows Store delivers everything you need to sell your apps and everything your users need to get apps.
For More Details please visit: http://msdn.microsoft.com/library/windows/apps/hh464920.aspx
Subscribe to:
Posts (Atom)