c# - Abstract factory with generics -


i have pretty complex domain various rules , exceptions , wonder if abstract factory may me out of jungle.

domain description

there various different hardware devices in network. of them have ip address, either of type transmitter or receiver (which can change, depending on configuration). how access them (protocol) , how these access points depends on device itself.

domain

public abstract class networkdevice {      public ipaddress ipaddress { get; set; }     public list<endpoint> endpoints { get; set; }     public networkconfiguration networkconfiguration { get; set; }  }  public abstract class endpoint {      public bool ismulticast { get; set; }     public string payloadtype { get; set; }  }  public ipcamera: networkdevice {      // ipcamera has various rtsp endpoints     // following solution acceptable v     public list<rtspendpoint> rtspendpoints {         {             return this.endpoints.where(x => x rtspendpoint);         }     }  }  public rtspendpoint : endpoint {      public string mediacontrol { get; set; }  }  public abstract specialdevice : networkdevice {      // device has rtsp , onvif endpoints     // , can either transmitter or receiver     // depending on configuration  }  public specialdevicetransmitter : specialdevice {      // has rtsp & onvif endpoints  }  public specialdevicereceiver {      // has onvif endpoints  } 

now, create new instance of specialdevicetransmitter has predefined set of 3 rtsp endpoints , 1 onvif endpoint. how can make without enums? , need factory every possible device , endpoint?

you can use reflection invoke constructors different types. e.g. every type can create constructor accepts array of endpoints , implement object creation this:

    t createdevice<t>(params endpoint[] endpts) t: class     {         constructorinfo ctor = typeof(t).getconstructor(new[] { typeof(endpoint[]) });          return ctor.invoke(new object[] { endpts}) t;      } 

same pattern can implemented various types of endpoint creation well. omitted error checks here.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -