不幸的是,您没有提供绘图的用例(以及动态/静态的上下文或关注点)。但是,Interface
或Decorator
在Composition over Inheritance (strategy pattern)
这里适用。
接口:
interface IPlot //this is optional
interface IDynamicPlot : IPlot
interface IStaticPlot : IPlot
// depending on context, the IPhasePlot can be defined as
// IDynamicPhasePlot inherited from IDynamicPlot. Same as for static.
interface IPhasePlot : IPlot
interface IRPMPlot : IPlot
class Plot : IPlot
class DynamicPlot : IDynamicPlot
//etc
策略模式:
如果派生图依赖于Dynamic
或,请使用此选项Static
。它充当外观类。
class DynamicRPMPlot{
IDynamicPlot dynamicPlot = new DynamicPlot(); //you can do constructor injection here
IRPMPlot rPMPlot = new RPMPlot(); //you can do constructor injection here
public void DoSomething(){
dynamicPlot.DoSomething();
// you can also migrate the implementation of RPMPlot to here,
//if it has different cases for dynamic or static
rPMPlot.DoSomething();
}
}
装饰图案:
如果RPM
anddynamic
被关注,请使用此选项。
class RPMPlot : IRPMPlot {
RPMPlot(IPlot decorated){
// can place null guard here
this.decorated = decorated;
}
IPlot decorated;
public void DoSomething(){
decorated.DoSomething(); //you can change the sequence
//implementation of rpmplot
}
}
void ConsumePlot(){
IPlot plot = new RPMPlot(new DynamicPlot());
plot.DoSomething();
}