Software Engineering Patterns
intermediatev1.0.0tokenshrink-v2
# Software Engineering Patterns Knowledge Pack ## SOLID Principles ### SRP — Single Responsibility Principle A cls should have only one reason to change. This means each cls encapsulates one cohesive responsibility. Violation indicators: cls names containing "And" or "Manager" that do many things, cls with methods operating on unrelated data, changes to one feature requiring edits to unrelated methods in the same cls. Example: A `UserService` that handles authentication, profile updates, AND email sending violates SRP. Refactor into `AuthService`, `ProfileService`, and `EmailService`. Each can change independently. ### OCP — Open-Closed Principle Software entities should be open for extension but closed for modification. When new behavior is needed, extend through new code rather than modifying existing working code. Impl strategy: Use abs cls or iface to define contracts. New behavior is added as new conc cls that impl the iface. Strategy pattern is the canonical OCP impl. Example: Instead of a switch statement over payment types inside a `processPayment` fn, define a `PaymentProcessor` iface with a `process` method, then create `CreditCardProcessor`, `PayPalProcessor`, etc. Adding a new payment type means adding a new cls, not editing existing code. ### LSP — Liskov Substitution Principle Subtypes must be substitutable for their base types without altering correctness. If cls B extends cls A, anywhere A is used, B should work without surprises. Common violations: Square extending Rectangle (changing width changes height — breaks Rectangle behavior), throwing NotImplementedException in overridden methods, strengthening preconditions or weakening postconditions in subtype. Test: Can you use the derived cls everywhere the base cls is expected without checking the type? If you need `instanceof` checks, you're violating LSP. ### ISP — Interface Segregation Principle Clients should not be forced to depend on methods they don't use. Prefer many small, focused ifaces over one large "fat" iface.
Showing 20% preview. Upgrade to Pro for full access.