๐Ÿ’Ž Lecture 09 ยท Design Principles by Uncle Bob

SOLID Principles

5 principles by Robert C. Martin (Uncle Bob) โ€” ุฃุณุงุณ ุงู„ู€ OOD ุงู„ุตุญูŠุญ. OCP ูˆ DIP ุฃู‡ู…ู‡ู… ููŠ ุงู„ุงู…ุชุญุงู†. ู…ุฑุชุจุทูŠู† ู…ุจุงุดุฑุฉ ุจู€ Strategy Pattern.

5
SOLID Letters
7
McConnell Traits
7
Chapters
8
Practice Qs
01

SOLID Overview

5 guidelines ู„ุชุฌู†ุจ bad design. ุญุฑูˆู SOLID = ุงุฎุชุตุงุฑ ู„ู„ู€ 5 principles.

S
Single Responsibility
O
Open-Close
L
Liskov Substitution
I
Interface Segregation
D
Dependency Inversion
๐Ÿ‘จโ€๐Ÿ’ผ Robert C. Martin โ€” "Uncle Bob"
  • Author of Clean Code + Agile Software Craftsmanship.
  • SOLID = set of guidelines to avoid bad OOP design.
  • ุงู„ู€ goal: code maintainable, extensible, testable.
02

S โ€” Single Responsibility Principle (SRP)

"A class should have only one reason to change."

๐Ÿ“– ุงู„ููƒุฑุฉ
  • ูƒู„ class ุจูŠู…ุซู„ concept ูˆุงุญุฏ ูˆูŠุญู…ู„ responsibility ูˆุงุญุฏุฉ.
  • ู„ูˆ ููŠู‡ 2 reasons ู„ุชุบูŠูŠุฑ ุงู„ู€ classุŒ ูŠุจู‚ู‰ ู„ุงุฒู… ุชู‚ุณู…ู‡ ู„ู€ class ุงุชู†ูŠู†.
  • ูƒู„ class ูŠู‡ุชู… ุจู€ one responsibility + one reason to change.
๐Ÿ’ป Example

โŒ Bad โ€” 2 responsibilities

class User {
  String name;
  String email;

  void save() {
    // DB logic
  }
  void sendEmail() {
    // SMTP logic
  }
}

โš ๏ธ 2 reasons to change: DB schema changes OR email server changes.

โœ… Good โ€” SRP

class User {
  String name; email;
}
class UserRepository {
  void save(User u) {...}
}
class EmailService {
  void send(User u) {...}
}

โœ“ Each class has ONE reason to change.

03

O โ€” Open-Close Principle (OCP) โญ

"Software entities should be open for extension, but closed for modification." โ€” ุงู„ุฃู‡ู… ู„ุฃู†ู‡ ุฃุณุงุณ Strategy Pattern.

๐Ÿ”Œ ุงู„ููƒุฑุฉ

ุงู‚ุฏุฑ ุชุถูŠู functionality ุฌุฏูŠุฏ ุจุฏูˆู† ู…ุง ุชุบูŠู‘ุฑ existing code.

ุฒูŠ mixer kitchen โ€” ุชู‚ุฏุฑ ุชุบูŠุฑ ุงู„ู€ attachments ุจุฏูˆู† ู…ุง ุชุบูŠุฑ ุงู„ู€ engine.

ูƒูŠู ู†ุทุจู‚ู‡ุŸ

  • Use Interfaces / Abstract Classes.
  • Use Design Patterns (Strategy, Decorator, etc.).
๐Ÿ“š Motivating Problem โ€” Sorting

ุนุงูŠุฒ ArrayList ุจุณู€ sort() method. ุงุจุฏุฃ ุจู€ bubble sort:

class ArrayList<T> {
  void sort() { /* bubble sort */ }
}

ุนุงูŠุฒ ุชุจุฏู‘ู„ ู„ู€ merge sortุŸ ุงุญุฐู bubble ูˆุถู merge. ุณู‡ู„.

ุนุงูŠุฒ ุงู„ุงุชู†ูŠู† ู…ุชุงุญูŠู†ุŸ ุถู if statement:

void sort(int sortChoice) {
  if (sortChoice == 1) { /* merge sort */ }
  if (sortChoice == 2) { /* bubble sort */ }
}
๐Ÿ”ด ุงู„ู…ุดูƒู„ุฉูƒู„ ู…ุง ุชุถูŠู algorithm ุฌุฏูŠุฏุŒ ู„ุงุฒู… ุชุนุฏู„ ุงู„ูƒูˆุฏ. ู‡ุฐุง violation ู„ู„ู€ OCP. ู…ุน ูƒู„ ุชุนุฏูŠู„ุŒ ุงู„ูƒู„ุงูŠู†ุชุณ ุงู„ู„ูŠ ุจูŠุณุชุฎุฏู…ูˆุง ุงู„ู€ class ู…ู…ูƒู† ูŠุชุฃุซุฑูˆุง.
๐Ÿ’ก Uncle Bob's Solution
"When writing a class, ensure that when you need to extend its behavior, you don't have to change the class but to extend it."

ุงู„ุญู„: ุงุณุชุฎุฏู… Abstract Classes + concrete classes ู„ุชู†ููŠุฐ behavior. ุฏู‡ ุงู„ู€ Strategy Pattern โ€” ู‡ุชุดูˆูู‡ ุชูุตูŠู„ุงู‹ ููŠ Lecture 12.

04

L โ€” Liskov Substitution Principle (LSP)

"Subtypes must be substitutable for their base types."

๐Ÿ“– ุงู„ููƒุฑุฉ (Barbara Liskov)

ู„ูˆ S ู‡ูˆ subtype ู…ู† TุŒ ูŠุจู‚ู‰ objects ู…ู† type T ู…ู…ูƒู† ุชุชุจุฏู„ ุจู€ objects ู…ู† type S ุจุฏูˆู† ู…ุง ุชุฃุซุฑ ุนู„ู‰ correctness.

ุงู„ู„ูŠ ุงู„ู€ subclass ู…ุง ูŠู„ุบูŠุด ุฃูˆ ูŠูƒุณุฑุด contract ุงู„ู€ superclass.

๐ŸŸฆ Classic Example โ€” Square vs Rectangle

โŒ Violates LSP

class Rectangle {
  int w, h;
  void setWidth(int w) { ... }
  void setHeight(int h) { ... }
}

// Square is-a Rectangle?
class Square extends Rectangle {
  // w must = h always
  void setWidth(int w) {
    this.w = w;
    this.h = w;  // surprise!
  }
}

โœ… Test if substitutable

void resize(Rectangle r) {
  r.setWidth(5);
  r.setHeight(4);
  assert(r.area() == 20);
}

// Passing a Square FAILS:
// setHeight changes width too
// โ†’ area = 16, not 20
// Square is NOT substitutable for Rectangle
๐Ÿคฏ ุงู„ู€ paradoxููŠ ุงู„ู€ mathุŒ square IS-A rectangle. ุจุณ ููŠ OOPุŒ subclass = behavior. ู„ูˆ behavior ุจูŠูƒุณุฑ expectationsุŒ ูŠุจู‚ู‰ violation ู„ู„ู€ LSP. ุงู„ุญู„: ู…ุง ุชุฎู„ูŠุด Square ุชุฑุซ ู…ู† Rectangle โ€” ุงุนู…ู„ู‡ู… separate types ุฃูˆ ุญุฏุฏ abstraction ุฃุนู„ู‰.
๐Ÿง Example 2 โ€” Bird vs Penguin

ู„ูˆ ุนู†ุฏูƒ Bird class ููŠู‡ุง fly()ุŒ ูˆ Penguin extends Bird โ€” ุจุณ ุงู„ุจุทุฑูŠู‚ ู…ุง ุจูŠุทูŠุฑุด! โ†’ ุจูŠูƒุณุฑ ุงู„ู€ substitutability.

ุงู„ุญู„: ุงูุตู„ ุงู„ุทูŠูˆุฑ ุงู„ุทุงูŠุฑุฉ ุนู† ุบูŠุฑ ุงู„ุทุงูŠุฑุฉ. ุงุณุชุฎุฏู… interfaces / abstractions ู…ู†ุงุณุจุฉ (ู…ุซู„ุงู‹ Flyable ู…ู†ูุตู„).

๐Ÿง‘โ€๐Ÿ’ผ Example 3 โ€” Employee vs VolunteerEmployee โญ

ุนู†ุฏูƒ Employee hierarchyุŒ ูˆ VolunteerEmployee extends Employee. ุจุณ ุงู„ู€ VolunteerEmployee ู…ู„ูˆุด salary/payment โ†’ ุงู„ู€ calcPay() ุจุชุจู‚ู‰ ู…ุดูƒู„ุฉ.

๐Ÿ”ด ู„ูŠู‡ ุฏู‡ ุฃุณูˆุฃุŸุงู„ูƒูˆุฏ ุงู„ู„ูŠ ุงู„ู…ูุฑูˆุถ ูŠุดุชุบู„ ุนู„ู‰ ุงู„ู€ Employee base class ู‡ูŠุถุทุฑ ูŠุนู…ู„ explicit reference ู„ูˆุงุญุฏ ู…ู† ุงู„ู€ derivatives (ูŠู€ูุญุต: ู‡ู„ ุฏู‡ VolunteerEmployeeุŸ). ุฏู‡ violation ุตุฑูŠุญ ู„ู„ู€ LSP:
  • VolunteerEmployee ู…ุด substitutable ู„ู€ Employee.
  • ุงู„ู€ users ุจุชูˆุน Employee ุจูŠุชุฃุซุฑูˆุง ุจู…ุฌุฑุฏ ูˆุฌูˆุฏ ุงู„ู€ VolunteerEmployee.
โœ… ุงู„ุญู„ (ู…ู† ุงู„ุณู„ุงูŠุฏุงุช)
  • ุงู„ู€ Volunteers ู…ุด employees ุฃุตู„ุงู‹.
  • VolunteerEmployee ู…ุง ูŠูˆุฑู‘ุซุด ู…ู† Employee.
  • ู…ุง ูŠุชุจุนุชุด ู„ู€ functions ุจุชุญุชุงุฌ calcPay().
05

I โ€” Interface Segregation Principle (ISP)

"Clients should not be forced to depend upon interfaces they do not use."

๐Ÿ“– ุงู„ููƒุฑุฉ

ู‚ุณู‘ู… ุงู„ู€ "fat" interfaces ู„ู€ smaller, more specific ones.

ุงู„ู€ "Fat" interfaces ุจุชุคุฏูŠ ู„ู€ "polluted" classes โ€” ู…ุฌุจุฑูŠู† implement methods ู…ุง ูŠุญุชุงุฌูˆู‡ุงุด (ุบุงู„ุจุงู‹ ุจูŠุฑู…ูˆุง NotImplementedException).

๐Ÿ’ป Example

โŒ Fat interface

interface Worker {
  void work();
  void eat();
  void sleep();
}

class Robot implements Worker {
  void work() {...}
  void eat() {
    throw new
      NotImplementedException();
  }
  // Robots don't eat/sleep!
}

โœ… Segregated interfaces

interface Workable {
  void work();
}
interface Feedable {
  void eat();
}

class Robot implements
                Workable {
  void work() {...}
}
class Human implements
        Workable, Feedable {
  void work() {...}
  void eat() {...}
}
๐Ÿšช Example 2 โ€” Door & SecurityDoor

ุนู†ุฏูƒ IDoor { lock(); unlock(); isOpen(); }. ุฌู‡ ุนุงูŠุฒ SecurityDoor ุชุทู„ู‘ู‚ alarm ู„ูˆ ูุถู„ุช ู…ูุชูˆุญุฉ โ†’ ุถูุช timeOut() ู„ู„ู€ IDoor.

๐Ÿ”ด ุงู„ู…ุดูƒู„ุฉู…ุด ูƒู„ ุงู„ุฃุจูˆุงุจ timed! ุฅุถุงูุฉ timeOut() ู„ู„ู€ IDoor ุจุชู„ูˆู‘ุซู‡ (polluted interface) ูˆุจุชุฌุจุฑ ูƒู„ door ุชุนู…ู„ dummy implementation.

โœ… Segregated

interface IDoor {
  void lock();
  void unlock();
  bool isOpen();
}
interface ITimerClient {
  void timeOut();
}

class SecurityDoor implements
              IDoor, ITimerClient {
  void timeOut() {...}
}

๐Ÿ“‹ Vehicle Example

// โŒ Vehicle{ drive(); fly(); }
// Car.fly() โ†’ OOPS
// Drone.drive() โ†’ OOPS

// โœ… Split them:
interface Drivable { void drive(); }
interface Flyable   { void fly(); }

class Car      implements Drivable
class Airplane implements Drivable, Flyable
class Drone    implements Flyable
๐Ÿ“Œ ู†ู‚ุงุท ู…ู‡ู…ุฉ ู…ู† ุงู„ุณู„ุงูŠุฏุงุช
  • Many client-specific interfaces > one general-purpose interface.
  • Fat interface = Fat class โ€” ุญุชู‰ ุงู„ุชุบูŠูŠุฑ ููŠ method ู…ุด ู…ุณุชุฎุฏู…ุฉ ู…ู…ูƒู† ูŠุฃุซุฑ ุนู„ู‰ ุงู„ู€ users.
  • ISP ู…ุด ู…ุนู†ุงู‡ interface ูˆุงุญุฏ ู„ูƒู„ class โ€” ุงู„ู…ุนู†ู‰: group clients by type (ู„ูˆ ุบูŠู‘ุฑุช ู„ู€ ClientA ู…ุง ูŠุฃุซุฑุด ุนู„ู‰ ClientB/ClientC).
  • ู…ุซุงู„ ูˆุงู‚ุนูŠ: ASP.NET Membership Provider ุฃุฌุจุฑ ุงู„ู€ custom provider ุนู„ู‰ implement 27 method/property โ€” fat interface ูƒู„ุงุณูŠูƒูŠ.
06

D โ€” Dependency Inversion Principle (DIP) โญ

"High-level modules should not depend on low-level modules. Both should depend on abstractions."

๐Ÿ“– ุงู„ููƒุฑุฉ
  • Abstractions should not depend on details.
  • Details should depend on abstractions.

ููŠ ุงู„ู€ traditional design: high-level modules ุจุชุนุชู…ุฏ ุนู„ู‰ low-level. ุฃูŠ ุชุบูŠูŠุฑ ููŠ low-level ุจูŠุฎุฑุจ high-level.

ููŠ ุงู„ู€ DIP: ุงู„ุงุชู†ูŠู† ุจูŠุนุชู…ุฏูˆุง ุนู„ู‰ interface (abstraction).

๐Ÿ’ป Example

โŒ Direct dependency

class MySQLDatabase {
  void save(String data) {...}
}

class UserService {
  private MySQLDatabase db;

  UserService() {
    // hard-coded dependency
    db = new MySQLDatabase();
  }
}

โš ๏ธ UserService can't switch to Postgres without code change.

โœ… Depend on abstraction

interface Database {
  void save(String data);
}
class MySQLDatabase
            implements Database {...}
class PostgresDatabase
            implements Database {...}

class UserService {
  private Database db;

  UserService(Database db) {
    this.db = db;  // injected
  }
}

โœ“ Easy swap. Easy testing (inject mock).

๐Ÿ’ก Strongly Related to Strategy Patternุงู„ู€ DIP ุจูŠุนู…ู„ ุงู„ู€ foundation ู„ู„ู€ Strategy. ู„ู…ุง UserService ุจูŠุนุชู…ุฏ ุนู„ู‰ Database interface ุจุฏู„ concreteุŒ ุชู‚ุฏุฑ ุชุณุชุจุฏู„ implementations ููŠ runtime โ€” exactly ุงู„ู€ Strategy Pattern.
๐Ÿงท Practical Notes (ู…ู† ุงู„ุณู„ุงูŠุฏุงุช)
  • ู„ูŠู‡ DIPุŸ ุงู„ู€ concrete classes ุจุชุชุบูŠุฑ ูƒุชูŠุฑ (volatile)ุŒ ุงู„ู€ abstractions ุจุชุชุบูŠุฑ ุฃู‚ู„. ุงู„ู€ abstractions = "hinge points" (ู†ู‚ุงุท ุงุฑุชูƒุงุฒ ุซุงุจุชุฉ). ูˆุจูŠุฏุนู… ุงู„ู€ Open/Close Principle.
  • ุขู…ู† ุฅู†ูƒ ุชุนุชู…ุฏ ุนู„ู‰ concrete classes ู…ุณุชู‚ุฑุฉ (stable) ุฒูŠ String ูˆ Vector โ€” ู…ุด ูƒู„ concrete class ู…ู…ู†ูˆุนุฉ. ุชุฌู†ู‘ุจ ุงู„ู€ volatile concrete classes ุจุณ.
  • UML check: ุงู„ู€ dependency arrows ุงู„ู…ูุฑูˆุถ ุชุดุงูˆุฑ ุนู„ู‰ interfaces / abstract classes โ€” ู…ุด ุนู„ู‰ concrete classes.
07

Steve McConnell โ€” Views on Design

ู…ู† ูƒุชุงุจ "Code Complete" โ€” Chapter 5. ุฌูˆู‡ุฑ ุงู„ู€ design = managing complexity.

๐Ÿ“ Where Does Design Fit?

ุงู„ู€ design ุจูŠู‚ุน ุจูŠู† ุงู„ู€ requirements ูˆุงู„ู€ code โ€” ู…ุฑุญู„ุชูŠู†:

Requirements โ†’ Architecture / High-Level Design โ†’ Low-Level Design โ†’ Code
๐Ÿชœ Levels of Design
  • High-level design โ€” ุงู„ู€ system components ูˆุงู„ู€ connections ุจูŠู†ู‡ู….
  • Low-level design โ€” ุงู„ู€ objects ยท classes ยท algorithms ยท variables.
  • ุงู„ุญุฏ ุจูŠู† ุงู„ุงุชู†ูŠู† blurry (ู…ุด ูˆุงุถุญ ุชู…ุงู…ุงู‹).
๐Ÿ’ฌ Quotes ุนู† ุงู„ู€ Design "The driving force behind design is managing complexity." ยท "The goal of software design techniques is breaking complicated problems into simple pieces." ยท "The key to performance is elegance, not battalions of special cases." โ€” Bentley & McIlroy

7 Characteristics of Good Design

1

Minimal Complexity

Divide & conquer ยท use abstractions ยท reduce coupling ยท increase cohesion ยท remove dead code ยท avoid clever tricks ยท comment ยท document.

2

Loose Coupling

ู‚ู„ู‘ู„ ุงู„ู€ connections ุจูŠู† ุฃุฌุฒุงุก ุงู„ุจุฑู†ุงู…ุฌ. ุจูŠุจุณู‘ุท ุงู„ู€ integration / testing / maintenance.

3

Strong Cohesion

ุงู„ู€ routines ุจุชุฏุนู… ุงู„ู€ purpose ุงู„ู…ุฑูƒุฒูŠ ู„ู„ู€ module. ุงู„ู‡ุฏู: maximize cohesion.

4

Extensibility

ุชุทูˆู‘ุฑ ุงู„ู†ุธุงู… ู…ู† ุบูŠุฑ ู…ุง ุชุฎุฑู‘ุจ structure-ู‡. ุงู„ุชุบูŠูŠุฑุงุช ุงู„ู…ุชูˆู‚ุนุฉ ุชุณุจุจ ุฃู‚ู„ trauma.

5

Reusability

ุฅุนุงุฏุฉ ุงุณุชุฎุฏุงู… ุฃุฌุฒุงุก ููŠ systems ุชุงู†ูŠุฉ.

6

Maintainability

ุฃุณู‡ู„ ููŠ ุฅุตู„ุงุญ ุงู„ู€ bugs ยท ุงู„ุชุญุณูŠู† ยท ุฅุถุงูุฉ features.

7

Leanness

ู…ููŠุด ุฃุฌุฒุงุก ุฒูŠุงุฏุฉ. Voltaire: "ุงู„ูƒุชุงุจ ูŠุฎู„ุต ู„ู…ุง ู…ููŠุด ุญุงุฌุฉ ุชุงู†ูŠุฉ ู…ู…ูƒู† ุชุชุดุงู„". ุงู„ูƒูˆุฏ ุงู„ุฒูŠุงุฏุฉ ุจูŠุฒูˆู‘ุฏ effort ุงู„ู€ dev/review/testing/maintenance.

๐ŸŽฏ

Practice Quiz โ€” Lecture 9

8 ุฃุณุฆู„ุฉ ุนู„ู‰ SOLID + McConnell. Q3 ููŠ ุงู…ุชุญุงู†ุงุช Winter 2023 ุจู€ 24 marks ุนู„ู‰ DIP + Adapter + Observer.

Question 1 Practice โ€” SRP
What does "Single Responsibility Principle" mean?
A A class should have only one method
B A class should have only one attribute
C A class should have only one reason to change
D A class should have only one instance
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: C โ€” One reason to change SRP = single concept + single responsibility = one reason to change.
A, B โ€” Classes can have multiple methods/attributes โ€” that's normal. ยท D โ€” That's Singleton pattern.
Question 2 Practice โ€” OCP
"Software entities should be open for extension, but closed for modification" means:
A Classes should never be modified
B Add new functionality by extending, not by changing existing code
C All classes must be marked final
D Don't use inheritance
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: B OCP = extend behavior without modifying source. Achieved by interfaces, abstract classes, or design patterns (Strategy, Decorator).
A โ€” Bug fixes are allowed; "closed for modification" means closed against extensions changing logic. ยท C โ€” final blocks all extension, opposite of OCP. ยท D โ€” Inheritance is actually part of how OCP is achieved.
Question 3 Practice โ€” Strategy/OCP
A sort() method uses an if/else chain to switch between bubble, merge, and quick sort. To add a new sort algorithm requires editing this chain. Which principle is violated?
A Open-Close Principle
B Single Responsibility
C Liskov Substitution
D Interface Segregation
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: A โ€” OCP ูƒู„ ู…ุง ุชุถูŠู algorithm ุฌุฏูŠุฏุŒ ุชุนุฏู„ ุงู„ูƒูˆุฏ ุงู„ู…ูˆุฌูˆุฏ. Open for extension = NO. Closed for modification = NO. ุงู„ุญู„: Strategy Pattern.
B (SRP) โ€” The class still has one responsibility (sorting). ยท C (LSP) โ€” No subclass involved. ยท D (ISP) โ€” No interface issue.
Question 4 Practice โ€” LSP
A Square class extends a Rectangle class. Setting width on Square also changes height, breaking caller assumptions. Which principle is violated?
A Open-Close
B Single Responsibility
C Liskov Substitution
D Dependency Inversion
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: C โ€” LSP Square's behavior breaks the contract of Rectangle. Code expecting Rectangle won't work correctly with Square.
Question 5 Practice โ€” ISP
A Worker interface has methods work(), eat(), sleep(). A Robot class implementing it must throw NotImplementedException for eat and sleep. Which principle is violated?
A SRP
B OCP
C LSP
D ISP
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: D โ€” ISP The Worker interface is too "fat". Robot is forced to depend on methods it doesn't use. ุงู„ุญู„: split into Workable, Feedable, Sleepable.
Question 6 Practice โ€” DIP
Which of these correctly applies Dependency Inversion?
A class Service { MySQLDb db = new MySQLDb(); }
B class Service { Database db; Service(Database d) { this.db = d; }}
C class Service extends Database { }
D class Service { static MySQLDb db = new MySQLDb(); }
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: B ุงู„ู€ Service ุจูŠุนุชู…ุฏ ุนู„ู‰ Database interface (abstraction)ุŒ ูˆุจูŠุชู… inject ุงู„ู€ concrete implementation. ุฏู‡ DIP โ€” depend on abstractions, not concretes.
A, D โ€” direct dependency ุนู„ู‰ concrete MySQLDb. Violation. ยท C โ€” inheritanceุŒ ู…ุด dependency.
Question 7 Practice โ€” McConnell
According to McConnell (Code Complete), what is the driving force behind software design?
A Maximizing performance
B Managing complexity
C Writing the fewest lines of code
D Using as many design patterns as possible
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: B โ€” Managing complexity McConnell: "The driving force behind design is managing complexity." ุงู„ู‡ุฏู ุชูƒุณูŠุฑ ุงู„ู…ุดุงูƒู„ ุงู„ู…ุนู‚ู‘ุฏุฉ ู„ุฃุฌุฒุงุก ุจุณูŠุทุฉ. (ุงู„ู€ 7 characteristics ุจุชุฎุฏู… ุงู„ู‡ุฏู ุฏู‡: minimal complexity, loose coupling, strong cohesion, extensibility, reusability, maintainability, leanness).
A โ€” "The key to performance is elegance, not battalions of special cases" โ€” ุงู„ุฃุฏุงุก ู…ุด ุงู„ู€ driving force. ยท C โ€” leanness ู…ููŠุฏุฉุŒ ุจุณ ู…ุด "ุฃู‚ู„ ุณุทูˆุฑ" ุจุฃูŠ ุซู…ู†. ยท D โ€” ุงู„ู€ patterns ูˆุณูŠู„ุฉุŒ ู…ุด ู‡ุฏู.
Question 8 Practice โ€” comprehensive
Which SOLID principle is the foundation for the Strategy Pattern?
A Single Responsibility
B Liskov Substitution
C Interface Segregation
D Open-Close Principle
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: D โ€” OCP Strategy = encapsulate algorithms in classes, swap them at runtime. ุงู„ู€ system ุจูŠูƒูˆู† open for adding new algorithms (just create a new strategy class) and closed for modification (the context class never changes). DIP ุจูŠุฏุนู… ุงู„ู€ OCP ู„ูƒู† ุงู„ุฃุณุงุณ ู‡ูˆ OCP.
๐Ÿ“‹

Cheat Sheet

SOLID + McConnell ููŠ ุตูุญุฉ ูˆุงุญุฏุฉ.

๐Ÿ”ต SOLID

S โ€” SRP
One reason to change per class
O โ€” OCP โญ
Open for extension, closed for modification
L โ€” LSP
Subtype substitutable for base
I โ€” ISP
No fat interfaces; split & specialize
D โ€” DIP โญ
Depend on abstractions, not concretes

๐Ÿ“ McConnell โ€” 7 Good Design Traits

1. Minimal Complexity
Simple > clever (driving force)
2. Loose Coupling
Few connections
3. Strong Cohesion
Routines serve one purpose
4. Extensibility
Change w/o damaging structure
5. Reusability
Reuse parts elsewhere
6. Maintainability
Easy fix/enhance/add
7. Leanness
No extra parts (Voltaire)

๐Ÿ”‘ Quick Identifiers

if/else chain โ†’ ?
OCP violation. Use Strategy.
class with 2 jobs โ†’ ?
SRP violation
throw NotImplemented โ†’ ?
ISP violation
new ConcreteClass() in constructor โ†’ ?
DIP violation
subclass breaks parent contract โ†’ ?
LSP violation

๐Ÿ‘จโ€๐Ÿ’ผ Authors

Uncle Bob
Robert C. Martin ยท Clean Code ยท SOLID
McConnell
Steve ยท Code Complete ยท 7 design traits
Liskov
Barbara Liskov ยท 1987 ยท LSP
โšก

Rapid Revision

Flashcards ยท Mistakes ยท Doctor favorites.

SOLID expandedุŸ
tap
SRP ยท OCP ยท LSP ยท ISP ยท DIP
SOLID authorุŸ
tap
Robert C. Martin (Uncle Bob)
SRP in 1 lineุŸ
tap
One reason to change
OCP in 1 lineุŸ
tap
Open for extension, closed for modification
LSP in 1 lineุŸ
tap
Subtype substitutable for base
ISP in 1 lineุŸ
tap
No fat interfaces โ€” split & specialize
DIP in 1 lineุŸ
tap
Depend on abstractions, not concretes
if/else chain for behavior = which violationุŸ
tap
OCP โ€” fix with Strategy
McConnell: driving force of designุŸ
tap
Managing complexity

๐Ÿšจ Common Mistakes

1. ุฎู„ุท SRP ูˆ ISPSRP ุนู† ุงู„ู€ class (one job). ISP ุนู† ุงู„ู€ interface (no fat APIs).
2. ุงุนุชุจุงุฑ "Square extends Rectangle" validููŠ math ู†ุนู…ุŒ ููŠ OOP ู„ุฃ โ€” ุจูŠูƒุณุฑ LSP.
3. DIP = "use interfaces"ู…ุด ุจุณ ุงุณุชุฎุฏุงู… interfaces. DIP = ุงู„ู€ high-level ูŠุนุชู…ุฏ ุนู„ู‰ abstractionุŒ ูˆ ุงู„ู€ low-level ูŠุนุชู…ุฏ ุนู„ู‰ ู†ูุณ ุงู„ู€ abstraction.
4. McConnell's design traits = 7 ู…ุด 10ุงู„ู€ 7 ุงู„ุตุญ: Minimal complexity ยท Loose coupling ยท Strong cohesion ยท Extensibility ยท Reusability ยท Maintainability ยท Leanness. (Fan-in/Fan-out ู…ุด ุถู…ู† ุงู„ู€ list ุจุชุงุนุฉ ุงู„ุฏูƒุชูˆุฑ).

โญ What Dr. El-Ramly Loves

๐Ÿ”ฅ SOLID ููŠ ุงู„ุงู…ุชุญุงู†
  1. Q3 (15-24 marks) โ€” Apply Strategy + DIP + Adapter ููŠ ูƒูˆุฏ ูŠุฏูˆูŠ. ุงู‚ุฑุฃ Lec 12 ูƒู…ุงู†.
  2. OCP scenario โ€” ูŠุฏูŠ ูƒูˆุฏ ููŠู‡ if/else ูˆูŠุทู„ุจ identify ุงู„ู€ violation.
  3. DIP code โ€” Winter 2025 Q1.09 โ€” ุฃูŠ ูƒูˆุฏ implement DIPุŸ
  4. Match principle to scenario โ€” squarerectangle = LSP, fat interface = ISP, ุฅู„ุฎ.