Skip to content

Commit 72c2a43

Browse files
Best Practices and Quiz 2 Added
1 parent 76e495c commit 72c2a43

File tree

3 files changed

+340
-1
lines changed

3 files changed

+340
-1
lines changed

content/Best Practices.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
tags:
3+
- bestpractices
4+
created: 2026-02-11
5+
title: Best Practices
6+
---
7+
You have completed all the necessary design patterns required while implementing your solution. However, it is not fixed that you must always use these design patterns exactly as defined. You can always tweak or adapt a pattern according to your own solution and requirements. The main goal of using design patterns is to make the code more maintainable, flexible, and understandable not to increase complexity unnecessarily.
8+
9+
1. The first important principle is **Keep It Simple, Stupid (KISS)**. Your goal should always be simplicity. Do not force yourself to use a design pattern just because you feel developers are expected to use them. If a simple solution solves the problem clearly and efficiently, then that solution is perfectly acceptable. Over-engineering a simple problem often makes the code harder to read and maintain.
10+
2. Another good practice is to **apply a design pattern only when your current implementation no longer meets your needs**. If the code becomes difficult to extend, maintain, or reuse, then it may be the right time to introduce a suitable pattern. Before applying any pattern, think about possible future scenarios and whether the pattern actually helps solve those problems.
11+
3. It is also important to **understand the problem clearly before choosing any pattern**. A design pattern is a tool, not a rule. Choosing a pattern without understanding the real problem can make the design unnecessarily complex. Always focus first on the requirements, then decide whether a pattern is truly needed.
12+
4. Another useful practice is to **keep classes and methods small and focused**. Each class should have a clear responsibility. When a class starts doing too many things, it becomes difficult to modify or test. Design patterns often help in separating responsibilities, but even without patterns, keeping code modular and organized is a good habit.
13+
5. You should also **prefer readability over cleverness**. Code is read more often than it is written. A straightforward implementation that everyone on the team can understand is usually better than a highly abstract design that only a few people understand.
14+
6. It is equally important to **avoid premature abstraction**. Do not generalize code too early. Write the simplest working solution first, and only introduce abstractions when you see repetition or a real need for flexibility. This approach keeps the design clean and prevents unnecessary layers.
15+
7. Another best practice is to **document your intent when using a pattern**. Even a short comment explaining why a pattern was used can help other developers understand the design decisions quickly.
16+
17+
Finally, remember that **design patterns are guidelines, not strict rules**. Real-world software often requires adapting or combining ideas rather than strictly following textbook implementations. The best design is the one that solves the problem clearly, remains easy to maintain, and can evolve with changing requirements.
18+
19+
### Interview CheatSheet
20+
21+
- [[Strategy Pattern]] :
22+
**Trigger clue:** “Different algorithms, interchangeable at runtime”
23+
**Core idea:** Encapsulate algorithms and make them interchangeable so the behavior can be selected at runtime.
24+
25+
- [[Observer Pattern]] :
26+
**Trigger clue:** “One-to-many dependency” or “Event notification system”
27+
**Core idea:** When one object (Subject) changes state, all dependent objects (Observers) are notified automatically.
28+
29+
- [[Decorator Pattern]] :
30+
**Trigger clue:** “Add behavior dynamically without modifying existing code”
31+
**Core idea:** Wrap an object inside another object to extend behavior at runtime.
32+
33+
- [[Factory Method Pattern]] :
34+
**Trigger clue:** “Defer object creation to subclasses”
35+
**Core idea:** Define an interface for creating objects but let subclasses decide which class to instantiate.
36+
37+
- [[Abstract Factory Pattern]] :
38+
**Trigger clue:** “Create families of related objects”
39+
**Core idea:** Provide an interface to create related objects without specifying their concrete classes.
40+
41+
- [[Singleton Pattern]] :
42+
**Trigger clue:** “Only one instance should exist”
43+
**Core idea:** Ensure a class has only one instance and provide a global access point.
44+
45+
- [[Command Pattern]] :
46+
**Trigger clue:** “Undo/Redo”, “Queue requests”, “Encapsulate actions”
47+
**Core idea:** Encapsulate a request as an object to parameterize clients and support undo or logging.
48+
49+
- [[Adapter Pattern]] :
50+
**Trigger clue:** “Incompatible interfaces must work together”
51+
**Core idea:** Convert one interface into another expected by the client.
52+
53+
- [[Facade Pattern]] :
54+
**Trigger clue:** “Provide a simple interface to a complex subsystem”
55+
**Core idea:** Hide system complexity behind a single unified interface.
56+
57+
- [[Iterator Pattern]] :
58+
**Trigger clue:** “Traverse a collection without exposing internal structure”
59+
**Core idea:** Provide a standard way to access elements sequentially without revealing the underlying representation.
60+
61+
- [[Composite Pattern]] :
62+
**Trigger clue:** “Tree structure” or “Treat individual and group uniformly”
63+
**Core idea:** Compose objects into tree structures and treat individual objects and compositions uniformly.
64+
65+
- [[State Pattern]] :
66+
**Trigger clue:** “Behavior changes based on internal state”
67+
**Core idea:** Allow an object to change its behavior when its internal state changes.
68+
69+
- [[Proxy Pattern]] :
70+
**Trigger clue:** “Control access to an object”
71+
**Core idea:** Provide a surrogate or placeholder that controls access to another object.
72+
73+
- [[Compound Pattern]] :
74+
**Trigger clue:** “Multiple patterns combined in one design”
75+
**Core idea:** Combine several design patterns to solve a complex problem in a structured way.

content/Quiz-2.md

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
---
2+
tags:
3+
- quiz
4+
created: 2026-02-11
5+
title: Quiz 2
6+
---
7+
All the Question are based on the pattern [[Singleton Pattern]], [[Command Pattern]], [[Adapter Pattern]], [[Facade Pattern]], [[Iterator Pattern]], [[Composite Pattern]], [[State Pattern]], [[Proxy Pattern]] & [[Compound Pattern]].
8+
#### Question 1
9+
10+
You are building an **expense dashboard app (like your CashFlow project)**.
11+
The application needs **one shared database connection** so that:
12+
- All modules use the same connection
13+
- Creating multiple connections would waste memory and slow the system
14+
15+
**Which design pattern is most appropriate here, and why?**
16+
> [!FAQ]- Answer and Explanation
17+
> **Pattern:** Singleton
18+
> **Explanation:**
19+
> A database connection is a classic **Singleton** use case because:
20+
> - Only **one instance** is needed across the application
21+
> - It provides a **global access point**
22+
> - Prevents unnecessary memory usage and connection overhead
23+
24+
---
25+
#### Question 2
26+
27+
You are designing a **remote control app** for smart home devices.
28+
Each button can:
29+
- Turn lights ON/OFF
30+
- Open/close curtains
31+
- Start the fan
32+
33+
You also want to support **Undo** functionality.
34+
**Which design pattern is most appropriate here, and why?**
35+
> [!FAQ]- Answer and Explanation
36+
> **Pattern:** Command
37+
> **Explanation:** The **Command Pattern** encapsulates each action as an object.
38+
> This allows:
39+
> - Storing commands
40+
> - Undoing operations
41+
> - Decoupling the invoker (remote) from receivers (devices)
42+
43+
---
44+
#### Question 3
45+
46+
You are integrating a **payment gateway** into an e-commerce system.
47+
Your system expects a **standard payment interface**, but the new gateway provides a **different API** that cannot be changed.
48+
49+
**Which design pattern is most appropriate here, and why?**
50+
> [!FAQ]- Answer and Explanation
51+
> **Pattern:** Adapter
52+
> **Explanation:** The **Adapter Pattern** converts one interface into another expected by the client.
53+
> It allows integration of incompatible systems without modifying existing code.
54+
55+
---
56+
#### Question 4
57+
58+
You are building a **movie streaming app**.
59+
Playing a movie involves:
60+
- Authentication
61+
- Loading subtitles
62+
- Initializing decoder
63+
- Buffering video
64+
65+
You want a simple method like:
66+
`playMovie()`
67+
68+
**Which design pattern is most appropriate here, and why?**
69+
> [!FAQ]- Answer and Explanation
70+
> **Pattern:** Facade
71+
> **Explanation:** The **Facade Pattern** provides a simplified interface to a complex subsystem, hiding internal operations from the user.
72+
73+
---
74+
#### Question 5
75+
76+
You are developing a **music playlist app**.
77+
Users should move through songs without knowing whether songs are stored in arrays, lists, or databases.
78+
79+
**Which design pattern is most appropriate here, and why?**
80+
> [!FAQ]- Answer and Explanation
81+
> **Pattern:** Iterator
82+
> **Explanation:** The **Iterator Pattern** provides a standard way to traverse a collection without exposing its internal structure.
83+
84+
---
85+
#### Question 6
86+
87+
You are building a **file explorer system** where:
88+
- A folder can contain files and other folders
89+
- Operations like `delete()` or `showSize()` should work uniformly
90+
91+
**Which design pattern is most appropriate here, and why?**
92+
> [!FAQ]- Answer and Explanation
93+
> **Pattern:** Composite
94+
> **Explanation:** The **Composite Pattern** allows treating individual objects and groups uniformly in tree structures.
95+
96+
---
97+
#### Question 7
98+
99+
You are designing a **document editor** where documents can be:
100+
- Draft
101+
- Review
102+
- Published
103+
Each state changes editing behavior.
104+
105+
**Which design pattern is most appropriate here, and why?**
106+
> [!FAQ]- Answer and Explanation
107+
> **Pattern:** State
108+
> **Explanation:** The **State Pattern** allows behavior to change dynamically based on the object's internal state.
109+
110+
---
111+
#### Question 8
112+
113+
You are developing an **image viewer** where large images should load only when opened, while placeholders are shown initially.
114+
115+
**Which design pattern is most appropriate here, and why?**
116+
> [!FAQ]- Answer and Explanation
117+
> **Pattern:** Proxy
118+
> **Explanation:** A **Virtual Proxy** delays object creation until needed, improving performance and memory usage.
119+
120+
---
121+
#### Question 9
122+
123+
You are integrating a **complex third-party payment library** and create a simple class exposing only a few methods like `startPayment()` and `verifyPayment()`.
124+
125+
**Which design pattern is most appropriate here, and why?**
126+
> [!FAQ]- Answer and Explanation
127+
> **Pattern:** Facade
128+
> **Explanation:** The **Facade Pattern** simplifies interaction with complex subsystems by providing a unified interface.
129+
130+
---
131+
#### Question 10
132+
133+
You are building a **text editor** that supports:
134+
- Copy
135+
- Paste
136+
- Cut
137+
- Undo / Redo
138+
Operations must be stored and reversible.
139+
140+
**Which design pattern is most appropriate here, and why?**
141+
> [!FAQ]- Answer and Explanation
142+
> **Pattern:** Command
143+
> **Explanation:** Commands encapsulate operations as objects, enabling undo/redo and history tracking.
144+
145+
---
146+
#### Question 11
147+
148+
You are designing a **company organizational hierarchy** where:
149+
- Managers contain employees
150+
- Employees and managers should be treated uniformly
151+
152+
**Which design pattern is most appropriate here, and why?**
153+
> [!FAQ]- Answer and Explanation
154+
> **Pattern:** Composite
155+
> **Explanation:** Composite models hierarchical structures and allows uniform treatment of individuals and groups.
156+
157+
---
158+
#### Question 12
159+
160+
A banking system performs authentication and validation before forwarding requests to the real transfer service.
161+
162+
**Which design pattern is most appropriate here, and why?**
163+
> [!FAQ]- Answer and Explanation
164+
> **Pattern:** Proxy
165+
> **Explanation:** A **Protection Proxy** controls access to another object by performing checks before forwarding requests.
166+
167+
---
168+
#### Question 13
169+
170+
You are designing a **vending machine** where behavior changes depending on:
171+
- Coin inserted
172+
- Item selected
173+
- Item dispensed
174+
175+
**Which design pattern is most appropriate here, and why?**
176+
> [!FAQ]- Answer and Explanation
177+
> **Pattern:** State
178+
> **Explanation:** Each state defines behavior, eliminating large conditional logic and making transitions easier.
179+
180+
---
181+
#### Question 14
182+
183+
Your application expects a logging interface, but a **legacy logging system** has a different method signature.
184+
185+
**Which design pattern is most appropriate here, and why?**
186+
> [!FAQ]- Answer and Explanation
187+
> **Pattern:** Adapter
188+
> **Explanation:** Adapter translates one interface into another compatible interface without modifying existing systems.
189+
190+
---
191+
#### Question 15
192+
193+
You are building a **graphics editor** where:
194+
- Shapes can be individual or grouped
195+
- Calling `draw()` should work for both
196+
197+
**Which design pattern is most appropriate here, and why?**
198+
> [!FAQ]- Answer and Explanation
199+
> **Pattern:** Composite
200+
> **Explanation:** Composite allows treating single objects and groups uniformly in tree structures.
201+
202+
---
203+
#### Question 16
204+
205+
A **library management system** stores books in multiple structures but must allow uniform traversal.
206+
207+
**Which design pattern is most appropriate here, and why?**
208+
> [!FAQ]- Answer and Explanation
209+
> **Pattern:** Iterator
210+
> **Explanation:** Iterator enables traversal without exposing internal storage details.
211+
212+
---
213+
#### Question 17
214+
215+
You are designing a **configuration manager** that must:
216+
- Load configuration once
217+
- Share it globally
218+
219+
**Which design pattern is most appropriate here, and why?**
220+
> [!FAQ]- Answer and Explanation
221+
> **Pattern:** Singleton
222+
> **Explanation:** Singleton ensures a single shared instance and controlled access.
223+
224+
---
225+
#### Question 18
226+
227+
A **video streaming platform** uses a local cache server that decides whether to fetch from cache or the main server.
228+
229+
**Which design pattern is most appropriate here, and why?**
230+
> [!FAQ]- Answer and Explanation
231+
> **Pattern:** Proxy
232+
> **Explanation:** A caching proxy controls access and improves performance by serving cached content.
233+
234+
---
235+
#### Question 19
236+
237+
A **smart home system** combines:
238+
- Command for actions
239+
- Composite for device groups
240+
- Iterator for traversal
241+
- Facade for simplified control
242+
243+
**What is this approach called?**
244+
> [!FAQ]- Answer and Explanation
245+
> **Pattern:** Compound Pattern
246+
> **Explanation:** Compound patterns combine multiple design patterns to solve complex design problems.
247+
248+
---
249+
#### Question 20
250+
251+
A **game character system** has states like:
252+
- Standing
253+
- Running
254+
- Jumping
255+
- Attacking
256+
Behavior changes dynamically with state.
257+
258+
**Which design pattern is most appropriate here, and why?**
259+
> [!FAQ]- Answer and Explanation
260+
> **Pattern:** State
261+
> **Explanation:** The State Pattern encapsulates state-specific behavior into separate classes and enables dynamic transitions.
262+

content/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ Consider an `SQLManager` class that performs CRUD operations. It has an `ILogger
138138
13. [[State Pattern]]
139139
14. [[Proxy Pattern]]
140140
15. [[Compound Pattern]]
141-
16. [[Glossary]]
141+
16. [[Best Practices]]
142+
17. [[Quiz-2]]
143+
18. [[Glossary]]
142144

143145
---
144146
> [!Note]

0 commit comments

Comments
 (0)