开发者

Template pattern violates encapsulation?

开发者 https://www.devze.com 2022-12-12 05:29 出处:网络
Please take a look at this piece of code I came up with. abstract class Command { public void Execute(string[] commandParameters)

Please take a look at this piece of code I came up with.

    abstract class Command
    {
     public void Execute(string[] commandParameters)
     {
        CommandResult result = ExecuteCommand(commandParameters);
        PrintResult(result);
     }

     public abstract CommandResult ExecuteCommand(string[] commandParameters);
     public abstract void PrintResult(CommandResult result);
    }

There will b开发者_运维技巧e several commands inheriting from this Command class. Each command would override ExecuteCommand and PrintResult. Although with this design, for the client code, I am exposing both Execute and ExecuteCommand function which is wierd. I feel like I need to define a template for a function but not expose other functions which are used in that template! Refactoring fellas, my code sucks almost all the time, please let me know what might be the best way out here.


Make ExecuteCommand and PrintResult protected instead of public. By doing this, it will be visible to subclasses only, but not to general client code.

0

精彩评论

暂无评论...
验证码 换一张
取 消