Skip to content

Commit 47f400d

Browse files
committed
Add Design Principles
1 parent 522e032 commit 47f400d

File tree

3 files changed

+184
-2
lines changed

3 files changed

+184
-2
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# awesome-low-low-design
2-
This repository contains low level design (LLD) resources to improve object oriented design skills and prepare for interviews.
2+
This repository contains low level design (LLD) resources to improve object oriented design (OOD) skills and prepare for interviews.
33

4-
## Low Level Design Fundamentals
4+
## Design Principles
5+
- [Basics of OOPS](concepts/OOPS.md)
56
- [SOLID Principles in Pictures](https://medium.com/backticks-tildes/the-s-o-l-i-d-principles-in-pictures-b34ce2f1e898)
67
- [SOLID Principles in Code](https://medium.com/android-news/solid-principles-the-definitive-guide-75e30a284dea)
8+
- [DRY Principle](concepts/DRY.md)
9+
- [YAGNI Principle](concepts/YAGNI.md)
10+
11+
## Design Patterns
12+
713

814
## Low Level Design Interview Problems
915
- [Design Parking Lot]

concepts/OOPS.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
## Basics of Object-Oriented Programming (OOPS)
2+
3+
Object-Oriented Programming (OOP) is a fundamental programming paradigm used in software development, defined by its use of classes and objects.
4+
5+
It’s built on four main principles: Inheritance, Polymorphism, Abstraction, and Encapsulation.
6+
7+
These principles not only help in creating structured and reusable code but also make it easier to understand, maintain, and modify.
8+
9+
### Inheritance
10+
nheritance allows one class to inherit the properties and methods of another class. It's a way to form a hierarchy between classes, promoting code reusability.
11+
#### Example:
12+
```
13+
class Vehicle {
14+
public void startEngine() {
15+
System.out.println("Engine started");
16+
}
17+
}
18+
19+
class Car extends Vehicle {
20+
public void openTrunk() {
21+
System.out.println("Trunk opened");
22+
}
23+
}
24+
25+
public class Main {
26+
public static void main(String[] args) {
27+
Car myCar = new Car();
28+
myCar.startEngine(); // Inherited method
29+
myCar.openTrunk(); // Own method
30+
}
31+
}
32+
```
33+
34+
In this Java example, `Car` inherits from `Vehicle`.
35+
36+
Car can use the startEngine method from Vehicle, demonstrating inheritance.
37+
38+
### Polymorphism
39+
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It’s the ability of multiple object types to implement the same functionality, which can be achieved either by method overloading or method overriding.
40+
41+
#### Example:
42+
```
43+
class Bird {
44+
public void sing() {
45+
System.out.println("Bird is singing");
46+
}
47+
}
48+
49+
class Sparrow extends Bird {
50+
public void sing() {
51+
System.out.println("Sparrow is singing");
52+
}
53+
}
54+
55+
public class Main {
56+
public static void main(String[] args) {
57+
Bird myBird = new Sparrow();
58+
myBird.sing(); // Outputs: Sparrow is singing
59+
}
60+
}
61+
```
62+
Here, `Sparrow` overrides the sing method of `Bird`. Despite referring to Sparrow with a Bird reference, the overridden method in Sparrow is called.
63+
64+
### Abstraction
65+
Abstraction is the concept of hiding complex implementation details and showing only the necessary features of an object. It can be achieved using abstract classes and interfaces.
66+
67+
#### Example:
68+
```
69+
abstract class Animal {
70+
abstract void makeSound();
71+
72+
public void eat() {
73+
System.out.println("Animal is eating");
74+
}
75+
}
76+
77+
class Dog extends Animal {
78+
public void makeSound() {
79+
System.out.println("Bark");
80+
}
81+
}
82+
83+
public class Main {
84+
public static void main(String[] args) {
85+
Animal myDog = new Dog();
86+
myDog.makeSound(); // Outputs: Bark
87+
myDog.eat(); // Inherited method
88+
}
89+
}
90+
```
91+
`Animal` is an abstract class that provides a method `makeSound()`.
92+
93+
`Dog` provides the specific implementation of this method.
94+
95+
### Encapsulation
96+
Encapsulation is the technique of bundling data (variables) and methods that act on the data into a single unit, often called a class, and restricting access to some of the object’s components.
97+
98+
```
99+
class BankAccount {
100+
private double balance;
101+
102+
public void deposit(double amount) {
103+
if (amount > 0) {
104+
balance += amount;
105+
}
106+
}
107+
108+
public void withdraw(double amount) {
109+
if (amount <= balance) {
110+
balance -= amount;
111+
}
112+
}
113+
114+
public double getBalance() {
115+
return balance;
116+
}
117+
}
118+
119+
public class Main {
120+
public static void main(String[] args) {
121+
BankAccount account = new BankAccount();
122+
account.deposit(1000);
123+
account.withdraw(500);
124+
System.out.println("Balance: " + account.getBalance());
125+
}
126+
}
127+
```
128+
129+
In this example, the balance of the `BankAccount` is kept private. It can only be modified through the `deposit` and `withdraw` methods and read through the `getBalance` method, showcasing encapsulation.

concepts/YAGNI.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# The YAGNI Principle
2+
YAGNI (You Aren't Gonna Need It) is a mantra in software development that encourages developers to implement things only when they are necessary.
3+
4+
## Understanding YAGNI
5+
YAGNI, an acronym for "You Aren't Gonna Need It", is a principle that recommends against adding functionality until it is deemed necessary.
6+
7+
Ron Jeffries, one of the creators of Extreme Programming, coined this term to prevent over-engineering and to keep software development as efficient and straightforward as possible.
8+
9+
### The Benefits of YAGNI
10+
- **Reduces Complexity:** By not adding unnecessary features, the codebase remains simpler and more manageable.
11+
- **Saves Time and Resources:** Time is not wasted on features that may never be used, allowing more focus on the current requirements.
12+
- **Easier Maintenance:** Fewer features mean less code to test and maintain.
13+
- **Increases Flexibility:** Without unnecessary code, it is easier to adapt the software to new requirements.
14+
15+
## Implementing YAGNI: Practical Examples
16+
17+
### Example 1: Avoiding Premature Generalization
18+
19+
**Non-YAGNI Approach:**
20+
21+
Imagine you are building a blog platform and decide to implement a complex tagging system that allows nested tags, even though the initial requirements only mention simple tags.
22+
23+
**YAGNI Approach:**
24+
25+
You implement a straightforward tagging system that meets the current requirements. If, in the future, the need for nested tags arises, you can extend the system then.
26+
27+
### Example 2: Avoiding Unnecessary Features
28+
29+
**Non-YAGNI Approach:**
30+
31+
Developing a feature for exporting reports in five different formats when the client has asked for only one specific format.
32+
33+
**YAGNI Approach:**
34+
35+
You focus on creating a robust and efficient export feature for the requested format. If the need for additional formats arises later, they can be added at that time.
36+
37+
## Tips for Practicing YAGNI
38+
39+
1. **Focus on Requirements:** Always align your development efforts with the current requirements, not on what you think might be needed in the future.
40+
2. **Refactor When Needed:** Apply YAGNI in conjunction with refactoring. If a new requirement arises, refactor your code to accommodate it.
41+
3. **Continuous Feedback:** Regularly communicate with stakeholders to ensure that what is being developed is what is actually needed.
42+
4. **Avoid Speculative Generality:** Do not generalize the solution for potential future problems. Keep solutions specific to the problem at hand.
43+
44+
## Conclusion
45+
The YAGNI principle is a powerful tool in the arsenal of a software developer. It encourages simplicity and efficiency, reminding us to focus on what is needed now rather than getting lost in the complexities of what might be needed in the future.
46+
47+
By adhering to YAGNI, developers can create software that is not only easier to maintain and extend but also delivers value faster to the end user.

0 commit comments

Comments
 (0)