为什么单例类不能被子类? [英] Why singleton classes cannot be sub-classed?

查看:206
本文介绍了为什么单例类不能被子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历单例的负面影响。这里是我不能理解的一点。这是链接和点。


Singleton的负面



以下点针对单例模式使用:



它们偏离单一责任原则。单例类有责任沿着
创建一个具有其他业务责任的实例。但是,这个问题可以通过将创建部分委托给工厂对象来解决。



单例类不能被子类。


http://www.codeproject.com/Articles/307233/Singleton-Pattern-Positive-and-Negative-Apects

解决方案

购买或借用GoF的副本



原来的GoF书在Singleton的实现部分中说明了以下内容。注意:实例与Java getInstance()相同。


$ b $ [...] Singleton类。主要的问题不是如何定义子类,而是安装其唯一的实例,以便客户端能够使用它。实质上,引用单例实例的变量必须使用子类的实例进行初始化。最简单的技术是确定要在Singleton的 Instance 操作中使用哪个单例。示例代码中的示例显示了如何使用环境变量实现此技术。

选择Singleton子类的另一种方法是执行 Instance 出来的父类并把它放在子类中。这使得C ++程序员在链接时决定单例类的类(例如,通过链接在包含不同实现的对象文件中),但是保持它对单例的客户端隐藏。

链接方法修复了在链接时选择单例类,这使得在运行时难以选择单例类。使用条件语句确定子类更灵活,但它硬连线的可能的Singleton类的集合。两种方法在所有情况下都不够灵活。

更灵活的方法使用单身注册表。而不是使用 Instance 定义可能的Singleton类的集合,Singleton类可以在知名的注册表中通过名称注册其单例实例。

注册表字符串名称和单例之间的映射。当 Instance 需要单例时,它会查询注册表,通过名称请求单例。


GoF书继续说明注册表的工作原理。



下面是使用环境变量的示例代码:


发生时有子类...我们将通过环境变量[...]选择[subtype]。




  MazeFactory * MazeFactory :: Instance(){
if(_instance == 0){
const char * mazeStyle = getenv(MAZESTYLE);

if(strcmp(mazeStyle,bombed)== 0 {
_instance = new BombedMazeFactory;

} )== 0)}
_instance = new EnchantedMazeFactory;

// ...其他可能的子类

} else {//默认
_instance = new MazeFactory;
}
}
return _instance;
}


I was going through the negative effects of singleton. Here is one of the point that I cannot understand at all. Here is the link and the point.

Negative sides of Singleton

The following points are used against the Singleton pattern:

They deviate from the Single Responsibility Principle. A singleton class has the responsibility to create an instance of itself along with other business responsibilities. However, this issue can be solved by delegating the creation part to a factory object.

Singleton classes cannot be sub classed.

http://www.codeproject.com/Articles/307233/Singleton-Pattern-Positive-and-Negative-Aspects

解决方案

Buy or borrow a copy of GoF

The original GoF book says the following in the Implementation part of Singleton. Note: Instance is the same as the Java getInstance().

  1. Ensuring a unique instance [...]
  2. Subclassing the Singleton class. The main issue is not so much defining the subclass but installing its unique instance so that clients will be able to use it. In essence, the variable that refers to the singleton instance must get initialized with an instance of the subclass. The simplest technique is to determine which singleton you want to use in the Singleton's Instance operation. An example in the Sample Code shows how to implement this technique with environment variables.

    Another way to chose the subclass of Singleton is to take the implementation of Instance out of the parent class and put it in the subclass. That lets a C++ programmer decide the class of singleton at link-time (e.g., by linking in an object file containing a different implementation) but keeps it hidden from the clients of the singleton.

    The link approach fixes the choice of singleton class at link-time, which makes it hard to choose the singleton class at run-time. Using conditional statements to determine the subclass is more flexible, but it hard-wires the set of possible Singleton classes. Neither approach is flexible enough in all cases.

    A more flexible approach uses a registry of singletons. Instead of having Instance define the set of possible Singleton classes, the Singleton classes can register their singleton instance by name in a well-known registry.

    The registry maps between string names and singletons. When Instance needs a singleton, it consults the registry, asking for the singleton by name.

The GoF book goes on to show how registries work.

Here's the Sample Code using the environment variable:

Now let's consider what happens when there are subclasses... We'll select the [subtype] through an environment variable [...]

MazeFactory* MazeFactory::Instance () {
    if (_instance == 0) {
        const char* mazeStyle = getenv("MAZESTYLE");

        if (strcmp(mazeStyle, "bombed") == 0 {
            _instance = new BombedMazeFactory;

        } else if (strcmp(mazeStyle, "enchanted") == 0) }
            _instance = new EnchantedMazeFactory;

        // ... other possible subclasses

        } else {     // default
            _instance = new MazeFactory;
        }
    }
    return _instance;
}                

这篇关于为什么单例类不能被子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆