Object Oriented Design Principles: SOLID, …
Table of Contents
1 Data Encapsulation v Abstraction v OOD
1.1 Encapsulation
- A "data structure" is declared.
- Certain operations are declared to be "public".
- The "data structure" is accessed/manipulated only via the above.
1.2 Abstraction
- A "data type" is declared
- Certain operations are available
- The "data type" is an abstraction
- Concrete representation to Abstraction mapping
- E.g., an array of items ==> set of items
- Information Hiding Principle
- Abstract Data Types v Classes
- Data Encapsulation v Data Abstraction
- See ../Abstraction
1.3 Class Invariants
- Class invariants are assertions.
- Must be true before and after every public methods.
- Must be true after every constructor.
- See ../Assertions
1.4 C++/Java/… Classes
- Provide data encapsulation
- Do not do Data Abstraction
- Language Design
- public v protected v private
- interface v class
2 SOLID Principles of Class Design
- (SRP) The Single Responsibility Principle
- (OCP) The Open Closed Principle: Classes and methods should be open for extension (of functionality) and closed for modification.
- (LSP) The Liskov Substitution Principle. Loosely stated: Subtypes must be substitutable for super type. A sub class must enhance functionality, but not reduce.
- (ISP) The Interface Segregation Principle
- (DIP) The Dependency Inversion Principle
- Note that some argue that LSP is not an OOD principle.
- In this course: We replace the above with Design by Contract This is often confused with LSP.
- All the above in greater detail at ./SOLID-principles.html
3 Principles of Package Cohesion
- (REP) The Reuse Release Equivalence Principle
- (CCP) The Common Closure Principle
- (CRP) The Common Reuse Principle
4 Principles of Package Coupling
- (ADP) The Acyclic Dependencies Principle
- (SDP) The Stable Dependencies Principle
- (SAP) The Stable Abstractions Principle
5 Design By Contract (DoC)
6 Liskov Substitution Principle (LSP)
7 AntiPatterns
- "An anti-pattern is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive."
- ./antipatterns.html
7.1 Examples from non-CS
- Bicycle shed: Giving disproportionate weight to trivial issues
- Escalation of commitment: Failing to revoke a decision when it proves wrong
- Peter Principle: Continually promoting otherwise well-performing employees up to their level of incompetence, where they remain indefinitely
- Stovepipe or Silos: An organizational structure of isolated or semi-isolated teams, in which too many communications take place up and down the hierarchy, rather than directly with other teams across the organization
- Ninety-ninety rule: Tendency to underestimate the amount of time to complete a project when it is "nearly done"
7.2 Examples from Software engineering
- Abstraction inversion: Not exposing implemented functionality required by callers of a function/method/constructor, so that the calling code awkwardly re-implements the same functionality in terms of those calls
- Big ball of mud: A system with no recognizable structure.
- Input kludge: Failing to specify and implement the handling of possibly invalid input
- Call super: Requiring subclasses to call a superclass's overridden method
- God object: Concentrating too many functions in a single part of the design (class)
- Sequential coupling: A class that requires its methods to be called in a particular order. (?)
- Poltergeists: Objects whose sole purpose is to pass information to another object
- Boat anchor: Retaining a part of a system that no longer has any use
- Error hiding: Catching an error message before it can be shown to the user and either showing nothing or showing a meaningless message. Also can refer to erasing the Stack trace during exception handling, which can hamper debugging.
- Lasagna code: Programs whose structure consists of too many layers.
8 References
- Web site, http://www.oodesign.com Includes pretty good descriptions of the principles mentioned on this page. Recommended Visit
- https://en.wikipedia.org/wiki/Anti-pattern A few selected items are given above. Software Engineering section: Required Reading.
- https://sourcemaking.com/ has most of the above materials presented well.
- http://antipatterns.com/ Recommended Visit.
- Robert Bruce Findler, Mario Latendresse, and Matthias Felleisen. Behavioral Contracts and Behavioral Subtyping. In Proceedings of ESEC/FSE-9, 229-236. http://www.ccs.neu.edu/scheme/pubs/fse01-flf.pdf 2001 Recommended Reading.
- Bertrand Meyer. Applying "Design by Contract". IEEE Computer, 25(10):40–51, October 1992. http://se.ethz.ch/~meyer/publications/computer/contract.pdf Also in his book "Object-Oriented Software Construction". Bertrand Meyer's article marked-up by pmateti Required Reading.