1
\$\begingroup\$

Is there a better way to write it? I need the best way to write it. It is this principle? Please show me a better way

it breaks SRP?

 public class PlacementLocation
{
    public int Row { get; set; }
    public int Column { get; set; }
    public int Location { get; set; }
}

public class PlacementLocations : PlacementLocation
{
    private void SetLocationInList()
    {
        _placementLocations = new List<PlacementLocation>
            {
                new PlacementLocation {Column = 1, Row = 9, Location = 1},
                new PlacementLocation {Column = 2, Row = 9, Location = 2},
                new PlacementLocation {Column = 3, Row = 9, Location = 3},
                new PlacementLocation {Column = 4, Row = 9, Location = 4},
                new PlacementLocation {Column = 5, Row = 9, Location = 5},
            };
    }

    private List<PlacementLocation> _placementLocations;

    public PlacementLocations()
    {
        _placementLocations = new List<PlacementLocation>();
        SetLocationInList();
    }

    public Tuple<int, int> this[int location]
    {
        get { return new Tuple<int, int>(_placementLocations[location].Row, _placementLocations[location].Column); }
    }

    public int this[int row, int column]
    {
        get
        {
            PlacementLocation singleOrDefault =
                _placementLocations.SingleOrDefault(d => d.Column == column && d.Row == row);
            return singleOrDefault != null ? singleOrDefault.Location : 0;
        }
    }
}

Update :

  public class PlacementLocations
{

    #region Variables

    private readonly List<PlacementLocation> _placementLocations;

    #endregion

    #region Constructor

    public PlacementLocations(List<PlacementLocation> placementLocations)
    {
        _placementLocations = placementLocations;
    }

    #endregion

    #region Public Methods

    public Tuple<int, int> FindRowColumn(int location)
    {
        return GetRowColumn(location);
    }

    public int FindLocation(int row, int column)
    {
        return GetLocation(row, column);
    }

    #endregion

    #region Indexes

    public Tuple<int, int> this[int location]
    {
        get { return GetRowColumn(location); }
    }

    public int this[int row, int column]
    {
        get { return GetLocation(row, column); }
    }

    #endregion

    #region Private Methods

    private Tuple<int, int> GetRowColumn(int location)
    {
        return new Tuple<int, int>(_placementLocations[location].Row, _placementLocations[location].Column);
    }

    private int GetLocation(int row, int column)
    {
        PlacementLocation singleOrDefault =
            _placementLocations.SingleOrDefault(d => d.Column == column && d.Row == row);
        return singleOrDefault != null ? singleOrDefault.Location : 0;
    }

    #endregion

}

public class LocationsFactory
{
    public static PlacementLocations GetPredefinedLocations()
    {
        var placementLocationList = new List<PlacementLocation>
            {
                new PlacementLocation {Column = 1, Row = 9, Location = 1},
                new PlacementLocation {Column = 2, Row = 9, Location = 2},
                new PlacementLocation {Column = 3, Row = 9, Location = 3},
                new PlacementLocation {Column = 4, Row = 9, Location = 4},
                new PlacementLocation {Column = 5, Row = 9, Location = 5},
            };

        return new PlacementLocations(placementLocationList);
    }
}
\$\endgroup\$
  • \$\begingroup\$ why have you edited the initial question code? Now it is not clear what question is about and answers below don't make any sense... \$\endgroup\$ – IharS Aug 24 '13 at 14:16
  • \$\begingroup\$ update question . \$\endgroup\$ – Sun Rise Aug 24 '13 at 14:24
  • \$\begingroup\$ you still haven't extracted the values from the class (what is told in answers below). What will you do if you need another location values - write one more class? :) \$\endgroup\$ – IharS Aug 24 '13 at 14:30
  • \$\begingroup\$ and you don't need a specific method which does just '_placementLocations = placementLocations;' - that is usually placed directly in constructor... \$\endgroup\$ – IharS Aug 24 '13 at 14:32
  • \$\begingroup\$ The question was edited. Is it correct? \$\endgroup\$ – Sun Rise Aug 24 '13 at 14:35
2
\$\begingroup\$

The inheritance doesn't make sense. 'PlacementLocations' is the object representing a collection of 'PlacementLocation' objects.

public class PlacementLocations
{
    private List<PlacementLocation> _placementLocations;

    public PlacementLocations(List<PlacementLocation> locations)
    {
        _placementLocations = locations;
    }

    public Tuple<int, int> this[int location]
    {
        get { return new Tuple<int, int>(_placementLocations[location].Row, _placementLocations[location].Column); }
    }

    public int this[int row, int column]
    {
        get
        {
            PlacementLocation singleOrDefault =
                _placementLocations.SingleOrDefault(d => d.Column == column && d.Row == row);
            return singleOrDefault != null ? singleOrDefault.Location : 0;
        }
    }
}

Class should not contain particular initialization values: use constructor parameters instead and initialize externally:

...

var placementLocationList = new List<PlacementLocation>
{
    new PlacementLocation { Column = 1, Row = 9, Location = 1 },
    new PlacementLocation { Column = 2, Row = 9, Location = 2 },
    new PlacementLocation { Column = 3, Row = 9, Location = 3 },
    new PlacementLocation { Column = 4, Row = 9, Location = 4 },
    new PlacementLocation { Column = 5, Row = 9, Location = 5 },
};

var placementLocations = new PlacementLocations(placementLocationList);

...

Update: if you need predefined locations list you can use some sort of factory...

public class LocationsFactory
{
    public static PlacementLocations GetPredefinedLocations()
    {
        var placementLocationList = new List<PlacementLocation>
        {
            new PlacementLocation { Column = 1, Row = 9, Location = 1 },
            new PlacementLocation { Column = 2, Row = 9, Location = 2 },
            new PlacementLocation { Column = 3, Row = 9, Location = 3 },
            new PlacementLocation { Column = 4, Row = 9, Location = 4 },
            new PlacementLocation { Column = 5, Row = 9, Location = 5 },
        };

        return new PlacementLocations(placementLocationList);
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ If you make PlacementLocations implement IEnumerable<PlacementLocation> and add an Add() method, you could even write new PlancementLocations { new PlacementLocation { Column = 1, Row = 9, Location = 1 }, … }, or even new PlancementLocations { { 1, 9, 1 }, … } if that's what you prefer. \$\endgroup\$ – svick Aug 24 '13 at 12:20
  • \$\begingroup\$ Values ​​are fixed and can not be changed. \$\endgroup\$ – Sun Rise Aug 24 '13 at 13:54
  • \$\begingroup\$ @Sun Rise, As you see the Placements object doesn't have external setters. Only constructor - values won't change after initialization. You can use a factory to create predefined location sets. \$\endgroup\$ – IharS Aug 24 '13 at 14:08
  • \$\begingroup\$ I changed class. Whether this way is true? what is factory ? \$\endgroup\$ – Sun Rise Aug 24 '13 at 14:14
  • \$\begingroup\$ @Sun Rise, by factory I mean some class that will build particular 'Locations' instances for you, while the base 'Locations' class will not be tied to specific values. \$\endgroup\$ – IharS Aug 24 '13 at 14:27
0
\$\begingroup\$

Yes. Your class is not reusable, as it uses a specific initialization.

Write a basic 'PlacementLocations' and a specialized and separate class with your initialization. Or do the initialization in the class using your PlacementLocations.

The latter is the usual case.

I would only recommend writing an own class with the specific initialization if these values represent something special which will be used more often.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.