C# 设计重构

C# 设计重构,c#,C#,我正在设计一个系统,其中出现了以下扫描。 我有一个方法f1(),它的行为在不同的实现中有所不同。 我有一个方法f2(),其行为对于所有实现都是相同的 我的设计如下: interface I1 { //Behaviour will vary across implementations void f1(); //Same behaviour for all implementations void f2(); } ab

我正在设计一个系统,其中出现了以下扫描。 我有一个方法f1(),它的行为在不同的实现中有所不同。 我有一个方法f2(),其行为对于所有实现都是相同的

我的设计如下:

interface I1
{
        //Behaviour will vary across implementations 
        void f1();
        //Same behaviour for all implementations
        void f2();

 }

    abstract class C
    {
        //Implemented in the Base class
        void f2()
        {


        }
    }

    public class C1:C,I1
    {
        //Implemented interface method
        public f1()
        {

        }

    }
    public class C2:C,I1
    {
        //Implemented  interface method
        public f1()
        {

        }

    }

设计正确吗?有人能在这个sceanario中提出合适的设计吗

您应该使用方法f1()和f2()只创建一个抽象类,如下所示:

abstract class A
{
    public abstract f1();
    protected void f2()
    {
    }
}

class B : A
{
    public override void f1()
    {
    }
}

现在,无论何时基于创建类,它们都可以为方法f1()指定自己的行为。

您应该只创建一个抽象类,方法f1()和f2()如下:

abstract class A
{
    public abstract f1();
    protected void f2()
    {
    }
}

class B : A
{
    public override void f1()
    {
    }
}

现在,无论何时基于创建类,它们都可以为方法f1()指定自己的行为。

另一种方法是使用策略模式:

public interface IF1Strategy
{
   void f1();
}

public sealed class C : I1
{
    private readonly IF1Strategy _f1Strategy;

    //strategy injected
    public C(IF1Strategy strategy)
    {
       _f1Strategy = strategy;
    }

    void f2()
    {
    }

    void f1()
    {
       //delegated to strategy
       _f1Strategy.f1();
    }
}
注意:仅适用于您的
f1
战略实施者不需要调用
f2
的情况

优点:

  • 您可以注入适当的策略
  • 您可以自己对策略进行单元测试
  • C
    可以独立于其他类进行更改,也就是说,这样可以节省您维护类继承权的时间。注意我是如何密封它的

简而言之,我选择的另一种选择是使用策略模式:

public interface IF1Strategy
{
   void f1();
}

public sealed class C : I1
{
    private readonly IF1Strategy _f1Strategy;

    //strategy injected
    public C(IF1Strategy strategy)
    {
       _f1Strategy = strategy;
    }

    void f2()
    {
    }

    void f1()
    {
       //delegated to strategy
       _f1Strategy.f1();
    }
}
注意:仅适用于您的
f1
战略实施者不需要调用
f2
的情况

优点:

  • 您可以注入适当的策略
  • 您可以自己对策略进行单元测试
  • C
    可以独立于其他类进行更改,也就是说,这样可以节省您维护类继承权的时间。注意我是如何密封它的

简而言之,我选择了

我遵循面向对象的设计原则和策略模式,为您的问题提供一个很好的解决方案。 以下是我遵循的原则: 1.喜欢组合而不是继承。 2.程序到接口,而不是实现

  public interface I1VariedBehavior
{
    void f1(); // Varies for the implementation.
}
public abstract class I1SameBehavior
{

    public void f2()
    {
        Console.WriteLine("f2 same behavior");
    }
}
public class F1Impl1 : I1VariedBehavior
{
    public void f1() // f1 is own implementation 
    {
        Console.WriteLine("F1 own implementation 1");
    }
}
public class F1Impl2 : I1VariedBehavior
{
    public void f1() // f1 is own implementation 
    {
        Console.WriteLine("F1 own implementation 2");
    }
}
public class C1 : I1SameBehavior
{
    I1VariedBehavior strategy;
    public C1(I1VariedBehavior strategy)
    {
        this.strategy = strategy;
    }
    public void f1()
    {
        strategy.f1();
    }        
}

public class Client
{
    public static void Main(String[] args)
    {
        C1 c1 = new C1(new F1Impl1());
        c1.f1();
        c1.f2();

        C1 c2 = new C1(new F1Impl2());
        c2.f1();
        c2.f2();
        Console.Read();
    }
}
输出:

F1 own implementation 1
f2 same behavior
F1 own implementation 2
f2 same behavior

希望能有所帮助。

我遵循面向对象的设计原则和策略模式,为您的问题提供一个很好的解决方案。 以下是我遵循的原则: 1.喜欢组合而不是继承。 2.程序到接口,而不是实现

  public interface I1VariedBehavior
{
    void f1(); // Varies for the implementation.
}
public abstract class I1SameBehavior
{

    public void f2()
    {
        Console.WriteLine("f2 same behavior");
    }
}
public class F1Impl1 : I1VariedBehavior
{
    public void f1() // f1 is own implementation 
    {
        Console.WriteLine("F1 own implementation 1");
    }
}
public class F1Impl2 : I1VariedBehavior
{
    public void f1() // f1 is own implementation 
    {
        Console.WriteLine("F1 own implementation 2");
    }
}
public class C1 : I1SameBehavior
{
    I1VariedBehavior strategy;
    public C1(I1VariedBehavior strategy)
    {
        this.strategy = strategy;
    }
    public void f1()
    {
        strategy.f1();
    }        
}

public class Client
{
    public static void Main(String[] args)
    {
        C1 c1 = new C1(new F1Impl1());
        c1.f1();
        c1.f2();

        C1 c2 = new C1(new F1Impl2());
        c2.f1();
        c2.f2();
        Console.Read();
    }
}
输出:

F1 own implementation 1
f2 same behavior
F1 own implementation 2
f2 same behavior

希望这会有帮助。

看起来不错。当然,没有上下文我们无法判断,但这将适用于注释中的描述。。。查看此链接。我想它会对您有所帮助。如果类必须从
C
继承才能获得
f2()
,为什么要有接口?您是否也考虑过将
f2()
实现为一种扩展方法,而不是在接口上声明它?看起来太棒了。当然,没有上下文我们无法判断,但这将适用于注释中的描述。。。查看此链接。我想它会对您有所帮助。如果类必须从
C
继承才能获得
f2()
,为什么要有接口?您是否也考虑过将
f2()
实现为扩展方法,而不是在接口上声明它?