是不是还在,如果这股跨请求实例的工厂? [英] Is it still a factory if it shares instances across requests?

查看:95
本文介绍了是不是还在,如果这股跨请求实例的工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,这完全是语义和命名...但我仍然执行一个工厂模式,如果它的使用,例如,返回单一实例?

也就是说,它仍然适当称之为一个工厂,如果它只是构建了一个新的实例一次:

  //我还在一个工厂?
公共静态类SomeFactory
{
  [ThreadStatic]
  私有静态对象_currentSomething;

  公共静态对象的东西
  {
     {返回_currentSomething? _currentSomething =新的对象(); }
  }
}
 

解决方案

它的看起来的工厂,我认为这是最好的相似性。好像你正试图使一个Singleton厂。但有一些关于它的感觉不对。

也许是因为它永远只能将返回一个对象......除非你打算做不同的事情。什么是你打算用这个做什么?嗯......我明白了,因为@LukeH说,一个线程辛格尔顿。

编辑: 我的个人的看法是,由于写的,它为图案的图案。在code可以做什么,但返回一个对象和的只有的对象。这是我修改了$ C $下的个人风格。请纠正我,如果我错了,我的跨pretation:

 静态无效的主要()
{
    目标OBJ1 = SomeFactory.Something;
    Console.WriteLine(OBJ1哈希:{0},obj1.GetHash code());

    对象OBJ2 = SomeFactory.Something;
    Console.WriteLine(OBJ2哈希:{0},obj2.GetHash code());

    Console.Read();
}

公共静态类SomeFactory
{
    [ThreadStatic]
    私有静态对象_currentSomething;

    公共静态对象的东西
    {
        得到
        {
            如果(_currentSomething!= NULL)
            {
                返回_currentSomething;
            }
            其他
            {
                _currentSomething =新的对象();
                Console.WriteLine(返回新的对象 - 哈希code:{0},_currentSomething.GetHash code());

                返回_currentSomething;
            }
        }
    }
}
 

Writelines确认,但基本的,我只能得到一个对象在主线程。酷...而是使用其他自定义对象是什么?

2日编辑:

我认为是这样的:

 公共静态类GenericFactory< T>其中,T:新的()
{
    [ThreadStatic]
    私有静态牛逼_currentSomething;

    公共静态对象的东西
    {
        得到
        {
            如果(_currentSomething!= NULL)
            {
                返回_currentSomething;
            }
            其他
            {
                _currentSomething =新T();
                Console.WriteLine(返回新{0}  - 哈希code:{1},_currentSomething.GetType(),_currentSomething.GetHash code());

                返回_currentSomething;
            }
        }
    }
}
 

Ok, this is entirely semantics and naming...but is my implementation still a factory pattern if it's used, for example, to return a singleton instance?

That is, is it still proper to call it a factory if it only constructs a new instance once:

// am I still a factory?
public static class SomeFactory 
{
  [ThreadStatic]
  private static object _currentSomething;

  public static object Something
  {
     get { return _currentSomething ?? _currentSomething = new object(); }
  }
}

解决方案

It looks factory and I think that is the best of the similarities. It seems like you are trying to make a Singleton Factory. But there is something about it that doesn't feel right.

Maybe because it is only ever going to return an object...unless you are going to do something different. What are you planning on doing with this? Ah...I see, as @LukeH says, a Thread Singleton.

EDIT: My personal take is that, as written, it is a pattern for a pattern. The code can do nothing but return an object and only an object. This is how I modified the code for personal style. Please correct me if I'm wrong with my interpretation:

static void Main()
{
    object obj1 = SomeFactory.Something;
    Console.WriteLine("obj1 hash:{0}", obj1.GetHashCode());

    object obj2 = SomeFactory.Something;
    Console.WriteLine("obj2 hash:{0}", obj2.GetHashCode());

    Console.Read();
}

public static class SomeFactory
{
    [ThreadStatic]
    private static object _currentSomething;

    public static object Something
    {
        get
        {
            if (_currentSomething != null)
            {
                return _currentSomething;
            }
            else
            {
                _currentSomething = new object();
                Console.WriteLine("Returning new object - hashcode: {0}", _currentSomething.GetHashCode());

                return _currentSomething;
            }
        }
    }
}

Writelines confirm, however rudimentary, that I only get one object on the main thread. Cool...but what about using other custom objects?

2nd Edit:

I would consider something like:

public static class GenericFactory<T> where T : new()
{
    [ThreadStatic]
    private static T _currentSomething;

    public static object Something
    {
        get
        {
            if (_currentSomething != null)
            {
                return _currentSomething;
            }
            else
            {
                _currentSomething = new T();
                Console.WriteLine("Returning new {0} - hashcode: {1}", _currentSomething.GetType(), _currentSomething.GetHashCode());

                return _currentSomething;
            }
        }
    }
}

这篇关于是不是还在,如果这股跨请求实例的工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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