Monostate vs. Singleton [英] Monostate vs. Singleton

查看:104
本文介绍了Monostate vs. Singleton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某种情况下,使用Monostate模式而不是单例来维护全局对象?

What are the scenarios when one would use a Monostate pattern instead of singleton inorder to maintain a global object?

编辑:
我知道Singleton和单调模式是。在很多情况下也实现了Singleton。只需要知道需要实现MonoState模式的场景(例子)。

I know what Singleton and Monostate patterns are. Have also implemented Singleton in quite a few scenarios. Just want to know the scenarios (case examples) where MonoState pattern needs to be implemented.

我需要在Windows窗体应用程序中维护每个屏幕的列列表。在这种情况下,我可以使用Singleton Dictionary。但是,我正在静态全局变量中存储一个列表,我想提供索引器(因为如果键不存在,我需要动态地添加新的条目),我可以在其中指定ScreenDetails.ScreenName作为关键字&获取ScreenDetails.ColumnsTable。由于索引器无法在静态类上运行,因此将模式更改为Monostate。

For eg. I need to maintain list of columns per screen in my windows forms app. I could use a Singleton Dictionary in this case. However, I am storing a List in the static global var and I wanted to provide indexers (since I need to dynamically add new entry to the list if key is not present) where I could specify ScreenDetails.ScreenName as a key & get the ScreenDetails.ColumnsTable. Since indexers can't operate on a static class I changed the pattern to Monostate.

所以我想知道哪些其他场景可能迫使用户使用Monostate而不是Singletons。

So I would like to know which other scenarios may compel a user to use Monostate instead of Singletons.

推荐答案

在它的基础上,Monostate只是Singleton的句法糖。 Monostate有趣的是当您开始子类化时,因为子类可以用不同的行为来装饰共享状态。

At its base Monostate is just syntactic sugar around Singleton. Where Monostate gets interesting is when you start subclassing, because the subclasses can decorate the shared state with different behavior.

一个简单的 - 如果有些设计而不是很有效:) - 例如:

A simple -- if somewhat contrived and not very efficient :) -- example:

public class GlobalTable implements Iterable<Key> {

  /** Shared state -- private */    
  private static final Map<Key, Value> MAP = new LinkedHashMap<Key, Value>();

  /** Public final accessor */    
  public final Value get(Key key) {
    return MAP.get(key);
  }

  /** Public final accessor */    
  public final boolean put(Key key, Value value) {
    return MAP.put(key);
  }

  /** Protected final accessor -- subclasses can use this to access
      the internal shared state */    
  protected final Set<Key> keySet() {
    return MAP.keySet();
  }

  /** Virtual -- subclasses can override for different behavior */    
  public Iterator<Key> iterator() {
    return Collections.unmodifiableSet(MAP.keySet()).iterator();
  }
}

现在如果我们想要索引访问怎么办? >

Now what if we want indexed access?

public class IndexedGlobalTable extends GlobalTable {

  public List<Key> getKeysAsList() {
    return Collections.unmodifiableList(new ArrayList<Key>(keySet()));
  }

  public Key getKeyAt(int index) {
    return getKeysAsList().get(index);
  }

  public Value getValueAt(int index) {
    return get(getKeyAt(index));
  }
}

排序键如何?

public class SortedGlobalTable extends GlobalTable {

  @Override
  public Iterator <Key> iterator() {
    return Collections
      .unmodifiableSortedSet(new TreeSet<Key>(keySet())).iterator();
  }

}

每当你需要一个或数据的其他视图,你只是实例化适当的子类。

Any time you need one or the other view of the data, you just instantiate the appropriate subclass.

当然,全局数据是否真的是一个好主意,首先是另一个问题,但至少Monostate可以让您更灵活地使用它。

Of course, whether global data is really a good idea in the first place is another question, but at least Monostate gives you more flexibility in how you use it.

这篇关于Monostate vs. Singleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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