1. The intent of the Singleton is to ensure that a class has only one instance and to provide a global point of access to it.
2. A singleton is usually lazy-initialized. [lazy-initialized is like:if( filed == null) { // initial } return field; ]
3. Symchronize is important in lazy-initialize when initial Singleton (multi-thread).( see book: Concurrent Programming in Java )
4. See the source code of java.util.Collections.SingletonSet
5. UML diagram of Singleton, see pic:
6. To use Singleton, approaches are below:
6.1 use static function to create instance(and make the constractor private so that other can not use it),like:
public class Singleton {
private static Singleton s;
private Singleton(){};
// lazy-initialize
public static Singleton getInstance() {
if (s == null)
s = new Singleton();
return s;
}
}
6.2 Use static field to mark whether the single instance has been created, like:
class Singleton {
static boolean instance_flag = false; // true if 1 instance
public Singleton() {
if (instance_flag)
throw new SingletonException("Only one instance allowed"); //custom exception
else
instance_flag = true; // set flag for 1 instance
}
}
6.3 use Collection: repeat object is not allowed in some collections, can be extended to a specified number of instance of a class.
分享到:
相关推荐
这是单例设计模式的基本示例。 您将在此仓库中找到两个示例。 首先是线程安全的单例,其次不是。...Thread 2 instance is : com.caslaner.designpatterns.singleton.service.CacheUnSynchronized@35c
Creational Patterns - Singleton, Builder, Factory, Prototype, and Abstract Factory Design Patterns Chapter 3. Structural Patterns - Composite, Adapter, and Bridge Design Patterns Chapter 4. ...
Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and more Work with the behavioral patterns such as ...
git clone https://github.com/indrekru/design-patterns-spring-boot.git 您需要在您的环境中安装Maven: Mac(自制): brew install maven Ubuntu: sudo apt-get install maven 正在安装 在环境中安装了Maven...
Spring 5 Design Patterns第五版
You know you don't want to reinvent the wheel (or worse, a flat tire), so you look to Design Patterns--the lessons learned by those who've faced the same problems. With Design Patterns, you get to ...
We've tried hard to avoid both of these categories with Design Patterns Explained Simply. This book is fast and simple way to get the idea behind each of the 29 popular design patterns. The book is ...
Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development ...
■Chapter 3: Creational Patterns: Singleton, Factory Method, and Prototype ■Chapter 4: Creational Patterns: Abstract Factory and Builder ■Chapter 5: Structural Patterns: Adapter, Bridge, Composite,...
• How Design Patterns Solve Design Problems • How to Select a Design Pattern • How to Use a Design Pattern A Case Study: Designing a Document Editor • Design Problems • Document Structure ...
Then you will move on to learn about two creational design patterns which are Singleton and Factory, and two structural patterns which are Facade and Proxy. Finally, the book also explains three ...
It starts with a general introduction to all types of programming patterns and goes on to describe 10 of the most popular design patterns in detail: Singleton, Iterator, Adapter, Decorator, State, ...
Patterns that empower, such as singleton, delegates, and the responder chain Patterns that hide complexity, including bundles, class clusters, proxies and forwarding, and controllers
the emerging object-oriented design patterns. Experienced object-oriented programmers find that certain ways of doing things work best and that these ways occur over and over again. The book shows how...
Patterns such as Visitor, Singleton, Command, and Factories Multi-method engines For each generic component, the book presents the fundamental problems and design options, and finally implements a ...
As you progress through the book, you will learn about Singleton patterns, Factory patterns, and Facade patterns in detail. After this, we'll look at how to control object access with proxy patterns....
Chapter 3: Singleton Patterns Chapter 4: Proxy Patterns Chapter 5: Decorator Patterns Chapter 6: Template Method Patterns Chapter 7: Strategy Patterns (Or, Policy Patterns) Chapter 8: Adapter Patterns...
Using the latest research in neurobiology, cognitive science, and learning theory, Head First Design Patterns will load patterns into your brain in a way that sticks. In a way that lets you put them ...
★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、TemplatMethod、Iterator、Composite、State、Proxy。 ★第12章介绍...