C#。如何灵活地提取公共逻辑
我有两个类,A和B。它们有很多共同的逻辑,但这种逻辑在某些地方是不同的,例如:C#。如何灵活地提取公共逻辑,c#,architecture,refactoring,C#,Architecture,Refactoring,我有两个类,A和B。它们有很多共同的逻辑,但这种逻辑在某些地方是不同的,例如: CommonLogicClass { CommonMethod() { common1 different1 common2 different2 } } 我不相信最好的方法存在,但我只是想知道什么方法是可行的 我发现只有两种相似的方法:将不同的部分传递到CommonLogicClass的构造函数中,或者使A和B实现接口并实现方法Dif
CommonLogicClass {
CommonMethod() {
common1
different1
common2
different2
}
}
我不相信最好的方法存在,但我只是想知道什么方法是可行的
我发现只有两种相似的方法:将不同的部分传递到CommonLogicClass的构造函数中,或者使A和B实现接口并实现方法Different1()和Different2(),然后使用接口代替不同的部分
这些方法存在一些问题。当我使用相同的方法创建三维C类时,我可能需要一个附加零件,如下面的示例所示:
CommonMethod() {
common1
different1
common2
different2
additionalCodeOnlyForCClass
}
在这种情况下,我必须添加空操作或向接口添加一个方法,并在除C之外的所有类中将其作为空方法实现
或者,当common2仅对a和B通用,而对C不通用时,情况可能更糟:
CommonMethod() {
common1
different1 \
different2 - (or just different1)
different3 /
}
我需要从common中排除common2,使其不同,并为除C之外的所有类添加相同的代码
所以,当一个最小的逻辑改变或增加时,总是有很多变化。提取公共逻辑的其他更灵活的方法有哪些?也许我应该学习一些模式,或者读一些书来回答类似这样的架构问题?我想您可能正在寻找模板方法模式: 创建一个抽象基类,实现公共功能。在子类中实现差异
void Main()
{
LogicBase a = new ClassA();
a.CommonMethod();
Console.WriteLine("----------------------------");
LogicBase b = new ClassB();
b.CommonMethod();
}
public class LogicBase
{
public void CommonMethod()
{
DoSomething_1();
DoSomething_2();
DoSomething_3();
}
protected virtual void DoSomething_1()
{
// Default behaviour 1
Console.WriteLine("DoSomething_1 - Hello from LogicBase");
}
protected virtual void DoSomething_2()
{
// Default behaviour 2
Console.WriteLine("DoSomething_2 - Hello from LogicBase");
}
protected virtual void DoSomething_3()
{
// Default behaviour 3
Console.WriteLine("DoSomething_3 - Hello from LogicBase");
}
}
public class ClassA : LogicBase
{
protected override void DoSomething_2()
{
// Behaviour specific to ClassA
Console.WriteLine("DoSomething_2 - Hello from ClassA");
}
}
public class ClassB : LogicBase
{
protected override void DoSomething_3()
{
// Behaviour specific to ClassB
Console.WriteLine("DoSomething_3 - Hello from ClassB");
}
}
运行该代码将提供以下输出:
DoSomething_1 - Hello from LogicBase
DoSomething_2 - Hello from ClassA
DoSomething_3 - Hello from LogicBase
----------------------------
DoSomething_1 - Hello from LogicBase
DoSomething_2 - Hello from LogicBase
DoSomething_3 - Hello from ClassB
我认为您可能正在寻找模板方法模式: 创建一个抽象基类,实现公共功能。在子类中实现差异
void Main()
{
LogicBase a = new ClassA();
a.CommonMethod();
Console.WriteLine("----------------------------");
LogicBase b = new ClassB();
b.CommonMethod();
}
public class LogicBase
{
public void CommonMethod()
{
DoSomething_1();
DoSomething_2();
DoSomething_3();
}
protected virtual void DoSomething_1()
{
// Default behaviour 1
Console.WriteLine("DoSomething_1 - Hello from LogicBase");
}
protected virtual void DoSomething_2()
{
// Default behaviour 2
Console.WriteLine("DoSomething_2 - Hello from LogicBase");
}
protected virtual void DoSomething_3()
{
// Default behaviour 3
Console.WriteLine("DoSomething_3 - Hello from LogicBase");
}
}
public class ClassA : LogicBase
{
protected override void DoSomething_2()
{
// Behaviour specific to ClassA
Console.WriteLine("DoSomething_2 - Hello from ClassA");
}
}
public class ClassB : LogicBase
{
protected override void DoSomething_3()
{
// Behaviour specific to ClassB
Console.WriteLine("DoSomething_3 - Hello from ClassB");
}
}
运行该代码将提供以下输出:
DoSomething_1 - Hello from LogicBase
DoSomething_2 - Hello from ClassA
DoSomething_3 - Hello from LogicBase
----------------------------
DoSomething_1 - Hello from LogicBase
DoSomething_2 - Hello from LogicBase
DoSomething_3 - Hello from ClassB
这也可能是一个解决方案,但我认为这个方案和我的一样灵活。也许我正在寻找的不是不可能的。。。但谢谢你的回答。这也可能是一个解决办法,但我认为这个办法和我的一样灵活。也许我正在寻找的不是不可能的。。。但是谢谢你的回答。