如何在C#中实现类之间的共享行为(没有多重继承) [英] How To Implement Shared Behavior Between Classes (Without Multiple Inheritance Of Course) in C#

查看:188
本文介绍了如何在C#中实现类之间的共享行为(没有多重继承)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:
所以几乎每个人都告诉我,我只需要重新开始我如何设计我的课程(感谢你们的方式,你的优秀答案!)。以提示,我开始广泛阅读策略模式。我想创建继承自抽象基类或类的行为类(或策略类)。然后,候选类将具有不同抽象基类/类的属性,作为行为或策略的类型。可能是这样的:

UPDATE: So pretty much everyone here has told me that I just need to start all over again on how I designed my classes (thank you folks for your excellent answers by the way!). Taking the hint, I started doing extensive reading on the strategy pattern. I want to create behavior classes (or strategy classes) that inherit from an abstract base class or classes. The Candidate class would then have properties w/ the different abstract base class/classes as the Type for the behaviors or 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's not used for elps
}

任何意见或建议?

ORIGINAL问题:

ORIGINAL Question:

我正在努力学习,变得更加精通设计模式和原则。我目前正在为几个让我痛苦的课程进行设计。这是一个非常精简的代码版本:

I am trying to learn and become more proficient at design patterns and principles. I have am currently working on a design for few classes that has stumped me. Here's a very condensed version of the code:

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属性和行为的一些实现需要是共同的或共享的。我已经阅读了关于C ++和Ruby的混合,这似乎是类似于我想做的事情。我也读过这个有趣的博客文章,讨论一个行为类型的想法,一个类可以继承多个行为,同时仍然保持一个是一个关系: http://www.deftflux.net/blog/post/A-good-design-for-多实现的inheritance.aspx 。这似乎表达了我想要的东西。任何人都可以给我一些关于如何使用良好的设计实践来完成这一点的方向?

The InternalEntryLevelCandidate class is primarily an EntryLevelCandidate but needs to share some of the implementations of InternalCandidate. I say implementations because I don't want the implementations to be different or repeated, otherwise I would use an interface for common contracts and have concrete implementations in each class. Some of the implementations of the InternalCandidate properties and behaviors need to be common or shared. I have read about C++ and Ruby mixins, which seem to be something similar to what I want to do. I also read this interesting blog post that discusses an idea for a behavior type where a class would be able to inherit multiple behaviors while still maintaining a single "is a" relationship: http://www.deftflux.net/blog/post/A-good-design-for-multiple-implementation-inheritance.aspx. This seems to convey what I am wanting. Can anyone give me some direction on how I can accomplish this using good design practices?

推荐答案

不可变数据值类。 如果您的各种候选子类中的任何属性表示某种有意义的数据值,请为所需的行为创建一个不可变类。每个不同的候选子类都可以使用数据类型,但你的代码仍然封装在数据类中。

Immutable data value classes. If any properties in your various Candidate subclasses represent some kind of meaningful data value, create an immutable class for it, with the behaviors you need. Each of your distinct Candidate subclasses can then use the data type, but your code is still encapsulated in the data classes.

扩展方法可以重载任何类。

我将避免装饰器模式,并坚持编译/反映功能。

I'd avoid the decorator pattern and stick with compiled/reflectable functionality.

构图。立即在单独的类中开发独特的行为,并围绕它们构建您的Candidate类,而不是在Candidate类中编写独特的行为,并尝试将其功能用于

Composition. Develop the unique behaviors in separate classes right away, and build your Candidate classes around them, rather than writing unique behaviors in your Candidate classes and trying to pull out their functionality for use in related classes later.

根据您如何使用类,您还可以将显式和隐式转换运算符实现为相关类型,因此不需要重新实现接口(你想避免的),你实际上可以把你的对象放入你需要的类型/实现中。

Depending on how you use the classes, you could also implement and make use of explicit and implicit conversion operators to a related type, so instead of reimplementing interfaces (which you wanted to avoid), you could actually cast your object into the type/implementation you need for whatever purpose.

另一件事我只是与最后一段相关的是,有一个租赁系统,您的类产生和适当类型的对象,允许其被操纵,然后消费以吸收更新的信息。

Another thing I just thought of, related to that last paragraph, is to have a leasing system, where your class spawns and object of the appropriate type, allows it to be manipulated, then consumes it later to assimilate the updated information.

这篇关于如何在C#中实现类之间的共享行为(没有多重继承)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆