LoginSignup
0
0

More than 3 years have passed since last update.

c# メモ

Last updated at Posted at 2020-01-18
using System;
using System.Collections.Generic;
using System.Linq;

public class PlayerRepository
{
    private static readonly List<Player> Players = new List<Player>();

    public IReadOnlyList<Player> GetAll()
    {
        CheckPlayers();
        return new List<Player>(Players);
    }

    public void Regist(Player p)
    {
        CheckArguments(p);
        CheckRegistYet(p);
        Players.Add(p);
    }

    public void UnRegist(Player p)
    {
        CheckArguments(p);
        CheckRegistAlready(p);
        Players.Remove(p);
    }

    public bool Exists(Player p)
    {
        return Players.Contains(p);
    }

    private void CheckPlayers()
    {
        if (Players.Any(null))
            throw new Exception("Exist null in the list.");
    }

    private void CheckArguments(params object[] args)
    {
        foreach (var arg in args)
            CheckArgment(arg);
    }

    private void CheckArgment(object arg)
    {
        if (arg == null)
            throw new ArgumentNullException();
    }

    private void CheckRegistYet(Player p)
    {
        if (Exists(p))
            throw new Exception("Player is already registed");
    }

    private void CheckRegistAlready(Player p)
    {
        if (Exists(p))
            throw new Exception("Player is not registed");
    }
}

戻り値がリストならIReadOnlyList、リストをコピーして返す。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0