what is singleton?

Virtinoo 2003-08-20 03:18:19
面试中问道什么是singleton,如何实现,答不上来,郁闷!!
...全文
48 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
aflyinghorse 2003-08-20
  • 打赏
  • 举报
回复
xiaoyunet(快乐的小猪)
是不是析构函数改为
static void destroy()
{
delete pa;
pa = 0;
}
OSNC_17 2003-08-20
  • 打赏
  • 举报
回复
const int*和int* const有什么区别呢?
xiaoyunet 2003-08-20
  • 打赏
  • 举报
回复
aflyinghorse() :
你的代码由bug,自己找吧。
aflyinghorse 2003-08-20
  • 打赏
  • 举报
回复
singleton类只能存在一个实例

class A
{
private:
static A * pa;
A(){}; //私有构造函数
public:
static A * create()
{
if(pa) return pa;
else return (pa=new A());
}
static void destroy(){if(pa) delete pa;}
};
A* A::pa = 0;
熊主任 2003-08-20
  • 打赏
  • 举报
回复
没什么奇怪的,上次碰着个人搞了N年VC,也没搞懂const int*和int* const的区别。
jack_wq 2003-08-20
  • 打赏
  • 举报
回复
我用了3年多的VC,也是第一次见到这个!
Schlemiel 2003-08-20
  • 打赏
  • 举报
回复
The Singleton Pattern ensures a class only has one instance, and provide a global point of access to it. It's important for some classes to have exactly one instance. How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but it doesn't keep you from instantiating multiple objects. A better solution is to make the class itself responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance. This is the Singleton pattern.
skywater 2003-08-20
  • 打赏
  • 举报
回复
singleton是类的唯一实例。singleton的访问被局限于必须通过静态方法Instance。多数情况下,singleton应该具有全局可见性,这可通过将其创建方法public来实现。和用全局变量模拟singleton不同,这种模式可以防止创建出多余的实例,同时兼具全局可见性。注意,该类的构造器被置为private,这就意味着没有任何办法可以绕过静态方法Instance来直接创建类的实例。

class Singleton
{
private static Singleton singleton = null;
public static Singleton Instance()
{
if (null == singleton)
singleton = new Singleton();
return singleton;
}
private Singleton()
{
}
}

  singleton模式的作用还不止于此,尤其是可以将其扩展,以创建类的可变数量的实例。假定有一个应用,当需要执行特定任务时就需要调度一个工作者线程。考虑到节约系统资源,我们使用singleton来实现这个线程类。不久,需要singleton线程处理的任务变得密集起来,如果我们决定扩展这个应用,我们可以很方便地增加工作者线程的数量,因为线程的创建和对它们的访问授权的所有逻辑都被定义在一个类中。

  singleton模式的另外一个优点是singleton的创建可以被延迟到真正需要的时候,正如表1所示。不管是否需要,全局变量一开始就被创建,但这个全局对象并不一定是一直都需要的。C#不支持全局变量,但还是有可能在某个方法的一开始就在堆上创建了一个对象并直到很久以后才使用它。果真如此的话,singleton模式为这种案例提供了一个优雅的解决方案。
sam1111 2003-08-20
  • 打赏
  • 举报
回复
著名的GoF的23个设计模式中的一个。主要是使特定对象的实例保持唯一
[Table of Contents] Preface to Book Foreword Guide to Readers Introduction • What Is a Design Pattern? • Design Patterns in Smalltalk MVC • Describing Design Patterns • The Catalog of Design Patterns • Organizing the Catalog • 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 • Formatting • Embellishing the User Interface • Supporting Multiple Look-and-Feel Standards • Supporting Multiple Window Systems • User Operations • Spelling Checking and Hyphenation • Summary Design Pattern Catalog Creational Patterns • Abstract Factory • Builder • Factory Method • Prototype • Singleton • Discussion of Creational Patterns Structural Patterns • Adapter • Bridge • Composite • Decorator • Facade • Flyweight • Proxy • Discussion of Structural Patterns Behavioral Patterns • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor • Discussion of Behavioral Patterns Conclusion • What to Expect from Design Patterns • A Brief History • The Pattern Community • An Invitation • A Parting Thought Glossary Guide to Notation • Class Diagram • Object Diagram • Interaction Diagram Foundation Classes • List • Iterator • ListIterator • Point • Rect Bibliography Index
Paperback: 164 pages Publisher: Packt Publishing - ebooks Account; 2 edition (February 15, 2016) Language: English ISBN-10: 178588803X ISBN-13: 978-1785888038 Key Features Understand the structural, creational, and behavioral Python design patterns Get to know the context and application of design patterns to solve real-world problems in software architecture, design, and application development Get practical exposure through sample implementations in Python v3.5 for the design patterns featured Book Description With the increasing focus on optimized software architecture and design it is important that software architects think about optimizations in object creation, code structure, and interaction between objects at the architecture or design level. This makes sure that the cost of software maintenance is low and code can be easily reused or is adaptable to change. The key to this is reusability and low maintenance in design patterns. Building on the success of the previous edition, Learning Python Design Patterns, Second Edition will help you implement real-world scenarios with Python's latest release, Python v3.5. We start by introducing design patterns from the Python perspective. 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. It also covers observer patterns, command patterns, and compound patterns. By the end of the book, you will have enhanced your professional abilities in software architecture, design, and development. What you will learn Enhance your skills to create better software architecture Understand proven solutions to commonly occurring design issues Explore the design principles that form the basis of software design, such as loose coupling, the Hollywood principle and the Open Close principle among others Delve into the object-oriented programming concepts and find out how they are used in software applications Develop an understanding of Creational Design Patterns and the different object creation methods that help you solve issues in software development Use Structural Design Patterns and find out how objects and classes interact to build larger applications Focus on the interaction between objects with the command and observer patterns Improve the productivity and code base of your application using Python design patterns

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧