我有一个类层次结构,负责将一个模型(或消息)解析或映射到另一个模型(或消息)。它具有重要的逻辑。
internal interface IMessageParser<T, K>
where T : class
where K : class
{
K Serialize(T originalMessage);
T Deserialize(K concreteMessage);
}
internal abstract class OriginalToConcreteMessageParser : IMessageParser<OriginalMessage, ConcreteMessage>
{
// some private methods that do stuff and are called in the Serialize() method
public virtual ConcreteMessage Serialize(OriginalMessage originalMessage)
{
// some stuff
}
}
有 21 个具体的解析器:
internal sealed class OriginalToConcreteMessageParserFooMessageParser : OriginalToConcreteMessageParser
{
}
internal sealed class OriginalToConcreteMessageParserBarMessageParser : OriginalToConcreteMessageParser
{
}
我想向 OriginalToConcreteMessageParser 添加一个新的私有(private)方法,并在 Serialize() 中调用它。我们将此方法称为 Baz()。
我可以创建 OriginalToConcreteBazMessageParser 并使所有 21 个具体实现都继承自它,但我宁愿不必这样做。
Baz() 提供的功能绝对是OriginalToConcreteMessageParser 的抽象层。
总而言之,我想在 OriginalToConcreteMessageParser 中注入(inject)一个方法,并在 Serialize() 中调用它,而不触及 OriginalToConcreteMessageParser。
请您参考如下方法:
我认为您可以尝试执行 decorator pattern ,或者可能是 strategy pattern
装饰器,有这样的动机,或多或少与你有的相同:
As an example, consider a window in a windowing system. To allow scrolling of the window's contents, we may wish to add horizontal or vertical scrollbars to it, as appropriate. Assume windows are represented by instances of the Window class, and assume this class has no functionality for adding scrollbars. We could create a subclass ScrollingWindow that provides them, or we could create a ScrollingWindowDecorator that adds this functionality to existing Window objects. At this point, either solution would be fine. Now let's assume we also desire the ability to add borders to our windows. Again, our original Window class has no support. The ScrollingWindow subclass now poses a problem, because it has effectively created a new kind of window. If we wish to add border support to all windows, we must create subclasses WindowWithBorder and ScrollingWindowWithBorder. Obviously, this problem gets worse with every new feature to be added. For the decorator solution, we simply create a new BorderedWindowDecorator—at runtime, we can decorate existing windows with the ScrollingWindowDecorator or the BorderedWindowDecorator or both, as we see fit.
但它可能比策略更难实现,而且可能对于您实际需要的东西来说太强大了。当一个子类将合并一个、两个或多个类的功能时,装饰器是很好的,但使用确切的接口(interface),就像只有一个类一样。
通过策略,您可以轻松切换类的特定行为。当唯一认为变化是一个函数时是好的,并且行为通常不是组合的,而只是不同实现之间的不同。可以说所有的类都有一个共同的行为,但是在序列化的那一刻,它们可以执行一些略有不同的操作。如何处理?好吧,你让你的 IMessageParser 能够接收一个解析策略(一个对象实现一个可能只有一个函数的 interfaz,所以你想放在 BAZ() 中的所有代码都将在策略对象中)。在每个具体类中,如果存在策略,序列化函数就会使用它。如果策略为空,则具体类仅使用默认行为。
这很好,因为您知道,您想使用 Baz() 函数为您的序列化函数添加一些功能,但仅在某些情况下,这可以解决问题。而且,在未来,它还允许您添加一些进一步的行为以在序列化期间执行,只需创建新的策略对象即可。
我会使用策略。您使用 execute 方法创建一个 SerializeStrategy 接口(interface)。然后是一个或多个实现该接口(interface)的具体类。然后在 IMessageParser 接口(interface)中定义一个 setStrategy 方法,并在基类、OriginalToConcreteMessageParser 或该级别的任何其他类中实现它,然后保存有策略对象。在子类中,只需检查是否有要使用的策略。
如果您仔细阅读该模式,并尽可能地让所有参与者解耦,您就可以构建一个 SOLID 模型,并且易于维护应用程序。
我们可以在上面的相同链接中读到:
This allows better decoupling between the behavior and the class that uses the behavior. The behavior can be changed without breaking the classes that use it, and the classes can switch between behaviors by changing the specific implementation used without requiring any significant code changes. Behaviors can also be changed at run-time as well as at design-time. For instance, a car object’s brake behavior can be changed from BrakeWithABS() to Brake() by changing the brakeBehavior member to: brakeBehavior = new Brake(); This gives greater flexibility in design and is in harmony with the Open/closed principle (OCP)
