A design problem involving Collections, Generics and Interfaces in C# -
There are lots of code in this post, but I really appreciate it if you read something and understood It ... and hopefully one solution will come with
Assume that I am structuring a network game where the institutions need to draw and some of them accordingly to the server Has been updated. / P>
Drawable
is in charge of drawing entities on the square screen:
class drawable {public int id {get; Set; } // I have inserted the ID instead of any other // class which is obtained from Drawable, as does not make the code of example // more complicated Public Zero Draw () {}}
The data received from the server implies IData with each code
IData
. Assume that we have the following data that will receive player
and enemy
:
interface IData {int ID {get; Set; }} Class PlayerData: idata {public id id {get; Set; } Public string name {get; Set; }} Category EnemyData: IData {public int id {get; Set; } Public perfection loss {received; Set; }}
Game organizations that update Our organizations are thus Then it is the basic framework of the game. Now, give me a As an example, say that I currently have the following entities: Now I need to put a Some of the following ( the code given below just shows what I want to do and thus it does not compile ): I have also tried to change the design of the Then I tried something like this: but it throws a compile-time error: Type 'player' to 'IUpdateable & lt; IData & gt; This is the general category 'DataHolder & lt; T & gt; What is the reason for using the parameter 'T'? Apply your T
is IData :
interface IUpdateable & lt; T & gt; Where T: IData {Zero Update (T Data); }
drawable
and updateable
:
class enemies: Dryable , IUpdateable & lt; EnemyData & gt; {Public Zune Update (Animated Data) {}} Class Player: Dryable, IUpdateable & lt; PlayerData & gt; {Public Zero Update (Player Data Data) {}}
this
dry
and updateable
objects, drawable
Key and a complex object that holds Dryer
and updateable
objects and their remote concrete IData :
class datahilder & lt; T, T1 & gt; Where T: Dryable, IUpdateable & lt; T1 & gt; Where T1: IDAT {Public T Unit} {Received; Set;} public IData data {get; Set;} Public Data Holder (T-Institution, IDT data) {entity = institution; Data = data; }}
var p1 = new player (); Var p1Data = new player data (); Var e1 = new enemy (); Var e1Data = new animation (); Var PlayerData = New DataHolder & lt; Player, PlayerData & gt; (P1, P1 data); Var EnemyData = New DataHolder & lt; Enemy, Animadata & gt; (E1, E1 data);
dictionary
which is a key ( p1.id
and E1. ID
) and their datahilder
( player data and
enemy data ) and their value.
Dictionary & lt; Int, DataHolder & gt; List = New Dictionary & lt; Int, DataHolder & gt; (); List.Add (p1.id, playerData); List Add (e1.id, enemy data); Regarding the use, I should be able to do the following:
foreach (var values in list.Values) {Var entity = value.Entity; Entity.Update (value.Data); }
DataHolder
for the following:
class datahilder & lt; T & gt; Where T: Dryable, IUpdateable & lt; IData & gt; {Receive the public T unit { Set;} public IData data {get; Set;} Public Data Holder (T-Institution, IDT data) {entity = institution; Data = data; }}
var playerData = new database holder & lt; Player & gt; (P1, p1 data); // error
Player
implies IUpdateable & lt; PlayerData & gt;
and PlayerData implies
IData . Is this an issue with variance? And is there any way around it?
dataholder
class or a non-normal class or interface , Then make that (non-normal) type of dictionary.
Comments
Post a Comment