Click here to Skip to main content
15,887,027 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
QuestionStateless server definition Pin
AikinX15-May-08 4:17
AikinX15-May-08 4:17 
AnswerRe: Stateless server definition Pin
led mike15-May-08 4:53
led mike15-May-08 4:53 
GeneralRe: Stateless server definition Pin
AikinX15-May-08 6:11
AikinX15-May-08 6:11 
GeneralRe: Stateless server definition Pin
led mike15-May-08 6:29
led mike15-May-08 6:29 
GeneralRe: Stateless server definition Pin
AikinX15-May-08 20:20
AikinX15-May-08 20:20 
GeneralRe: Stateless server definition Pin
led mike16-May-08 4:39
led mike16-May-08 4:39 
AnswerRe: Stateless server definition Pin
AikinX15-May-08 6:10
AikinX15-May-08 6:10 
QuestionDelegates vs. Class Hierarchy Pin
peterchen10-May-08 22:37
peterchen10-May-08 22:37 
In a helper class, I have a dictionary (object, target), where target can be created in a very few different ways, e.g. a delegate, a type or a specific object.

Specifying a System.Type for target will be the most common option, but the caller should be able to use a custom delegate as well.

One way to implement this is an abstract base:

// ---- 1st implementation - virtual class hierarchy
// implementation 1

class TargetBase
{
  abstract object Convert(object o);
};

public class TypeAdapter
{
  Dictionary<object, TargetBase> dict;

  public void Add(Type source, Type target)       { dict.Add(source, new TypeTarget(target); }
  public void Add(Type source, MyDelegate target) { dict.Add(source, new DelegateTarget(target); }

  object Map(object o) { return dict[o.GetType()].Convert(o); }
};

(I've omitted the Typetarget, DelegateTarget implementations for brevity)

Alternatively, since a delegate is in there anyway, I could use the delegate for the other options:

// ---- 2nd implementation - using the delegates directly
// implementation 1
public class TypeAdapter
{
  Dictionary<object, MyDelegate> dict;

  public void Add(Type source, MyDelegate target) { dict.Add(source, new DelegateTarget(target); }
  public void Add(Type source, Type target)       
  { 
    dict.Add(source, delegate(object o) { Activator.CreateInstance(target, o); } 
  }

  object Map(object o) { return dict[o.GetType()](o); }
};


The current implementation uses struct containing either a delegate or a type, and acts depending on which one is not null. This is IMO quite ok, since there is only a minor chance of a 3rd option, and I can't conceive more.

I'd like to get some feedback which way you would go, and why. Topics I see are readability/maintainability and performance*



*) I claim "not a premature optimization" here, since it's intended as a library component I don't know how it will be used, and I have no feeling for the performance/overhead of virtual calls vs. delegates.

We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist


AnswerRe: Delegates vs. Class Hierarchy Pin
led mike12-May-08 4:58
led mike12-May-08 4:58 
GeneralRe: Delegates vs. Class Hierarchy Pin
peterchen12-May-08 5:17
peterchen12-May-08 5:17 
GeneralRe: Delegates vs. Class Hierarchy Pin
led mike12-May-08 5:51
led mike12-May-08 5:51 
GeneralRe: Delegates vs. Class Hierarchy Pin
peterchen12-May-08 11:54
peterchen12-May-08 11:54 
GeneralRe: Delegates vs. Class Hierarchy Pin
led mike15-May-08 6:27
led mike15-May-08 6:27 
GeneralRe: Delegates vs. Class Hierarchy Pin
peterchen15-May-08 7:10
peterchen15-May-08 7:10 
GeneralRe: Delegates vs. Class Hierarchy Pin
led mike15-May-08 7:15
led mike15-May-08 7:15 
GeneralRe: Delegates vs. Class Hierarchy [modified] Pin
peterchen15-May-08 7:26
peterchen15-May-08 7:26 
AnswerRe: Delegates vs. Class Hierarchy [modified] Pin
Roger Alsing14-May-08 23:26
Roger Alsing14-May-08 23:26 
QuestionException Handling Dilemma Pin
Brady Kelly8-May-08 4:21
Brady Kelly8-May-08 4:21 
AnswerRe: Exception Handling Dilemma Pin
Pete O'Hanlon8-May-08 4:31
mvePete O'Hanlon8-May-08 4:31 
GeneralRe: Exception Handling Dilemma Pin
Brady Kelly8-May-08 4:55
Brady Kelly8-May-08 4:55 
GeneralRe: Exception Handling Dilemma Pin
Pete O'Hanlon8-May-08 4:58
mvePete O'Hanlon8-May-08 4:58 
QuestionInstance vs Static + Option Pin
Brady Kelly7-May-08 2:49
Brady Kelly7-May-08 2:49 
AnswerRe: Instance vs Static + Option Pin
peterchen10-May-08 22:41
peterchen10-May-08 22:41 
AnswerRe: Instance vs Static + Option Pin
Member 9614-May-08 8:37
Member 9614-May-08 8:37 
GeneralRe: Instance vs Static + Option Pin
Brady Kelly14-May-08 10:23
Brady Kelly14-May-08 10:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.