C# 在基本界面相同的情况下,适合处理“不同的”;基类很适合处理“复制”。没有理由不能同时使用这两种技术。提供的第二种方法与我自己的建议非常接近。创建一个执行settext/gettext的新类/接口,并将其实例注入需要该功能的每个类中。然后直接调用该实例。由于
C# 在基本界面相同的情况下,适合处理“不同的”;基类很适合处理“复制”。没有理由不能同时使用这两种技术。提供的第二种方法与我自己的建议非常接近。创建一个执行settext/gettext的新类/接口,并将其实例注入需要该功能的每个类中。然后直接调用该实例。由于,c#,oop,inheritance,multiple-inheritance,C#,Oop,Inheritance,Multiple Inheritance,在基本界面相同的情况下,适合处理“不同的”;基类很适合处理“复制”。没有理由不能同时使用这两种技术。提供的第二种方法与我自己的建议非常接近。创建一个执行settext/gettext的新类/接口,并将其实例注入需要该功能的每个类中。然后直接调用该实例。由于一种方法可能会直接调用另一种方法,这可能仍然感觉像是“浪费”,但您的应用程序将具有更高的可扩展性和解耦性。创建一个执行settext/gettext的新类/接口,并将其实例注入需要该功能的每个类中。然后直接调用该实例。由于一种方法可能会直接调用
在基本界面相同的情况下,适合处理“不同的”;基类很适合处理“复制”。没有理由不能同时使用这两种技术。提供的第二种方法与我自己的建议非常接近。创建一个执行settext/gettext的新类/接口,并将其实例注入需要该功能的每个类中。然后直接调用该实例。由于一种方法可能会直接调用另一种方法,这可能仍然感觉像是“浪费”,但您的应用程序将具有更高的可扩展性和解耦性。创建一个执行settext/gettext的新类/接口,并将其实例注入需要该功能的每个类中。然后直接调用该实例。由于一种方法可能会直接调用另一种方法,这可能仍然感觉像是“浪费”,但您的应用程序将更具可扩展性和解耦性。它看起来干净多了。我最喜欢这个解决方案。看起来干净多了。
public abstract class WebObject
{
string Id { get; set; }
string Name { get; set; }
public void Click() { Console.WriteLine("Clicking object"); }
public string GetAttribute() { return "An attribute"; }
public void SetAttribute(string attribute, string value)
{
//code to set attribute
}
}
interface IReadable
{
string GetText();
}
interface IWriteable
{
void SetText(string value);
}
public class TextBox: WebObject, IWriteable, IReadable
{
}
public class TextBox: WebObject, IWriteable, IReadable
{
public void SetText(string value)
{
//Implemented code
}
public string GetText()
{
//Implemented code
return "some value";
}
}
public static class Setter
{
public static void SetText(string value)
{
//Code to set text
}
}
public static class Getter
{
public static string GetText()
{
//Code to get text
return "";
}
}
public class TextBox: WebObject, IWriteable, IReadable
{
public void SetText(string value)
{
Setter.SetText(value);
}
public string GetText()
{
return Getter.GetText();
}
}
public abstract class WebObject {
protected void SetTextImpl() { /* Implementation */ }
protected void GetTextImpl() { /* Implementation */ }
}
public class TextBox: WebObject, IWriteable, IReadable {
public void SetText() { SetTextImpl(); }
public void GetText() { GetTextImpl(); }
}
public class Span: WebObject, IReadable {
public void GetText() { GetTextImpl(); }
}
public abstract class WebObject : IReadable {
protected void SetTextImpl() { /* Implementation */ }
protected void GetTextImpl() { /* Implementation */ }
// Implement IReadable -- this could be combined with GetTextImpl() but
// is implemented separately for consistency.
public void GetText() { GetTextImpl(); }
}
public class TextBox: WebObject, IWriteable {
public void SetText() { SetTextImpl(); }
}
public class Span: WebObject, IReadable {
}
public abstract class WebObjectReadWrite : WebObject, IReadable, IWritable
{
// Could be made virtual if some subclasses need to overwrite default implementation.
public void Read()
{
// Implementation
}
// Could be made virtual if some subclasses need to overwrite default implementation.
public void Write()
{
// Implementation
}
}
public class TextBox : WebObjectReadWrite
{
}
public class TextBox : WebObject, IReadable, IWriteable
{
private IReadable _readable = new TextReader();
private IWriteable _writeable = new TextWriter();
public void Read()
{
_readable.Read();
}
public void Write()
{
_writable.Write();
}
}
public class Span : WebObject, IReadable
{
// Reused class.
private IReadable _readable = new TextReader();
public void Read()
{
_readable.Read();
}
}
public class TextReader : IReadable
{
public void Read()
{
// Reusable implementation
}
}