简体   繁体   English

清洁代码 - 以下划线开头的受保护方法的目的是什么?

[英]Clean Code - what's the purpose of protected method starting with an underscore?

I am reading Uncle Bob's Clean Code . 我正在阅读鲍勃叔叔的清洁代码 In chapter 16, the book shows how to refactor an example. 在第16章中,该书展示了如何重构一个例子。 There is one part that I cannot catch the purpose of writing in such way. 有一部分我无法达到以这种方式写作的目的。

  • What the purpose of using protected keyword here? 这里使用protected关键字的目的是什么?
  • Why do some methods like _getMinimumYear() start with an underscore? 为什么像_getMinimumYear()这样的方法以下划线开头?
  • Why do use a pair of method with same name rather than an abstract method like 为什么使用一对具有相同名称的方法而不是像抽象方法那样
    public abstract int getMinimumYear();

public abstract class DayDateFactory {

    private static DayDateFactory factory = new SpreadsheetDateFactory();
    public static void setInstance(DayDateFactory factory) {
        DayDateFactory.factory = factory;
    }

    protected abstract DayDate _makeDate(int ordinal);
    protected abstract DayDate _makeDate(int day, Month month, int year);
    protected abstract DayDate _makeDate(int day, int month, int year);
    protected abstract DayDate _makeDate(java.util.Date date);
    protected abstract int _getMinimumYear();
    protected abstract int _getMaximumYear();

    public static DayDate makeDate(int ordinal) {
      return factory._makeDate(ordinal);
    }

    public static DayDate makeDate(int day, Month month, int year) {
      return factory._makeDate(day, month, year);
    }

    public static DayDate makeDate(int day, int month, int year) {
      return factory._makeDate(day, month, year);
    }

    public static DayDate makeDate(java.util.Date date) {
      return factory._makeDate(date);
    }

    public static int getMinimumYear() {
      return factory._getMinimumYear();
    }

    public static int getMaximumYear() {
      return factory._getMaximumYear();
    }
}

    public class SpreadsheetDateFactory extends DayDateFactory {
    public DayDate _makeDate(int ordinal) {
    return new SpreadsheetDate(ordinal);
    }

    public DayDate _makeDate(int day, Month month, int year) {
    return new SpreadsheetDate(day, month, year);
    }

    public DayDate _makeDate(int day, int month, int year) {
    return new SpreadsheetDate(day, month, year);
    }

    public DayDate _makeDate(Date date) {
    final GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    return new SpreadsheetDate(
    calendar.get(Calendar.DATE),
    Month.fromInt(calendar.get(Calendar.MONTH) + 1),
    calendar.get(Calendar.YEAR));
    }

    protected int _getMinimumYear() {
    return SpreadsheetDate.MINIMUM_YEAR_SUPPORTED;
    }

    protected int _getMaximumYear() {
    return SpreadsheetDate.MAXIMUM_YEAR_SUPPORTED;
    }
}

Python uses a leading underscore to say that the method is internal, and not part of any contract with the outside world. Python使用一个前导下划线来表示该方法是内部的,而不是与外部世界签订任何合同的一部分。 It seems like Uncle Bob is doing something similar, except here of course there's no tool support. 好像鲍勃叔叔正在做类似的事情,除了这里当然没有工具支持。 Over time he has shifted his focus from writing for an audience familiar with C++ to writing for those familiar with scripting languages; 随着时间的推移,他将注意力从为熟悉C ++的观众的写作转移到为熟悉脚本语言的人写作; he's probably expecting his readers have enough familiarity with Python or Ruby to recognize this sort of thing. 他可能期望他的读者对Python或Ruby有足够的熟悉来识别这种事情。 So he is using a convention, just not a Java one. 所以他正在使用一个约定,而不是Java。

Here Uncle Bob is putting underscores on the instance methods of the factory that he introduces. 在这里,鲍勃叔叔正在对他介绍的工厂实例方法进行强调。 It seems like he's not intending that those methods be exposed (they are visible only to subclasses and to classes in the same package), subclasses of his factory will have to implement them but code outside of the package will not see them. 看起来他并不打算公开这些方法(它们只对子类和同一个包中的类可见),他工厂的子类必须实现它们,但是包外的代码不会看到它们。 He also wants to use the same names for the factory methods as he uses for the public static methods, but he needs a convention to keep them straight. 他还希望对工厂方法使用与公共静态方法相同的名称,但他需要一个约定来保持它们的正确性。 I think he's trying to minimize the potential for confusing the instance methods of the internal factory with the exposed static methods, without introducing a separate interface for the factory. 我认为他正试图尽量减少将内部工厂的实例方法与暴露的静态方法混淆的可能性,而不为工厂引入单独的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM