How to implement collaborative behavior between classes (without multiple inheritance) in C #

UPDATE: So almost everyone here told me that I just need to start all over again as I developed my classes (thank you for the excellent answers, by the way!). Taking a hint, I began to do extensive reading in the strategy outline . I want to create behavior classes (or strategy classes) that inherit from an abstract base class or classes. The Candidate class will have properties w / various abstract base classes / classes Typefor both behavior and strategies. maybe something like this:

public abstract class SalaryStrategy {
    public abstract decimal Salary { get; set; }
    public abstract decimal Min { get; set; }
    public abstract decimal Mid { get; set; }
    public decimal CompaRatio {
        get {
            if (this.Mid == 0) { return 0; }
            else { return this.Salary / this.Mid; }
        }
    }
}

public class InternalCurrentSalaryStrategy {
    public override decimal Salary { get; set; }
    public override decimal Min {
        get { return this.Salary * .25m; }
        set { }
    }
    public override decimal Mid { get; set; }
}

public class Candidate {
    public int Id { get; set; }
    public string Name { get; set; }
    public SalaryStrategy CurrentSalaryStrategy { get; set; }
}

public static void Main(string[] args) {
    var internal = new Candidate();
    internal.CurrentSalaryStrategy = new InternalCurrentSalaryStrategy();
    var internalElp = new Candidate();
    internalElp.CurrentSalaryStrategy = new InternalCurrentSalaryStrategy();
    var elp = new Candidate();
    // elp.CurrentSalaryStrategy can stay null cause it not used for elps
}

Any comments or suggestions?


ORIGINAL Question:

. , . :

public class Candidate {
    public int Id { get; set; }
    public string Comments { get; set; }
    // lots more properties and behaviors...
}

public class InternalCandidate : Candidate {
    public decimal CurrentMid { get; set; }
    public decimal CurrentMax {
         get { return this.CurrentMin * 1.3m;
    }
    // lots more properties and behaviors...
}

public class EntryLevelCandidate : Candidate {
    public string Gpa { get; set; }
    // lots more properties and behaviors...
}

public class InternalEntryLevelCandidate /* what do I inherit here??? */ {
    // needs all of the properties and behaviors of
    // EntryLevelCandidate but also needs the CurrentMin and
    // CurrentMax (and possibly more) in InternalCandidate
}

InternalEntryLevelCandidate EntryLevelCandidate, InternalCandidate. , , , . InternalCandidate . ++ Ruby mixins, - , . , , , "": http://www.deftflux.net/blog/post/A-good-design-for-multiple-implementation-inheritance.aspx. , , . - - , , ?

+1
5

. - Candidate - , , , , Candidate , - .

. .

/ .

. , , .

, , , ( ) /, .

, , , , , , , , .

+5

, , , ( PDF).

, , - . , InternalCandidate GPA. InternalCandidate GPA. , InternalEntryLevelCandidate, , GPA. , , EntryLevelCandidate . "" GPA. -, , GPA .

Edit: , .

+4

:

, , , , . JP, .

, , :

  • , , Resolve , .

  • , , , , .

  • , this is IInterface . ( )

: #

+2

, . , , , , Decorator pattern .

, - - . , , . , Qi4J .

+1

I would just use a delegation template . Ultimately, I would use an interface for every single piece of functionality, and then have a specific class as a delegate for each interface. Then your final classes simply use the delegates they need and can inherit from multiple interfaces.

public class InternalEntryLevelCandidate : EntryLevelCandidate {
    private  InternalCandidate internalCandidateDelegate
        = new InternalCandidate();

    public decimal CurrentMid { 
        get { return internalCandidateDelegate.CurrentMid; }
        set { internalCandidateDelegate.CurrentMid = value; }
    }

    public decimal CurrentMax {
        get { return internalCandidateDelegate.CurrentMax }
    }
}
0
source

All Articles