Reputation: 35822
I have a board and the board has moving tiles. It is pretty straight forward that my model will have a Board class and a Tile class what are POCOs and Board "has" tiles (Board has an IList <Tile>)
I have a UI (Unity3d behaviour) Say BoardBehaviour "has a" Board. But here starts my problem. I also have TileBehaviour UI object which "has a" Tile. This seems to be redundant because:
I am a bit confused...
Any ideas how to connect the model and the actual UI classes?
Upvotes: 3
Views: 2642
Reputation: 1
I would suggest something like this: you should have Two interfaces 1. BoardBehavior 2. TilesBehavior. Implement them in Class Board and Tiles respectively. Now, Board should have List of Tiles. This problem can be solved by Association or Composition. I would suggest you should association. Which means in your Board class lets have a reference of List of Tiles which implement only TilesBehavior. I Hope I am not sounding too complex here. Let me know if I am, I will present a code for this approach to make it more clear.
Upvotes: 0
Reputation: 504
Probably, BoardBehaviour and TileBehaviour are bad classes. Let's analyze current situation. BoardBehaviour has Board. That is strange. In fact, Board should have behaviour. That's why I can propose to create base class BehaviourItem, that will have all the behaviour functionality. Board and Tile should be inherited from BehaviourItem. That should solve some complexity problems.
Upvotes: 1
Reputation: 4245
is a relationship: when you inherit from base class, the derived class is a baseclass; i.e A car is a vehicle;
has a relationship: when you say the class have something (composition) i.e the car has a engine. The engine is part of the car.
So here moving Tile is part of board. so the Board "has a" Tile.
when you implement the behaviour (Inherit property) its "is a" relationship. ther is no redundancy.
BoardBehaviour "has a" Board.
Board has the board behaviour.
Board class implements BoardBehaviour. Tile class implements TileBehaviour. Board class's inner class in Tile (or you can create new Tile object in Board if Tile is needed as seperate class).
Upvotes: 1
Reputation: 39182
Has A
A Board has Tiles. A BoardBehavior has TileBehaviors
Here's a theoretical constructor:
BoardBehavior(Board board, ...)
{
this.m_tileBehavoirs = board.Tiles
.Select(tile => new TileBehavior(tile, ...)).ToList();
}
Upvotes: 0