Software Design Principles and Patterns
Classified in Computers
Written at on English with a size of 4.98 MB.
Lecture 2: Dynamic Dispatch and Interfaces
- Dynamic Dispatch: The process of selecting which implementation of a polymorphic operation to call at runtime.
- Interface: Calling a method that is not in the interface will cause a compilation error.
Lecture 3: N/A
Lecture 4: Method Contracts, Exceptions, and Unit Testing
- Method Contract: Should define pre/post conditions and exceptional behavior. The client is to blame if the precondition is not met, and the service is to blame if the postcondition is not met. Exceptional behavior specifies what the code will do if a precondition is violated.
- Exception: Runtime exception (unchecked) and IO exception (checked). The IO exception must be caught; otherwise, the code won't compile.
- Unit Test: Test boundary and test exhaustively. Specification test (black box) and structural test (white box). Sometimes we reach 100% statement coverage but still can miss a defect in the code. We do boundary interior testing for "for loops."
- Decision tables examples and previous midterm question
- Previous midterm question answers
- A. 2 (x !=0 y !=0), (x != 0 y==0)
- B. 1 more test with x == 0
- C. No, when step = 0, the error cannot be detected
Lecture 5: System Sequence Diagrams and Domain Models
- System Sequence Diagram
- Behavioral Contract
- Domain Model: If we do not think of some conceptual class X as text or a number in the real world, it's probably a concept, not an attribute.
Lecture 6: Object Model Diagrams
- Object model diagram
Lecture 11:
Lecture 13:
Lecture 14:
Lecture 16:
Lecture 12:
Lecture 7: Design Goals, Principles, Patterns, and Heuristics
Design Goals
- Robustness
- Flexibility
- Extensibility
- Reusability
- Efficiency
- Scalability
- Security
- Maintainability
- Testability
- Modularity
- Interoperability
- Usability
- Portability
Design Principles
- Low representational gap
- Low coupling
- High cohesion
Design Patterns
- Strategy pattern
- Template method pattern
- Composite pattern
- Decorator pattern
- Observer pattern
- Builder pattern
- Fluent API pattern
- Iterator pattern
- Module pattern
- Model-View-Controller (MVC) pattern
- Facade pattern
- Adapter pattern
- Proxy pattern
- Producer-Consumer pattern
Anti-Patterns
- Bloaters (Long Methods, Large Classes)
- Misuse of Inheritance
- Conditional Complexity (Using 'instanceof' or 'switch' instead of Polymorphism)
- Parallel Inheritance Hierarchies
- Shotgun Surgery (Any Change Requires Lots of Edits)
- Speculative Generality (Excessive, Unused Hierarchies)
- Misplaced Responsibilities (Operations Posing as Classes)
- Data Classes (Lack of Encapsulation)
- Feature Envy (Heavy Usage of One Class' Data from Another)
- Law of Demeter Violation (Long Chains of Calls)
- Middle Man (A Class That Only Delegates Work)
Design Heuristics
- Controller (decoupled UI and domain logic)
- Information expert
- Creator (promotes low coupling and high cohesion, B aggregates A objects, B contains A objects, B records instances of A objects, B closely uses A objects, or B has the initializing data for creating A objects (the more the better))
- Law of Demeter (principle of least knowledge)
Lecture 8: Dynamic Dispatch, Final Keyword, and Subtyping
- Dynamic Dispatch: At compile time, determine which class to look in and the method signature to be executed. At runtime, determine the dynamic class (the actual class, not the interface) of the receiver and which method to invoke.
- Final Keyword: A final class prevents extending the class, a final method prevents overriding the method, and a final field prevents reassignment after initialization.
-
Behavioral Subtyping: Same or stronger invariants than the superclass, same or weaker preconditions for all methods than the superclass, same or stronger postconditions for all methods in the superclass.
- a) Square is not a behavioral subtype of a rectangle.