1. 分离父类方法的主流程和主流程内子流程的实现
2. 适用场景
a. 子类各方法间调用顺序一致, 则可对这些方法进行封装
3. 代码
class Builder { public static void Test() { var v1 = new SubOne(); var v2 = new SubTwo(); var builder = new BuilderDemo(); builder.Builder(v1); builder.Builder(v2); } internal class BuilderDemo { public void Builder(BuilderBase builder) { builder.BuildOne(); builder.BuildTwo(); builder.BuildThree(); } } internal abstract class BuilderBase { public abstract void BuildOne(); public abstract void BuildTwo(); public abstract void BuildThree(); } internal class SubOne : BuilderBase { public override void BuildOne() { Console.WriteLine("SubOne 的 BuildOne"); } public override void BuildTwo() { Console.WriteLine("SubOne 的 BuildTwo"); } public override void BuildThree() { Console.WriteLine("SubOne 的 BuildThree"); } } internal class SubTwo : BuilderBase { public override void BuildOne() { Console.WriteLine("SubTwo 的 BuildOne"); } public override void BuildTwo() { Console.WriteLine("SubTwo 的 BuildTwo"); } public override void BuildThree() { Console.WriteLine("SubTwo 的 BuildThree"); } } }
时间: 2024-11-09 02:36:57