.net - c# object behaviour with polymorphism -


i'm trying solve problem involving polymorphism that's giving me headaches. code this:

public abstract class base {    public int val {get;set;}    public virtual bool merge(base obj) { return false; } }  public class : base {    public override bool merge(base obj)    {       /* if obj of type sum "val" , return true, otherwise retur false */    } }  public class b : base {    public override bool merge(base obj)    {       /* if obj of type b sum "val" , return true, otherwise retur false */    } }  public class composite {    list<base> objects;    public void addobject(base obj)    {       foreach(base b in this.objects)       {           if (b.merge(obj))               return;       }        this.objects.add(obj);    } } 

what i'm trying achieve is, upon adding obj composite class, make object being added "merge" same type, otherwise add list. i'd code behaviour without using 'if' statements or checking object type
edit: when adding object composite class, i'd have object inside composite updated value of object being added. if have:

composite c = new composite(); c.addobject(new a(2)); c.addobject(new b(10)); // here c has { (a,2) , (b,10) } c.addobject(new a(1)); // here c should have { (a,3) , (b,10) } 

so far came complicated solution doesn't involves 'if' statements:

public abstract class base {    public int val {get;set;}    public virtual bool trymerge(base obj) { return false; }    public virtual bool mergea(a obj)  { return false; }    public virtual bool mergeb(b obj)  { return false; } }  public class : base {    public override bool trymerge(base obj)    {       return obj.mergea(this);    }     public override bool mergea(a obj)    {       obj.val += this.val;       return true;    } }  public class b : base {    public override bool merge(base obj)    {       return obj.mergeb(this);    }     public override bool mergeb(b obj)    {       obj.val += this.val;       return true;    } }  public class composite {    list<base> objects;    public void addobject(base obj)    {       foreach(base b in this.objects)       {           if (b.trymerge(obj))               return;       }        this.objects.add(obj);    } } 

any suggestion? in advance!

your approach not bad, although should declare mergea , mergeb functions protected since should not exposed, need type check , casting before calling mergea , mergeb.

another approach defining in base class (without overloading of these functions):

public abstract class base {    public int val {get;set;}    public bool merge(base obj) {       if(obj.gettype() != this.gettype())        return false;       this.val += obj.val;       return true;    } } 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -