Advertisement
vaidesh

SOLID principles problems

Dec 7th, 2024 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.07 KB | Software | 0 0
  1. Here are some **problems** designed to help you identify which SOLID principle is violated. Analyze each scenario and determine which principle isn’t followed:
  2.  
  3. ---
  4.  
  5. ### **Problem 1: Multiple Responsibilities**  
  6. A class `FileManager` handles the following tasks:  
  7. 1. Reading a file from the disk.  
  8. 2. Parsing the file content to a specific format.  
  9. 3. Saving the parsed data into a database.  
  10.  
  11. ---
  12.  
  13. ### **Problem 2: Inflexible Class Extension**  
  14. A `DiscountCalculator` class is designed to calculate discounts for products.  
  15. - It has a method `calculateDiscount(String customerType)` that applies different discounts based on `customerType`.  
  16. - Adding a new `customerType` requires modifying the `calculateDiscount` method.  
  17.  
  18. ---
  19.  
  20. ### **Problem 3: Lacking Substitutability**  
  21. A `Rectangle` class has `getWidth` and `getHeight` methods.  
  22. A subclass `Square` overrides these methods, ensuring that the width and height are always equal.  
  23. However, this breaks certain algorithms that expect rectangles to behave normally.  
  24.  
  25. ---
  26.  
  27. ### **Problem 4: Unnecessary Dependencies**  
  28. A `Printer` class depends on a `Database` class to retrieve printable data.  
  29. This makes the `Printer` class impossible to reuse in scenarios where data doesn’t come from a database (e.g., reading from a file or network).  
  30.  
  31. ---
  32.  
  33. ### **Problem 5: Violating Interface Segregation**  
  34. An interface `Bird` defines the following methods:  
  35. - `fly()`, `swim()`, and `chirp()`.  
  36. Both `Duck` and `Penguin` classes implement this interface.  
  37. - While `Penguin` can swim and chirp, it doesn’t fly, so it leaves the `fly()` method unimplemented.  
  38.  
  39. ---
  40.  
  41. ### **Problem 6: Hard-Coded Dependencies**  
  42. A `ReportGenerator` class internally creates a `PDFGenerator` instance to generate PDF reports.  
  43. This makes it impossible to use the same `ReportGenerator` for other formats like Excel or CSV.  
  44.  
  45. ---
  46.  
  47. ### **Problem 7: Tight Coupling**  
  48. A `UserService` class interacts directly with a `MySQLDatabase` class for data operations.  
  49. The system cannot switch to another database type (e.g., PostgreSQL or MongoDB) without modifying the `UserService` class.  
  50.  
  51. ---
  52.  
  53. ### **Problem 8: Responsibility Confusion**  
  54. A `Logger` class is responsible for:  
  55. 1. Writing logs to the console or a file.  
  56. 2. Deciding what type of logs (info, debug, error) should be written.  
  57.  
  58. ---
  59.  
  60. ### **Problem 9: Overloaded Interfaces**  
  61. A `Shape` interface has the following methods:  
  62. - `drawCircle()`, `drawSquare()`, and `drawRectangle()`.  
  63. The `Circle` class implements this interface, but it only needs the `drawCircle()` method, leaving the other methods unimplemented.  
  64.  
  65. ---
  66.  
  67. ### **Problem 10: Lack of Dependency Injection**  
  68. A `NotificationService` class creates an `EmailSender` object internally to send notifications.  
  69. Switching to an `SMSSender` requires modifying the `NotificationService` class.  
  70.  
  71. ---
  72.  
  73. These problems simulate real-world challenges and are excellent for sharpening your understanding of SOLID principles. Focus on analyzing the scenarios and identifying the violated principle(s).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement