BaseBean

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In object-oriented programming, inheritance should be used for "IS-A" relationships, not "HAS-A" relationships. A BaseBean is a utility class from which concrete entities have been derived via subclassing, creating a false implication that the derived classes represent subtypes of the utility class in the business domain. The BaseBean is an example of an anti-pattern (where the "Bean" part of the name comes from the standard Java naming convention for a generic entity object, or JavaBean). For example, if a utility class exists called DatabaseUtils, which contains utility methods for setting up and tearing down database connections, and other classes such as Employee extend from it, DatabaseUtils is a BaseBean, because in the real world, an employee is not a "database utils".

Proper design suggests that the inherited functionality should be provided via delegation instead.

A class should not inherit from another class simply because the parent class contains functionality needed in the subclass, as the inheriting class may behave improperly when used as a replacement of the parent class, thus violating the Liskov substitution principle. Instead, delegation (has-a relationship) could be used to obtain the business logic or data structure that is required. In other words, this case warrants composition over inheritance. In some cases, in Java, a utility class containing only static methods can be created to contain the necessary functionality. Object-oriented programming emphasizes that objects should be meaningful and should communicate with each other in the way that resembles the real-world entities they are emulating. A "BaseBean" is not a real-world object, nor is it descriptive. It may be that it needs to be refactored as a more meaningful class, and referenced rather than extended.

See also[edit]

  • The CallSuper anti-pattern - in which a derived class has the requirement to call into the parent class to complete its work