typescript - 何时适用单一责任原则?

我正在从事一个有 Teams 课程的项目。但是,我不知道是否应该简化这些类的属性,特别是 teamSchema 和 teamClients,并将其提取到另一个对象中。

class Team {
    teamNumber: number
    clientsSchema: Array<String>
    teamClients: Array<Client>
}

我应该提取并把它变成这个吗?

class Team {
    teamNumber: number
    teamClients: TeamClients
}

class TeamClients {
    teamClients: Array<Client>
    clientsSchema: Array<String>
}

背景:这是针对红队与蓝队网络安全竞赛的,其中每个团队都有一个团队 # 和客户,每个团队都有相同数量的客户和相同的客户模式。架构只是他们需要保护的竞争对手框的 IP(例如 10.1.1.x,其中 x 是团队编号)。

全班:

class Team {
    teamNumber: number
    teamSchema: Array<String>
    teamClients: Array<Client>

    constructor(teamNumber, teamSchema, teamClients) {
        this.teamNumber = teamNumber
        this.teamSchema = teamSchema
        this.teamClients = teamClients
    }

    // Gets all clients, if client couldn't be found it is pushed as null to the returned array
    getTeamClients(allClients) {
        var teamClients = []
        for(const box of this.teamSchema) {
            const ip = convertXtoNum(box, this.teamNumber)
            const client = getClientByIP(ip)
            teamClients.push(client)
        }
        return teamClients
    }

    hasAllSchemaClients() {
        for(const client of this.teamClients) {
            if(client == null) {
                return false
            }
        }
        return true
    }

    getLowestCheckTime() {
        const currentTime = Math.floor(new Date().getTime()/1000)
        var lowestCheckTime = currentTime
        for(const client of this.teamClients) {
            if(client.lastCheckTime < lowestCheckTime) {
                lowestCheckTime = client.lastCheckTime
            }
        }
        return lowestCheckTime
    }
}

回答1

在我看来,

getTeamClients(allClients) 应该设为私有并创建一个使用它的构造函数

hasAllSchemaClients() 应该被移动到像 TeamClients 这样的新类中,以分离并帮助未来的可读性

getLowestCheckTime() 应该成为 Team 类的私有函数,因为它的目的不是 TeamClients 的行为。