i developer works embedded devices (programmed c , assembly). c# , oop knowledge limited (although can fake way through when necessary). have 5 devices interface pc via usb. pc calculations , sends result device. same calculations performed each device, calculations done differently. each device, have c# windows forms application work , sends data , forth device. currently, i'm trying 5 different applications merged 1 can make changes, add new devices easily, , have standard user interface. problem don't know best way since don't know device used until run time. i'm trying avoid bunch of if statements , able put each device in separate file. here psudo-code of i'm talking about.
class device //this every device can { ... dowork1(); dowork2(); dowork3(); ... } class device1 { ... dowork1(); //this how work done device dowork2(); dowork3(); ... } class device2 { ... dowork1(); //this how work done device (not same way device1) dowork2(); dowork3(); } public partial class frmmain : form { private (some kind of object or something) currentdevice; public frmmain() { ... //determine device (could 1 of five) being used currentdevice = (which device being used) //could currentdevice = new device1(); } } private void button1_click() { currentdevice.dowork1(); //but device1.dowork1() or device2.dowork1(), depending on device being used (which determined in frmmain constructor) } i'm not sure, i'm thinking use interface or maybe inherit device1 class device class , override methods...but don't know how have 1 generic way of saying currentdevice.dowork1() since currentdevice device1 or device2.
any ideas appreciated. i'm using visual studio 2008 .net 3.5 on windows xp sp3 , windows 7.
i hope described problem enough. if not, or if didn't mention should, please let me know. i'm new stackoverflow , c#.
thank you,
michael
i little confused @ first because in case pc calculations devices receive result. understand need different implementations of on pc, not devices themselves.
the real trick here not using interface or inheritance - figured out. trick getting right implementation type, , use factory part.
but have decide on inheritance vs interface well.
use inheritance if "something" part of common, meaningful family. inheritance should have strong "is a" element.
otoh many objects exist might calculation might not want make family. composition useful. inheritance need have them share common base class. here can use composition allow each object use common interface allow pc perform calculation.
i suggest approach.
you should have reference common, generic interface, idocalculation, or such, defines method signature called in same way device.
next have device specific implementation interface, each device can have different implementation. create class each device type/implementation.
now trick class need without having know is. once again keep details hidden , make method calls generic, can create parameterized factory. factory accepts parameter describes device pc needs calculation for. interprets parameter , based on creates specific class implements idocalculation. returned , done.
i leave figure out how these objects need organized different assemblies...
//common interface public interface idocalculation { //use whatever method signatures need int docalculation(); } public class deviceimplementation1 : idocalculation { #region idocalculation members public int docalculation() { //device 1 specific code goes here } #endregion } public class deviceimplementation2 : idocalculation { #region idocalculation members public int docalculation() { //device 2 specific code goes here } #endregion } // simple factory not require lot of oop understanding. public class devicecalculationfactory { //return correct implementor based on device type passed in public idocalculation getcalculationimplementationinstance(string devicetype) { switch (devicetype) { case "device1": return new deviceimplementation1(); case "device2": return new deviceimplementation2(); default: //todo ??? return null; } } } // simple client calls methods , send results public class devicemanager { //to calculation, implementor correct device type factory - presumably pc knows device of interest, example "device1" public void dothecalculationthing(string devicetype) { devicecalculationfactory factory = new devicecalculationfactory(); idocalculation calculation = factory.getcalculationimplementationinstance(devicetype); int result = calculation.docalculation(); // send result device } }
Comments
Post a Comment