oop中的多对多关系 [英] Many-to-many relationship in oop

查看:92
本文介绍了oop中的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



可以说我们有两个类,团队 >玩家




  • 任何给定的玩家可以在多个团队

  • 任何团队可以拥有尽可能多的玩家



我喜欢调用方法,如




  • playerX.getTeamList() / code>以获取他/她在
  • 中的所有团队的列表
  • teamY.getPlayerList()以获得团队中所有玩家的列表



(或有一些其他方法可以有效地实现)



我可以想到两种做法,但是他们根本不觉得好像是oop pattens。
你能想到任何好的方法,也许是设计图案?

解决方案

球员和球队之间的关系形成 双向图
期待评论(和downvotes?)!我是OOD noob。

  class MyPlayer 
{
public string Name {get;组; }

public MyPlayer(string n)
{
Name = n;
}
}

class MyTeam
{
public string Name {get;组; }

public MyTeam(string n)
{
Name = n;
}
}

class PlayerTeamPair
{
public MyPlayer Player {get;组; }
public MyTeam Team {get;组; }

public PlayerTeamPair(MyPlayer p,MyTeam t)
{
Player = p;
团队= t;
}
}

class PlayerTeamBipartiteGraph
{
public List&Player PlayerTeamPair>边缘{get;组; }

public PlayerTeamBipartiteGraph()
{
Edges = new List&Player PlayerTeamPair>();
}

public void AddPlayerAndTeam(MyPlayer p,MyTeam t)
{
Edges.Add(new PlayerTeamPair(p,t));
}

public List< MyTeam> GetTeamList(MyPlayer p)
{
var teams = from e in Edges where e.Player == p select e.Team;
return teams.ToList< MyTeam>();
}

public List< MyPlayer> GetPlayerList(MyTeam t)
{
var players = from e in edges where e.Team == t select e.Player;
return players.ToList< MyPlayer>();
}

}


类程序
{
static void Main(string [] args)
{
var G = new PlayerTeamBipartiteGraph();

MyPlayer a = new MyPlayer(A);
MyPlayer b = new MyPlayer(B);
MyPlayer c = new MyPlayer(C);
MyPlayer d = new MyPlayer(D);

MyTeam t1 = new MyTeam(T1);
MyTeam t2 = new MyTeam(T2);

G.AddPlayerAndTeam(a,t1);
G.AddPlayerAndTeam(b,t1);
G.AddPlayerAndTeam(c,t1);
G.AddPlayerAndTeam(b,t2)
G.AddPlayerAndTeam(d,t2);

G.GetTeamList(b).ForEach(t => Console.Write({0},t.Name));
Console.WriteLine();
G.GetPlayerList(t2).ForEach(p => Console.Write({0},p.Name));
Console.WriteLine();
}
}


what is best way to model many-to-many relationship?

lets say we have a two classes , Team and Player

  • any given Player can be in multiple Team s
  • any Team can have as many Player s as they like

I like to call methods like

  • playerX.getTeamList() to get the list of all the Team s he/she is in
  • teamY.getPlayerList() to get the list of all the Player s in the team

(or have some other way to do this effectively)

I can think of two ways of doing this , but they just don't feels like good oop pattens. can you think of any good ways , perhaps a design patten ?

解决方案

Relationship between players and teams form Bipartite Graph. Expecting comments(and downvotes?)! I am OOD noob.

    class MyPlayer
    {
        public string Name { get; set; }

        public MyPlayer(string n)
        {
            Name = n;
        }
    }

    class MyTeam
    {
        public string Name { get; set; }

        public MyTeam(string n)
        {
            Name = n;
        }
    }

    class PlayerTeamPair
    {
        public MyPlayer Player { get; set; }
        public MyTeam Team { get; set; }

        public PlayerTeamPair(MyPlayer p,MyTeam t)
        {
            Player = p;
            Team  = t;
        }
    }

    class PlayerTeamBipartiteGraph
    {
        public List<PlayerTeamPair> Edges { get; set; }

        public PlayerTeamBipartiteGraph()
        {
            Edges = new List<PlayerTeamPair>();
        }

        public void AddPlayerAndTeam(MyPlayer p, MyTeam t)
        {
            Edges.Add(new PlayerTeamPair(p, t));
        }

        public List<MyTeam> GetTeamList(MyPlayer p)
        {
            var teams = from e in Edges where e.Player == p select e.Team;
            return teams.ToList<MyTeam>();
        }

        public List<MyPlayer> GetPlayerList(MyTeam t)
        {
            var players = from e in Edges where e.Team == t select e.Player;
            return players.ToList<MyPlayer>();
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            var G = new PlayerTeamBipartiteGraph();

            MyPlayer a = new MyPlayer("A");
            MyPlayer b = new MyPlayer("B");
            MyPlayer c = new MyPlayer("C");
            MyPlayer d = new MyPlayer("D");

            MyTeam t1 = new MyTeam("T1");
            MyTeam t2 = new MyTeam("T2");

            G.AddPlayerAndTeam(a, t1);
            G.AddPlayerAndTeam(b, t1);
            G.AddPlayerAndTeam(c, t1);
            G.AddPlayerAndTeam(b, t2);
            G.AddPlayerAndTeam(d, t2);

            G.GetTeamList(b).ForEach(t => Console.Write(" {0} ",t.Name));
            Console.WriteLine();
            G.GetPlayerList(t2).ForEach(p => Console.Write(" {0} ",p.Name));
            Console.WriteLine();
        }
    }

这篇关于oop中的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆