简体   繁体   English

经理类的目的是什么?

[英]What is the purpose of a Manager Class?

I see a lot of classes labelled "Manager".我看到很多类标记为“经理”。 How is a manager class used?如何使用管理器类?

For example, does it get used by composition like this?:例如,它是否被这样的组合使用?:

var m: Mananger = new ManagerClass();
m.doSomething();

Manager classes are the common dumping ground for code that somehow didn't fit somewhere else.管理器类是不适合其他地方的代码的常见垃圾场。 They also tend to be or become god classes .他们也倾向于成为或成为神类

Classes like that are used to manage a set of objects of another class, which are often resources.像这样的类用于管理另一个类的一组对象,这些对象通常是资源。

For example, let's say you have a pool of database connections, each one represented by an object of DBConnection class.例如,假设您有一个数据库连接池,每个连接池由DBConnection类的一个对象表示。

If your code needs to connect to DB via a pool of connections, it will merely ask DBConnection_Manager class for a new connection.如果您的代码需要通过连接池连接到 DB,它只会向DBConnection_Manager类请求新连接。 Why do we need the manager class?为什么我们需要经理类?

The Manager class will consult its list of DBConnection objects, and determine if any of them is un-allocated, and return one. Manager 类将查询它的 DBConnection 对象列表,并确定它们中是否有任何一个未被分配,并返回一个。 If all are allocated, it will either create one and add to the pool (subject to max connections allowed limit) or place the request on queue, or report back failure.如果所有都分配了,它将创建一个并添加到池中(受最大连接数限制)或将请求放入队列,或报告失败。

ALL of this functionality is full hidden from the caller - the nitty-gritty details of managing the pool are the job of the Manager class.所有这些功能都对调用者完全隐藏 - 管理池的细节是 Manager 类的工作。

This is just a specific example, but the idea is that resource management is centralized and encapsulated withing a Manager class and the user code merely asks for "a resource".这只是一个具体的例子,但其思想是将资源管理集中并封装在一个 Manager 类中,用户代码仅要求“一个资源”。

I'm not sure if there's a bona-fide "design pattern" for this approach, though I found at least one web page that says so: http://www.eventhelix.com/realtimemantra/ManagerDesignPattern.htm我不确定这种方法是否有真正的“设计模式”,但我发现至少有一个网页这样说: http : //www.eventhelix.com/realtimemantra/ManagerDesignPattern.htm


Update: in case of actionscript, such a class could be used to manage sounds, or event listeners, or GUI widgets (eg context menus)更新:在动作脚本的情况下,这样的类可用于管理声音、事件侦听器或 GUI 小部件(例如上下文菜单)

有些人认为经理是一种代码味道

it usually means somebody is implementing a design pattern and doesnt know (or doesnt want to use) the formal name for it.这通常意味着有人正在实现一种设计模式并且不知道(或不想使用)它的正式名称。 you'll have to read the code to draw any meaningful conclusions.您必须阅读代码才能得出任何有意义的结论。

In my opinion, manager class should be used in these conditions:在我看来,经理类应该在这些情况下使用:

  1. Multiple objects need to be process需要处理多个对象
  2. Pass object list to caller is not enough, you should provide some advance functions such as select the max or the min one将对象列表传递给调用者是不够的,您应该提供一些高级功能,例如选择maxmin
  3. The objects to be managed should belong to one class (manage easily)要管理的对象应该属于一个类(易于管理)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM