Class 层次结构:是否有更清晰的模式?

Class hierarchy: Is there a cleaner pattern for this?

我是用 ES2015 写的,但是这个问题也可以带到其他语言上。

在我的情况下,我有一个 Chat class 这样的:

// Chat.js
import { socket, config } from "./Util.js";
import User from "./User.js";
class Chat {
    constructor(socket, config) {
        // …
    }
    roomExists(room) {
        // …
    }
    createRoom(room) {
        // …
    }
}
// Make sure to export the same instance to every other module,
// as we only need one chat (resembling a singleton pattern)
export default new Chat(socket, config);

现在,这个 class 在 createRoom() 的某处使用了 User。问题是 User class 需要使用我们导出的 Chat 实例:

// User.js
import chat from "./Chat.js";
export default class User {
     join(room) {
         if (chat.roomExists(room)) {
             // …
         }
     }
}

但是现在我们在 Chat.jsUser.js 之间有一个依赖循环。此脚本不会 运行。解决这个问题的一种方法是永远不要直接导入 Chat.js,而是做这样的事情:

// Chat.js
import { socket, config } from "./Util.js";
import User from "./User.js";
class Chat {
    constructor(socket, config) {
        // Pass `this` as a reference so that we can use
        // this chat instance from within each user
        this.userReference = new User(this);
    }
    roomExists(room) {
    }
    createRoom(room) {
    }
}
// No need to export a singleton anymore, as we pass the
// reference to the chat in the User constructor

但是现在,每隔一个 class 都依赖于 Chat,并且 必须在我们实例化聊天实例后给它一个引用 。这也不干净,是吗? Chat 现在是单点故障,以后很难交换。

是否有更简洁的管理方式?

已更新

显然 User 不应该有 Chat 。所以,聊天应该是这样的

class Chat{
   //other declarations

  addUser(user,room) {}

  createSpecificRoom(user,name){}

  moveUserToRoom(user,newRoom) {}
}

如果你想从用户对象做事,我们可以使用双重调度。

class User{
  joinChat(chat,room){
      chat.addUser(this,room);
  }
}

var chat=new Chat();
var user= new User();
user.joinChat(chat,room);

但是,IMO 最好的办法是只使用 Chat 到 add/remove 用户。从语义上讲,它的工作是跟踪房间和用户。关于单例,如果您使用的是支持 DI 的框架,那么几乎所有服务都是单例,但您不应该关心它。只需在需要的地方注入 Chat