SearchWiki
Heuristics.RecentChanges
Edit Page
Page Revisions
Home
Design Patterns
Heuristics
Software Development
Software Process


Patterns
TheIntroduction LogicalDesign PhysicalDesign PatternFoundations GuidelinesAndMyths ReferenceImplementation DependencyPatterns UsabilityPatterns ExtensibilityPatterns MaintenancePatterns AntiPatterns UtilitiesAndTools

AllPatterns

Name

MinimizeVisibility

Statement

Minimize visibility of methods, attributes, and classes.

Sketch

Description

Class 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 Variations

Those methods that are public should be ClearMethods. This will help with ease of use and understandability.

Example: calculateDiscount vs. getDiscount

Consequences

Sample Code

Illustrate 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 changeName

class SalaryEmp extends Employee
 - calculatePay
 - changeName
 - getSalary

class CommissionEmp extends Employee
 - calculatePay
 - changeName
 - getCommission

interface PayCalculator
 - calculatePay(Employee)

class SalaryPayCalculator
  - calculatePay(Employee employee) //SalaryEmp emp = (SalaryEmp) employee;

Related Pattern(s)

HollywoodPrinciple? and LawOfDemeter?
Formerly MinimizePublicMethods - Minimize public methods.
Edit Page - Page Revisions - WikiHelp - SearchWiki - RecentChanges
Page last modified on October 25, 2004, at 09:35 PM