你会认为这是单身/单身吗? [英] Would you consider this a singleton/singleton pattern?

查看:109
本文介绍了你会认为这是单身/单身吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想想在Global.asax.cs文件中我有一个实例类作为一个私有字段。让我们这样说:

  private MyClass _myClass = new MyClass(); 

我在Global上有一个名为GetMyClass()的静态方法,获取当前的HttpApplication并返回该实例。

  public static MyClass GetMyClass()
{
return((Global)HttpContext.Current.ApplicationInstance )。_我的课;
}

所以我可以通过调用Global.GetMyClass获取当前请求httpapplication上的实例()。



请记住,有多个(全局)HttpApplication。每个请求都有一个HttpApplication,它们是合并/共享的,所以在最真实的意义上,它不是一个真正的。但是它确实遵循这种模式。



所以问题是,你至少会考虑这个单例模式?



你会说不应该使用?你会不鼓励使用吗?你会说这是一个 可能 不好的做法,像一个真正的单身人士。



你能看到可能出现的任何问题从这种类型的使用场景?



或者你会说这不是一个真正的单身,所以没关系,而不是坏习惯。你会推荐这个作为半准单身人士,每个请求需要一个实例吗?您是否使用过/其他模式/建议?



你曾经使用过这样的东西吗?



<我曾经在过去的项目中使用过这个项目,但我不确定是否应该远离这种做法。我过去从来没有遇到任何问题。



请给我你的想法和意见。



我不是问单身是什么。而在许多情况下,我认为使用不当的单身不良做法。那是我。但是,这不是我想要讨论的。我正在尝试讨论这个我给出的情景。

解决方案

这是否符合单身人士的切割方式,仍然遇到与Singleton相同的问题:




  • 它是一个静态的具体参考,不能代替单独的行为或stubbed / mocked一个测试

  • 你不能子类化并保留这个行为,所以很容易规避这个例子的单例性质


Imagine in the Global.asax.cs file I had an instance class as a private field. Let's say like this:

private MyClass _myClass = new MyClass();

And I had a static method on Global called GetMyClass() that gets the current HttpApplication and returns that instance.

public static MyClass GetMyClass()
{
    return ((Global)HttpContext.Current.ApplicationInstance)._myClass;
}

So I could get the instance on the current requests httpapplication by calling Global.GetMyClass().

Keep in mind that there is more than one (Global) HttpApplication. There is an HttpApplication for each request and they are pooled/shared, so in the truest sense it is not a real singleton. But it does follow the pattern to a degree.

So as the question asked, would you consider this at the very least the singleton pattern?

Would you say it should not be used? Would you discourage its use? Would you say it's a possibly bad practice like a true singleton.

Could you see any problems that may arise from this type of usage scenario?

Or would you say it's not a true singleton, so it's OK, and not bad practice. Would you recommend this as a semi-quasi singleton where an instance per request is required? If not what other pattern/suggestion would you use/give?

Have you ever used anything such as this?

I have used this on past projects, but I am unsure if it's a practice I should stay away from. I have never had any issues in the past though.

Please give me your thoughts and opinions on this.

I am not asking what a singleton is. And I consider a singleton bad practice when used improperly which is in many many many cases. That is me. However, that is not what I am trying to discuss. I am trying to discuss THIS scenario I gave.

解决方案

Whether or not this fits the cookie-cutter pattern of a Singleton, it still suffers from the same problems as Singleton:

  • It is a static, concrete reference and cannot be substituted for separate behavior or stubbed/mocked during a test
  • You cannot subclass this and preserve this behavior, so it's quite easy to circumvent the singleton nature of this example

这篇关于你会认为这是单身/单身吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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