简体   繁体   English

我有哪种设计模式

[英]Which design pattern do I have

I have two classes, each of them doing same thing but only differences are, that they both use different logic in some function in the code. 我有两个类,每个类都做同样的事情但只有差异,它们在代码中的某些函数中使用不同的逻辑。 Let say: 让我们说:

class A
{
    //has same fields, and method
    void GetDataTable()
    {
         //logic here changes up to table of the database and for some fields.
    }
}

class B
{
    //has same fields, and method
    void GetDataTable()
    {
         //logic here changes up to table of the database and for some fields.
    }
}

At the end of the day, I would add another same behaving class and GetDataTable method with different logic. 在一天结束时,我将添加另一个具有不同逻辑的相同行为类和GetDataTable方法。 What kind of design pattern or OO technique I have to apply for more quality code. 我需要采用什么样的设计模式或OO技术才能获得更高质量的代码。

You're probably looking for the Strategy Pattern . 你可能正在寻找战略模式

You define an interface with the method that each "logic" class has to implement. 您可以使用每个“逻辑”类必须实现的方法定义接口。 Each "logic" class will implement this interface and performs its logic in the implemented method. 每个“逻辑”类将实现此接口并在实现的方法中执行其逻辑。

interface IGetDataTable
{
    void GetDataTable();
}

class A : IGetDataTable
{
    public void GetDataTable() { /* Do logic here for class A */ }
}

class B : IGetDataTable
{
    public void GetDataTable() { /* Do logic here for class B */ }
}

You then choose the appropriate class (that implements IGetDataTable ) for your needs. 然后,根据需要选择适当的类(实现IGetDataTable )。

No pattern per se, just inheritance. 没有模式本身,只是继承。

I've used an abstract class instead of an interface because you mentioned there are common methods. 我使用了抽象类而不是接口,因为你提到了常见的方法。

public abstract class MyBaseClass
{
    // TODO: Common methods here.

    // Inheriting classes must implement this.
    public abstract void GetDataTable();
}

public class A : MyBaseClass
{
    public void GetDataTable()
    {
        // Specific implementation here.
    }
}

public class B : MyBaseClass
{
    public void GetDataTable()
    {
        // Specific implementation here.
    }
}

If you have something that uses these classes - but only the GetDataTable method then it would be good practice to have an interface. 如果你有一些东西使用这些类 - 但只有GetDataTable方法,那么拥有一个接口是一个好习惯。 IGetDataTable for example. 例如IGetDataTable

If not, there's no need, the abstract class is the interface. 如果没有,则没有必要,抽象类接口。

public interface IGetDataTable
{
    void GetDataTable();
}

public abstract class MyBaseClass : IGetDataTable
{
    // TODO: Common methods here.

    // Inheriting classes must implement this.
    public abstract void GetDataTable();
}

public class A : MyBaseClass
{
    public void GetDataTable()
    {
        // Specific implementation here.
    }
}

public class B : MyBaseClass
{
    public void GetDataTable()
    {
        // Specific implementation here.
    }
}

您可以使用策略模式 ,或者只是为两个类创建共同的抽象祖先。

As Styxxy suggest I use strategy pattern like that: 正如Styxxy建议我使用这样的策略模式:

Refactoring code so result are: 重构代码所以结果是:

-More flexible. - 更灵活。
-Can change behavior at runtime - 可以在运行时更改行为
-no code duplication - 没有代码重复

class Program
{
    static void Main(string[] args)
    {
        Apply a = new Apply(); //I will create if I dont need any logic but be able to get AB's behavior, I will call that class NoApply
        Helper(a);

    }
    static void Helper(AB myobj)
    {
        myobj.CanGetDataTable();
    }
}

abstract class AB
{
    IGetDataTable gtb;

    public virtual void LogicSetter(IGetDataTable dt)
    {
        gtb = dt;
    }

    public void CanGetDataTable()
    {
        this.gtb.GetDataTable();
    }
    public void DoSameThingBothAB()
    {
        Console.WriteLine("do same things");
    }
}

class Apply:AB
{
    public Apply()
    {
        base.LogicSetter(new LogicForA());
    }
}

public interface IGetDataTable
{
    void GetDataTable();
}

public class LogicForA:IGetDataTable
{
    public void GetDataTable()
    {
        Console.WriteLine("logic for A");
    }
}

public class LogicForB:IGetDataTable
{
    public void GetDataTable()
    {
        Console.WriteLine("logic for B");
    }
}

public class LogicforFutured:IGetDataTable
{
    public void GetDataTable()
    {
        Console.WriteLine("logic for object created in 2019");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM