1

这实际上是非常基本的。也许有人提供了一个好的:) 解决方案。

有一个接口,比如 IComponent。

public interface IComponent {
    string GetStatus();
}

如果当前时间在上午 12:00 和上午 12:10 之间,则IComponent需要跳过一部分逻辑的几种实现。GetStatus()但是还有其他实现IComponent根本不关心任何时间间隔。

可以这么说:

public interface MyComponent : IComponent {
    public string GetStatus() {
        StringBuilder result = ...;
        ....
        if (/*current time between 12:00AM and 12:10AM */)
            result.Append("good enough");
        else {
            //calculate result
            result.Append(/*calculated result*/);
        }
        ...
        return result.ToString();
    }
}

所以我基本上需要的是封装

if (/*current time between 12:00AM and 12:10AM */)
        return "good enough";

进入某个类,我们称它为'SomeBehavior'或 smth,它可以在整个所需 IComponent的实现中重用。

如果有帮助,此条件的含义if是“跳过统计文件检查”,因此可以将其命名为例如 SkipStatFilesCheckBehavior。

虽然我也不确定命名,但这就是我在这里的原因(你可能会以某种方式命名它们比“行为”更合适)。实施它的最佳方法是什么?如何将“行为”更好地注入IComponent- 实现(例如通过构造函数或其他任何东西)?如果我将来需要一些其他“行为”,该解决方案是否可以扩展?也许将来某些“行为”将需要引用IComponent-implementation。

4

3 回答 3

0

I posted an answer using Inheritance, but you said in your comment that you don't want to use inheritance. Here is a composition example:

public class Status 
{
    virtual public string getStatus()
    {
        StringBuilder result = new StringBuilder();
        if (1 == 1) //This IF statement will compare the time
            result.Append("good enough");
        else
        {
            //calculate result
            result.Append("calculated result");
        }
        return result.ToString();
    }
}

public class Component1 : IComponent
{
    public string getStatus()
    {
        Status component = new Status();
        String Status = component.getStatus();
        //Do the rest of the getStatus specific to Component1
        return Status;
    }
}

public class Component2 : IComponent
{
    public string getStatus()
    {
        String Status = "";
        //Do the rest of the getStatus specific to Component2
        return Status;
    }
}
public interface IComponent
{
    public string getStatus();
} 

In the code above Component1 "has a" status, but component 2 does not. You could create sub classes from the Status class to extend the behaviour of getStatus().

于 2013-03-22T19:37:55.090 回答
0

也许是这样的:

interface Component {
    String status();
}
abstract class ComponentABC implements Component {
    ComponentABC() {
        this(false,false);
    }
    ComponentABC(boolean behaviour1,boolean behaviour2) {
        this.behaviour1=behaviour1;
        this.behaviour2=behaviour2;
    }
    public String status() {
        String s="component";
        if(behaviour1)
            s+=" with behaviour1";
        if(behaviour2)
            s+=" with behaviour2";
        return s;
    }
    boolean behaviour1,behaviour2;
}
class BasicComponent extends ComponentABC {}
class Behaviour1Component extends ComponentABC {
    Behaviour1Component() {
        super(true,false);
    }
}
class Behaviour2Component extends ComponentABC {
    Behaviour2Component() {
        super(false,true);
    }
}
class BehaviourBothComponent extends ComponentABC {
    BehaviourBothComponent() {
        super(true,true);
    }
}
public class So15578113 {
    public static void main(String[] args) {
        Component[] components=new Component[]{new BasicComponent(),new Behaviour1Component(),new Behaviour2Component(),new BehaviourBothComponent()};
        for(Component component:components)
            System.out.println(component.status());

    }
}
于 2013-03-22T23:27:32.233 回答
0

我会使用代理模式。据我了解,您可以这样使用它:

// common interface
public class IComponent {
    // I split getStatus, because both concrete cases have
    // common pre and post processings
    public string GetStatus() {
        StringBuilder result = ...;
        preProcess(result);
        mainProcess(result);
        postProcess(result);
        return result.ToString();
    }
    private void preProcess(StringBuilder str);
    private void postProcess(StringBuilder str);
    // proxied method
    private void mainProcess(StringBuilder str);
}

// class implementing the basic behavior
public class MyComponent : IComponent {
    private void mainProcess(StringBuilder result) {
        result.Append(/*calculated result*/);
    }
}

// proxy class that calls the basic behavior under certain conditions only
public class SkipStatFilesCheckBehavior : IComponent {
    IComponent basicBehavior;

    public SkipStatFilesCheckBehavior(IComponent newBasicBehavior) {
        basicBehavior = newBasicBehavior;
    }

    private void mainProcess(StringBuilder result) {
        if (/*current time between 12:00AM and 12:10AM */)
            result.Append("good enough");
        else { 
            basicBehavior.mainProcess(result);
        }
    }
}
于 2013-03-23T22:28:17.807 回答