Objects are containers

by moodyharsh 2016-08-29

Objects are containers

In case of Abstract Data Types, objects hold Data and Methods.
Objects are the most natural fit for ADTs.

In case of Design Patterns, objects hold code. This is where majority
of the complains come from.

However if you limit the Design Patterns to just,

  • Command Pattern or Function Object or Closure
  • Decorator Pattern
  • Interceptor and Filter Pattern
  • Observer Pattern
  • Publish Subscribe
  • Service Locator Pattern
  • Module Pattern
  • Visitor Pattern

you will find that OOP is fairly manageable.

If the object which is not an ADT and has many public methods, you
are probably overdoing it. Coupled with inheritance this is a
disaster. Why is that ? Because if the object has more public
methods, you will have to go through huge troubles to implement
Object Substitution. By troubles, I mean

  • Adapter Pattern
  • Proxy Pattern
  • Bridge
  • Mediator
  • Composition
  • Dependency Injection
  • Delegation
  • Facade
  • Mocking

So don’t do it ! Use lots of Commands in your codebase you just
have to replace one method, execute, for Object Substitution. This
simplifies many things and that is the way to go.