Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

javascript - 两类关注点分离(separation of concerns on two class)

Hello I am in doubt to apply the separation of concerns concept in two classes(您好,我不确定将关注点分离概念应用于两个类别)

I basically have my class(我基本上上课了)

to create a match(建立比赛)

class Match {
    constructor (players) {
      this.id = uuid.v4 (). toString ();
      this.players = players;
      this.isActive = false;
    }
    // Match rest methods ...
  }

  module.exports = Match;

and how I need a match collection to do a socket.io logic(以及我如何需要匹配集合来执行socket.io逻辑)

or need an array of these matchs to access when needed(或需要这些匹配项的数组以在需要时进行访问)

and i make this class Matches or MatchManager:(我使此类Matches或MatchManager:)

class Matches {
    constructor() {
      this.matches = [];
    }

    addMatch(match) {
      if(match){
        this.matches.push(match);
      }
    }
    getMatch(id){
      if(id){      
        return this.matches.find((match) => match.id = match )
      }else{
        return null;
      }

    }


  }

  module.exports = Matches;

In my class match I have my isActive that I will start and finish a game using isActive setting to true or false(在课堂比赛中,我拥有isActive,我将使用isActive设置为true或false来开始和结束游戏)

so a function startMatch () and endMatch ()(所以一个函数startMatch()和endMatch())

that I would need to change my isActive to true or false, but I am unsure how to do this in a function (accessing a specific match)(我需要将isActive更改为true或false,但是我不确定如何在函数中执行此操作(访问特定匹配项))

and also with this design I have another problem(而且在这种设计下,我还有另一个问题)

and another doubt Could I create a match function to create a Match?(还有另一个疑问,我可以创建一个匹配函数来创建一个匹配吗?)

  ask by gabriel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Less is more in programming.(编程少即是多。)

Start with the simplest solution to a problem.(从最简单的问题解决方案开始。)

Complicate the problem, your solution breaks, now find the next simplest solution to that problem.(使问题复杂化,您的解决方案中断了,现在找到该问题的下一个最简单的解决方案。) Reiterate.(重申。)

If all your Matches class is do list oriented stuff, than maybe it should be a list instead.(如果您所有的Matches类都是面向列表的东西,那么应该代替它。)

I don't know how you are integrating with socket.io, but imagine the scenario where a new match is created.(我不知道您是如何与socket.io集成的,但是可以想象创建新匹配的场景。)

// Declare a global matches list, on your app entry.
matches = []

// On a new match event, create a Match, and add to list.
app.on('newMatch', function(players){
    matches.push(new Match(players))
});

Opinion(意见)

Don't make it harder for yourself by adding functionality/complexity that you MIGHT need.(通过添加您可能需要的功能/复杂性,不要让自己变得更难。)

Add it when you need it.(在需要时添加它。) Much easier to maintain, and much faster to develop.(易于维护,开发速度更快。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...