寻找一种模式 [英] Looking for a pattern

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

问题描述

您好,


我正在寻找一个好的模式。我有一个相当大的软件应用程序

,它使用服务管理器来提供许多服务......

配置,颜色,数据查找,单位等等。直到现在

服务经理一直是单身人士,任何想要访问

服务的人都会询问单身人士。


现在我们有一个新的要求...在不同配置的同一应用程序空间中运行

软件的多个实例。

< sarcasm> shocking< / sarcasm>


所以,现在我需要考虑一个好的设计模式来帮助我。

我只能提出两个尴尬的选择:


1.将服务管理器密钥传递给

需要访问服务管理器的每个类的每个构造函数。该类可以转到单例

来按键询问服务管理器的实例。这是尴尬的
我不喜欢它。


2.创建一个获取服务的界面,并且每个对象都在

对象树实现接口。传递父母对象

对孩子的引用并实现接口,以便它们一直爬到树根节点,以获得存储在根节点中的

服务管理器的实例。这样更好,但仍然很尴尬。


是否有更好的设计模式可以满足我的需求?我使用.NET C#

,虽然它不应该太重要(除非.NET

已经有我可以利用的服务)。


谢谢,

Brian

Hello,

I am looking for a good pattern. I have a rather large software app
that makes use of a service manager for its many services...
configuration, colors, data lookup, units, etc. Up until now the
service manager has been a singleton and anyone who wants access to a
service just asks the singleton.

Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>

So, now I need to think about a good design pattern to help me here.
I can only come up with two awkward options:

1. Pass a service manager key to every constructor of every class that
needs access to the service manager. The class can go to a singleton
to ask for the instance of the service manager by key. This is
awkward and I don''t like it.

2. Create an interface for getting a service, and have every object in
the object tree implement the interface. Pass a "parent" object
reference to the "child" and implement the interfaces so they climb
the tree all the way to the root node to get an instance of the
service manager stored in the root node. This is better, but still
awkward.

Is there a better design pattern out there to do what I need? I am
using .NET C#, though it shouldn''t matter too much (unless .NET
already has a service I can leverage).

Thanks,
Brian

推荐答案

2007年10月31日星期三19 :30:23 -0000,Bilz< Br ********** @ gmail.com>

写道,引用或间接引用某人说:
On Wed, 31 Oct 2007 19:30:23 -0000, Bilz <Br**********@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>现在我们有了新的要求...在具有不同配置的相同应用程序空间中运行多个
软件实例。
< sarcasm> shocking< /讽刺>
>Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>



您需要某种工厂来创建服务提供商。也许

它可以缓存它们,并且如果之前已经使用了它的一套

配置,则重用现有的提供程序。


您创建一个密钥类,其中包含各种有区别的
初始化参数,然后是一个与各种

字段相关的hashCode。然后使用此密钥类为您的HashMap创建索引

预构建提供程序缓存。

-

Roedy Green加拿大心灵产品

Java词汇表
http://mindprod.com


10月31日下午4:32,Roedy Green< see_webs ... @ mindprod.com.invalid>

写道:
On Oct 31, 4:32 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:

2007年10月31日星期三19:30:23 -0000,Bilz< BrianGeni ... @ gmail.com>

写道,引用或间接引用某人说:
On Wed, 31 Oct 2007 19:30:23 -0000, Bilz <BrianGeni...@gmail.com>
wrote, quoted or indirectly quoted someone who said :

现在我们有了新的要求...运行

软件的多个实例具有不同配置的应用程序空间。

< sarcasm> shocking< / sarcasm>
Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>



您需要某种工厂来创建服务提供商。也许

它可以缓存它们,并且如果之前已经使用了它的一套

配置,则重用现有的提供程序。


您创建一个密钥类,其中包含各种有区别的
初始化参数,然后是一个与各种

字段相关的hashCode。然后使用此密钥类为您的HashMap创建索引

预构建提供程序缓存。

-

Roedy Green加拿大心灵产品

Java Glossaryhttp://mindprod.com


You need some sort of factory to create your service provider. Perhaps
it can cache them, and reuse an existing provider if its set of
configurations have already been used before.

You create a key class that contains the various distinguishing
initialisation parameters, then a hashCode that xors the various
fields. Then use this key class to create index into your HashMap
cache of pre-built providers.
--
Roedy Green Canadian Mind Products
The Java Glossaryhttp://mindprod.com



好​​的,这很好......但是所有类如何获得密钥?你把它们传递给每个构造函数,然后跟踪对象图表中的所有键吗?这就是我想要避免的......

虽然我怎么想都没办法。

Ok, that is fine... but how does all of the classes get the key? Do
you pass them in to every constructor, and keep track of the key all
the way down the object graph? This is what I am trying to avoid...
though I can''t think of a way how.


Bilz写道:
Bilz wrote:

你好,


我正在寻找一个好的模式。我有一个相当大的软件应用程序

,它使用服务管理器来提供许多服务......

配置,颜色,数据查找,单位等等。直到现在

服务经理一直是单身人士,任何想要访问

服务的人都会询问单身人士。


现在我们有一个新的要求...在不同配置的同一应用程序空间中运行

软件的多个实例。

< sarcasm> shocking< / sarcasm>


所以,现在我需要考虑一个好的设计模式来帮助我。

我只能提出两个尴尬的选择:


1.将服务管理器密钥传递给

需要访问服务管理器的每个类的每个构造函数。该类可以转到单例

来按键询问服务管理器的实例。这是尴尬的
我不喜欢它。


2.创建一个获取服务的界面,并且每个对象都在

对象树实现接口。传递父母对象

对孩子的引用并实现接口,以便它们一直爬到树根节点,以获得存储在根节点中的

服务管理器的实例。这样更好,但仍然很尴尬。


是否有更好的设计模式可以满足我的需求?我使用.NET C#

,虽然它不应该太重要(除非.NET

已经有了我可以利用的服务)。
Hello,

I am looking for a good pattern. I have a rather large software app
that makes use of a service manager for its many services...
configuration, colors, data lookup, units, etc. Up until now the
service manager has been a singleton and anyone who wants access to a
service just asks the singleton.

Now we have a new requirement... run multiple instances of the
software in the same application space with different configurations.
<sarcasm>shocking</sarcasm>

So, now I need to think about a good design pattern to help me here.
I can only come up with two awkward options:

1. Pass a service manager key to every constructor of every class that
needs access to the service manager. The class can go to a singleton
to ask for the instance of the service manager by key. This is
awkward and I don''t like it.

2. Create an interface for getting a service, and have every object in
the object tree implement the interface. Pass a "parent" object
reference to the "child" and implement the interfaces so they climb
the tree all the way to the root node to get an instance of the
service manager stored in the root node. This is better, but still
awkward.

Is there a better design pattern out there to do what I need? I am
using .NET C#, though it shouldn''t matter too much (unless .NET
already has a service I can leverage).



我更倾向于#2解决方案#1。更加灵活。


作为一个显而易见的长期解决方案。


对于脏黑客:如果软件的两个实例是实际上

在不同的线程中运行,然后你可以将每个线程注册到

a给定的软件实例并让单例基于线程创建




Arne

I would prefer solution #1 over #2. Much more flexible.

As a long time solution that seems obvious.

For a dirty hack: if the two instances of the software are actually
running in different threads, then you could register each thread to
a given instance of the software and have the singleton create
based on thread.

Arne


这篇关于寻找一种模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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