gpt4 book ai didi

c# - 接口(interface)不同时实现开闭原则

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:28 33 4
gpt4 key购买 nike

我有以下工厂安全系统用例。目前将系统设计为控制台应用程序。 - 可以使用人工输入来增加或减少机器的速度 - i 或 d - 当速度增加到 50 以上时,会发出警报 - 机器中有一个安全锁,可以通过人为输入再次锁定或解锁 - l 或 u - 如果机器速度增加 10 且安全锁未锁定,则会发出警报

我已经实现了该系统,但是该系统不可扩展。明天如果引入另一个安全系统,那么看起来我需要回到现有的类并进行修改。

实现开闭原则看起来很困难,因为速度增加/减少 (ISpeedController) 和机器锁定/解锁 (ISafetyLock) 等功能不共享通用接口(interface)。

public interface ISpeedController
{
void Increase();
void Decrease();
}

public interface ISafetyLock
{
void Lock();
void UnLock();
}

此外,还有一个事件速度变化,只要速度发生变化就会触发该事件以发出警报。这让事情变得更加复杂。

您能否帮我设计一下系统,以便我们可以在未来无缝添加新的安全措施而不影响当前代码?

谢谢。

最佳答案

好吧,您只需要采用约定优于配置的方法。

比如可以定义一个带注册的通用接口(interface):

using System;
using System.Linq;

public class Program
{
public static void Main()
{
var handlerType = typeof(IHandleKey);
var classes = typeof(Program).Assembly // you can get them however you want
.GetTypes()
.Where(p => handlerType.IsAssignableFrom(p) && p.IsClass)
.Select(t => (IHandleKey)Activator.CreateInstance(t)) // or use IoC to resolve them...
.ToArray();

while(true) {
var key = Console.ReadLine(); // or however you get your input
var handler = classes.FirstOrDefault(x => x.Key == key);

if (handler == null) {
Console.WriteLine("Couldn't find a handler for " + key);
} else {
handler.Handle();
}
}
}
}

public interface IHandleKey
{
String Key { get; }
void Handle();
}

public class Banana : IHandleKey
{
public String Key { get { return "u"; } }
public void Handle()
{
Console.WriteLine("I did banana work");
}
}

这样,如果您需要开发一项新功能,您只需添加一个包含有关有效 key 和实现逻辑信息的类即可。

同样,如果您不想让实例准备好处理命令,您可以将其拆分并拥有一个描述类型键的属性,如下所示:

using System;
using System.Linq;

public class Program
{
public static void Main()
{
var handlerType = typeof(IHandleKey);
var classes = typeof(Program).Assembly
.GetTypes()
.Where(p => handlerType.IsAssignableFrom(p) && p.IsClass && p.GetCustomAttributes(typeof(KeyHandlerAttribute), false).Count() > 0) // note we're checking for attribute here. This can be optimised.
.ToArray();

while(true) {
var key = Console.ReadLine(); // or however you get your input
var concreteType = classes.FirstOrDefault(x => ((KeyHandlerAttribute)(x.GetCustomAttributes(typeof(KeyHandlerAttribute), false).First())).Key == key);

if (concreteType == null) {
Console.WriteLine("Couldn't find a handler for " + key);
} else {
var handler = (IHandleKey)Activator.CreateInstance(concreteType); // or use IoC to resolve them...
handler.Handle();
}
}
}
}

public interface IHandleKey
{
void Handle();
}

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class KeyHandlerAttribute: Attribute
{
public String Key { get; set; }
}

[KeyHandler(Key = "u")]
public class Banana : IHandleKey
{
public void Handle()
{
Console.WriteLine("I did banana work");
}
}

更新这是使用第二个变体并实现共享状态和基于事件的通信的更新程序列表。

老实说,我觉得这很琐碎,所以请随时提出任何问题,因为我不确定什么看起来更令人困惑,什么不是......

using System;
using System.Linq;
using System.Collections.Generic;

namespace Whatever
{
public class Program
{
public static void Main()
{
// This part belongs to IoC as a Singleton
var state = new State();

// This part belongs to IoC as scoped services
var handlerType = typeof(IHandleKey);
var dict = new Dictionary<String, Object>();

foreach (var type in typeof(Program).Assembly
.GetTypes()
.Where(p => handlerType.IsAssignableFrom(p) && p.IsClass))
{
var attributes = type.GetCustomAttributes(typeof(KeyHandlerAttribute), false);
if (attributes.Any())
{
var attribute = (KeyHandlerAttribute)attributes.First();
var handlr = (IHandleKey)Activator.CreateInstance(type);
handlr.RegisterEvent(state);
dict.Add(attribute.Key, handlr);
}
}

// Main routine here
while (true)
{
var key = Console.ReadLine(); // or however you get your input
var handler = dict.ContainsKey(key) ? (IHandleKey)dict[key] : null;

if (handler == null)
{
Console.WriteLine("Couldn't find a handler for " + key);
}
else
{
handler.Handle();
}
}
}
}

// This class allows us to share state.
public class State : ISharedState
{
// As required by the question, this is an event.
public event EventHandler StateChanged;
public void RaiseStateChange(object sender)
{
this.StateChanged.Invoke(sender, new EventArgs());
}
}

// This makes our Handlers unit testable.
public interface ISharedState
{
event EventHandler StateChanged;
void RaiseStateChange(object sender);
}

// Familiar interface -> note how we have a 'register event' method now.
// We could instead just use a constructor on the HandlerBase. This is really dealer's choice now.
public interface IHandleKey
{
void Handle();
void RegisterEvent(ISharedState state);
}

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class KeyHandlerAttribute : Attribute
{
public String Key { get; set; }
}

// To avoid boilerplate in our handlers for registering/unregistering events we have a base handler class now.
public abstract class HandlerBase: IHandleKey
{
protected ISharedState _state;
public abstract void Handle();
public void RegisterEvent(ISharedState state)
{
this._state = state;
this._state.StateChanged += OnStateChanged;
}

public abstract void OnStateChanged(object sender, EventArgs e);

~HandlerBase()
{
this._state.StateChanged -= OnStateChanged;
}
}

// Actual handlers...
[KeyHandler(Key = "u")]
public class Banana : HandlerBase
{
public override void Handle()
{
Console.WriteLine("I did banana work");
this._state.RaiseStateChange(this);
}

public override void OnStateChanged(object sender, EventArgs e)
{
if (sender != this) // optional, in case we don't want to do this for self-raised changes
{
Console.WriteLine("State changed inside Banana handler");
}
}
}

[KeyHandler(Key = "c")]
public class Cheese : HandlerBase
{
public override void Handle()
{
Console.WriteLine("I did cheese work");
this._state.RaiseStateChange(this);
}

public override void OnStateChanged(object sender, EventArgs e)
{
if (sender != this) // optional, in case we don't want to do this for self-raised changes
{
Console.WriteLine("State changed inside cheese handler");
}
}
}
}

关于c# - 接口(interface)不同时实现开闭原则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57950541/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com