试图弄清楚,如果这code。通过使用一个Singleton造成任何好处 [英] Trying to figure out if this code creates any benefit by using a Singleton

查看:190
本文介绍了试图弄清楚,如果这code。通过使用一个Singleton造成任何好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目中的合作开发者之一(和previous开发人员)使用单/门面只是关于上课的每一页,有很多方法,它的内部调用,但实际上不保持数据。

I'm working on a project where one of the co-developers (and previous developers) use a Singleton/Facade for just about every page of class that has a lot of method calls inside of it, but that don't actually maintain the data.

例如:

public class FooFacade
{
    private static FooFacade m_facade = null;
    private static DataAccessManager m_dataAccessMgr = null;

    public StringBuilder Status {get; set; }

    private FooFacade()
    {
        this.Status = new StringBuilder();
    }

    public static FooFacade getInstance()
    {
        if (m_facade == null)
        {
            m_dataAccessMgr = DataAccessManager.getInstance();
            m_facade = new FooFacade();
        }

        return m_facade;
    }

    public void clearStatus()
    {
        this.Status.Remove(0, Status.Length);
    }

 public void Method1(string value1, int value2)
    {
     // DO SOMETHING
    }


 public List<string> Method2(string value1, int value2)
    {
     // DO SOMETHING ELSE
     // RETURN LIST
    }

现在,我有一个命名约定,他们有Singelton同一类的外观和事实的门面是不是一个真正的门面内的事实,某些问题。 (但是这是一个完全不同的谈话)。

Now, I have certain issues with the naming convention and the fact that they have the Singelton inside the same class as the Facade and the fact that the Facade isn't really a Facade. (but that's a whole different conversation).

所以我的问题是,是否真的有此受益。 ,开发商可以解释的最好的是,它是内存管理的更好,因为你没有不断地创造和对象的处置。

So my question is whether there really is a benefit from this. The best that the developer could explain is that it is better for memory management since you're not constantly creating and disposing of objects.

我们的应用程序不是一个企业级应用程序,我们没有与内存问题。每当该网站是缓慢的,这真的是由于数据库,而不是code。

Our application is not an enterprise level app and we don't have issues with memory. Anytime the site is slow, it's really due to the database and not the code.

感谢您的帮助。我是谁爱知道为什么让自己成为更好的开发的开发商。因为我不能在单词意义开发商得到它,我伸手你们。

Thanks for your help. I'm a developer who loves to know why to make myself a better developer. Since I can't get it from the developer in words that make sense, I'm reaching out to you guys.

谢谢,
乍得

更新时间:
由于下面的评论,我知道状态为一个严重的问题,因为它具有作为一个巨大的安全漏洞的机会。有没有当谈到内存管理,速度快等优点在一个Singleton使用这种code什么好处?还是会是更容易每次我需要时间来实例化FooFacade。

UPDATED Thanks to the comments below, I know that the status is a serious concern as it has a chance of being a huge security flaw. Is there any benefit to using this code in a Singleton when it comes to memory management, speed, etc? Or would it just be easier to instantiate FooFacade every time I need it.

推荐答案

由于您的对象有一个内部状态(状态),你是在自找麻烦。特别是,如果有史以来单从多个线程中使用(例如在Web应用程序)的code可能是行不通的。

Because your object has an internal state (Status) you are asking for trouble. Specifically, if ever the singleton is used from within multiple threads (for example in a web app) the code will probably just won't work.

只有当你有没有内部状态类使用单身。

Use singletons only if you have classes that have no internal state.

这篇关于试图弄清楚,如果这code。通过使用一个Singleton造成任何好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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