经理课程的目的是什么?
我看到很多类都标有“Manager”。如何使用管理器类?
例如,它是否被这样的组合使用?:
var m: Mananger = new ManagerClass();
m.doSomething();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
管理器类是一些不适合其他地方的代码的常见垃圾场。他们也往往是或成为神类。
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.
像这样的类用于管理另一个类的一组对象,这些对象通常是资源。
例如,假设您有一个数据库连接池,每个连接都由
DBConnection
类的一个对象表示。如果您的代码需要通过连接池连接到数据库,它只会向 DBConnection_Manager 类请求新连接。为什么我们需要经理类?
Manager 类将查询其 DBConnection 对象列表,并确定其中是否有任何对象未分配,并返回一个。如果所有都已分配,它将创建一个并添加到池中(受允许的最大连接限制)或将请求放入队列,或报告失败。
所有这些功能对调用者来说都是完全隐藏的——管理池的具体细节是 Manager 类的工作。
这只是一个具体的例子,但其思想是资源管理是集中的并封装在 Manager 类中,并且用户代码仅请求“资源”。
我不确定这种方法是否有真正的“设计模式”,尽管我发现至少一个网页是这样说的:http://www.eventhelix.com/realtimemantra/ManagerDesignPattern.htm
更新:在动作脚本的情况下,这样的类可用于管理声音、事件侦听器或 GUI小部件(例如上下文菜单)
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.If your code needs to connect to DB via a pool of connections, it will merely ask
DBConnection_Manager
class for a new connection. 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. 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.
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".
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
Update: in case of actionscript, such a class could be used to manage sounds, or event listeners, or GUI widgets (e.g. context menus)
有些认为管理者是代码味道
Some consider managers to be a code smell
这通常意味着有人正在实现一种设计模式,但不知道(或不想使用)它的正式名称。您必须阅读代码才能得出任何有意义的结论。
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.
在我看来,管理器类应该在以下情况下使用:
max
或min
oneIn my opinion, manager class should be used in these conditions:
max
or themin
one