简体   繁体   中英

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. There is one part that I cannot catch the purpose of writing in such way.

  • What the purpose of using protected keyword here?
  • Why do some methods like _getMinimumYear() start with an underscore?
  • 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. 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; he's probably expecting his readers have enough familiarity with Python or Ruby to recognize this sort of thing. So he is using a convention, just not a Java one.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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