CS Study - 면접을위한 CS전공 지식 노트 1
1. Design Patterns
1.1. Singleton Pattern :
Based on my class, only one instance is created to form the logic used.
Even if the constructor is called multiple times, the actual object created is one, and constructors called after the first creation return the object created by the first constructor.
A disadvantage is that it creates strong coupling between modules.
To slightly loosen the coupling between modules, the concept of Dependency Injection (DI) can be used.
The Singleton pattern is used when there is a need to ensure that a class has only one instance such as DB connection , central login system.. etc
Pros and Cons :
Pros:
1. Controlled Access
2. Reduced Memory Overhead
3. Initializaion Control : allows for lazy intitializaion, where the instance is creatd only when it is needed for the first time. reducing start up time
Cons:
1. Tight Coupling, Testing Challenge, Global State( may lead to hidden dependencies )
2. Scalability Issues : ensure the singleton pattern is thread-safe in multi-threaded application
3. OOP : Undermines Inheritance , Polymorphism, Information Hiding
By the nature of this pattern, it restreicts the creation of multiple instances, limiting ability to use inheritance ,leaverge plymorphism and it is usually public so compromising infomration hiding
+ Dependency Injection (DI)
Dependency Injection (DI) is a design pattern where a class receives its dependencies from outside rather than creating them itself, promoting loose coupling
example ) Implementing Dependency Injection
In an Express.js application, when creating an instance of UserService, an instance of userRepository is injected through the constructor. To achieve this, first create an instance of userRepository and then pass it to the constructor of UserService.
Copy code
class UserService {
constructor(userRepository) {
this.userRepository = userRepository;
}
findUser(id) {
return this.userRepository.find(id);
}
}
Pros and Cons :
Pros:
1. Easier testing
2. Loose Coupling
3. Improved code maintainability + Increased flexibility
4. Enhence and support OOP : encapsulation , abstraction , inheritance ,Polymorphism
Cons:
1. Performance Issue : creating more class at the first might end up taking longer time
2. Complexity
1.2. Factory Design Pattern :
The Factory Design Pattern involves using interfaces to define the functions for each class and a factory to instantiate these classes, allowing client-side code to utilize these instances through the interface. This approach effectively encapsulates the object creation process, promoting decoupled code, easier maintenance, and adherence to SOLID principles.
Pros and Cons :
Pros:
1. Easier testing
2. Loose Coupling
3. Improved code maintainability + Increased flexibility
4. Enhence and support OOP : Encapsulation Abstraction Polymorphism Adherence to SOLID Principles
Cons:
1. Development Overhead
2. Complexity
/ Define Shape interfaces and their implementations
class Circle {
draw() {
console.log('Drawing a Circle');
}
}
class Square {
draw() {
console.log('Drawing a Square');
}
}
class Rectangle {
draw() {
console.log('Drawing a Rectangle');
}
}
// Factory for creating instances of shapes
class ShapeFactory {
getShape(shapeType) {
if (!shapeType) {
return null;
}
switch (shapeType.toLowerCase()) {
case 'circle':
return new Circle();
case 'square':
return new Square();
case 'rectangle':
return new Rectangle();
default:
return null;
}
}
}
// Usage
const shapeFactory = new ShapeFactory();
// Create a Circle
const circle = shapeFactory.getShape('circle');
circle.draw(); // Output: Drawing a Circle
// Create a Square
const square = shapeFactory.getShape('square');
square.draw(); // Output: Drawing a Square
// Create a Rectangle
const rectangle = shapeFactory.getShape('rectangle');
rectangle.draw(); // Output: Drawing a Rectangle
'IT > Web Programming' 카테고리의 다른 글
[Core Javascrpt] 01. Data type (0) | 2023.06.19 |
---|---|
[React] Week1_1 (0) | 2023.05.22 |
[ 토크부트] 토크부트(talkbout) 부트캠퍼들의 커뮤니티 (0) | 2021.08.28 |
[ 토크부트] 부트캠퍼들의 속시원한 이야기 토크부트(talkbout) (0) | 2021.08.27 |
[Django] Python Django 파이썬 장고 (0) | 2021.03.08 |