Naming convention for non-virtual and abstract methods

前端 未结 8 1122
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 02:49

I frequently find myself creating classes which use this form (A):

abstract class Animal {
  public void Walk() {
    // TODO: do something before walking

    /         


        
相关标签:
8条回答
  • 2021-02-05 03:29

    Methods are means of taking action and going by that rule method names should be either verb or verb phrases.And its applicable to methods irrespective of where they are declared.For me Dog.Walk looks more natural than Dog.WalkInternal.And yes naming of method is more of a guideline than a design pattern :).If you are a .Net guy , then I will recommend "Framework Design GuideLines" book by Brad Adam and Krzystof Cwalina , which clearly address such problems.

    0 讨论(0)
  • 2021-02-05 03:31

    One name for this pattern is "Template Method Pattern"

    In C#, the method to be overridden by a derived class is preceded by "On". The logic behind this naming is that the derived class is custom responding to what is conceptually an internal event. This led to some confusion with C#'s other, now deprecated, use of "On" for raising events, but that's an old C# specific problem and it doesn't really exist anymore.

    class BaseClass
    {
        protected virtual void OnInitialize() {};
        private PreInitialize() {}
        private PostInitialize() {}
        void Initialize()
        {
           PreInitialize();
           OnInitialize();
           PostInitialize();
        }
    }
    
    class DerivedClass : BaseClass
    {
        protected override void OnInitialize() { /*do whatever*/ }
    }
    
    0 讨论(0)
提交回复
热议问题