【发布时间】:2021-03-29 06:47:00
【问题描述】:
我正在尝试重构(按概念拆分)以下类:
import myService from "./myService"
...
export default function MyService() {
if (!isInstantiated) {
myService.configurate(configuration);
// Initialize Auth and Database
this.auth = myService.auth(); // myService.auth() has to be performed after myService.configurate()
this.database = myService.database(); // myService.database() has to be performed after myService.configurate()
isInstantiated = true;
}
}
MyService.prototype.methodAssociatedWithAuthConcept = function () {
...
};
... more mothods associated with the "Auth" concept
MyService.prototype.methodAssociatedWithDatabaseConcept = function (email, password) {
...
};
... more mothods associated with the "Database" concept
将来这个类会非常大,所以把它分成其他类“Auth”和“Database”是我最好的选择,但是......myService.auth()和myService.database()必须在@987654324时执行@ 在 MyService 构造函数的单例中完成。
此外,我还必须能够从 MyService 类访问 Auth 类和 Database 类,在 MyService 单例中实例化它们,以便执行以下操作:
const myServiceObj = new MyService();
myServiceObj.database.someMethodAssociatedWithDatabaseConcept();
有什么想法吗?
【问题讨论】:
标签: javascript oop refactoring