如何解决C#创建实例级接口的最佳方法
作为一个例子,假设我有一个带有一堆节点的有向无环图。我想遍历该图,但是每个节点都有一个保护条件。例如,如果节点B
的值小于10,则只能遍历节点A
。因此,我创建了一个称为IsActive
的方法,该方法特定于每个节点。我尝试使用抽象类方法对此进行建模:
abstract class Node
{
public int value;
public abstract bool IsActive();
}
class NodeB : Node
{
public override bool IsActive()
{
// return nodeA.value < 10
}
}
但是,我想避免为每个节点创建一个新类。那么在实例级别初始化/实现方法的最佳方法是什么?我做了其他一些事情,但是我真的不确定什么是“最佳”方法。
谢谢!
解决方法
您不需要很多课。您可以简单地使用Node
类,但可以对其进行修改。
public class Node
{
public int Value { get; set; } = 0;
public int LowerValueLimit { get; set; } = int.MinValue;
public int UpperValueLimit { get; set; } = int.MaxValue;
public bool IsActive()
{
return Value >= LowerValueLimit && Value <= UpperValueLimit ;
}
}
现在只需创建具有逻辑基础的节点
// is active will be true for anything between int.MinValue to 10
var nodeA = new Node() { UpperValueLimit = 10 };
// will be active for value between 50 and 60
var nodeB = new Node() { LowerValueLimit = 50,UpperValueLimit = 60 };
,
您可以在Func<bool>
类中将Node
作为属性:
public class Node
{
public int Value {get;set;}
public Func<bool> IsActiveFunction { get; set; }
public bool IsActive()
{
return IsActiveFunction.Invoke();
}
}
这样,您可以为每个节点独立定义条件:
Node nodeA = new Node();
Node nodeB = new Node();
nodeA.IsActiveFunction = () => nodeB.Value < 10;
nodeB.IsActiveFunction = () => Math.sin(nodeA.Value) == 0; // bad example,because you should not use == to compare doubles,but you get the idea
,
我会更进一步,创建可以用来创建验证逻辑的接口
using System;
using System.Collections.Generic;
using System.Linq;
namespace NodeExample
{
class Program
{
static void Main(string[] args)
{
var graphRoot = new Node(1);
var nodeA = new Node(7,new ValidateNodeBehaviour(currentNode => currentNode.Value >= 5));
var nodeB = new Node(3,new ValidateNodeBehaviour(currentNode => currentNode.Value >= 10 && nodeA.Value <= 10));
var nodeC = new Node(4);
graphRoot.Connections.Add(nodeA);
graphRoot.Connections.Add(nodeB);
nodeB.Connections.Add(nodeC);
//traverse the graph
var currentNode = graphRoot;
while (true)
{
var availableConnection = currentNode.Connections.FirstOrDefault(x => x.IsValid());
if (availableConnection == null)
break;
currentNode = availableConnection;
}
}
}
public interface IValidatableBehavior
{
public bool IsValid(Node currentNode);
}
public class ValidateNodeBehaviour : IValidatableBehavior
{
private Func<Node,bool> validateFunc;
public ValidateNodeBehaviour(Func<Node,bool> validateFunc)
{
this.validateFunc = validateFunc ?? throw new ArgumentNullException(nameof(validateFunc));
}
public bool IsValid(Node currentNode)
{
return validateFunc(currentNode);
}
}
public class Node
{
private readonly IValidatableBehavior validatableBehavior;
public int Value { get; set; }
public List<Node> Connections { get; } = new List<Node>();
public Node(int value)
{
Value = value;
}
public Node(int value,IValidatableBehavior validatableBehavior) : this(value)
{
this.validatableBehavior = validatableBehavior;
}
public bool IsValid()
{
if (validatableBehavior == null)
{
return true;
}
return validatableBehavior.IsValid(this);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。