TheIntroduction LogicalDesign PhysicalDesign PatternFoundations GuidelinesAndMyths ReferenceImplementation DependencyPatterns UsabilityPatterns ExtensibilityPatterns MaintenancePatterns AntiPatterns UtilitiesAndTools
StatementMinimize visibility of methods, attributes, and classes.
DescriptionClass methods should not be made public until you need to invoke the methods from classes residing in other packages. You should default method visibility to private, and gradually increase visibility on demand. You should also avoid generating default accessor/mutator classes for attributes. Most IDE's support this feature, so while it's a pretty easy thing to do, but it's not always the wisest. The public methods on a class define the class' API.
Implementation VariationsThose methods that are public should be ClearMethods. This will help with ease of use and understandability. Example: calculateDiscount vs. getDiscount
Sample CodeIllustrate how to expose the accessor/mutator methods by defining an interface exposing the business functions and a concrete subclass exposing the getter/setter methods to the classes that need to use them. abstract class Employee
- abstract calculatePay - abstract changeNameclass SalaryEmp extends Employee
- calculatePay - changeName - getSalaryclass CommissionEmp extends Employee
- calculatePay - changeName - getCommissioninterface PayCalculator
- calculatePay(Employee)class SalaryPayCalculator
- calculatePay(Employee employee) //SalaryEmp emp = (SalaryEmp) employee;
Related Pattern(s)HollywoodPrinciple? and LawOfDemeter?
Formerly MinimizePublicMethods - Minimize public methods.