问题描述
给定一个语义上应该是某个类型的对象的类,但是也有多个操作用于在其自己的类型的对象上操作,最好的方式是什么在这种情况下组织课堂?我发现这是一个常见的事情,当一个开发人员创建对象,但正在考虑一个程序的心态。示例:
Class $ {
private m_userData;
函数User(){}
函数GetUserData(){}
函数KillAllUsers(){}
函数MaimAllUsers(){}
}
从您的描述不太清楚,但似乎KillAllUsers和MainAllUsers方法在一组用户上运行。我建议创建一个自定义的UserCollection,将这些方法作为实例方法,或者创建它们作为静态,并传递给一组用户。在现代域模型术语中,您将处理一个UserRepository。
Given a class that is semantically supposed to be an object of a certain type, but also has multiple operations for operating on objects of its own type, what is the best way(pattern?) of organizing the class in this type of scenario? I find this to be a common occurrence when a developer creates objects but is thinking with a procedural mindset.
Example:
Class User {
private m_userData;
function User() {}
function GetUserData() {}
function KillAllUsers(){}
function MaimAllUsers(){}
}
It's not very clear from your description, but it seems that the "KillAllUsers" and "MainAllUsers" methods are operating on a set of users. I would recommend creating a custom UserCollection that has these methods as instance methods or create them as statics and pass in a collection of users. In modern domain model terms, you'd be dealing with a UserRepository.
这篇关于OO设计 - 将特定于实例的功能与特定于类的功能分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!