전체 글에 해당하는 글 350

CS Study - 면접을위한 CS전공 지식 노트 1

IT/Web Programming|2024. 2. 13. 20:59

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

 


https://github.com/backend-deep-dive/CS-Study/blob/main/1.%20%EB%94%94%EC%9E%90%EC%9D%B8%20%ED%8C%A8%ED%84%B4%EA%B3%BC%20%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D%20%ED%8C%A8%EB%9F%AC%EB%8B%A4%EC%9E%84(1)/1.%20%EB%94%94%EC%9E%90%EC%9D%B8%20%ED%8C%A8%ED%84%B4%EA%B3%BC%20%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D%20%ED%8C%A8%EB%9F%AC%EB%8B%A4%EC%9E%84(1)_%EC%86%A1%EB%AF%BC%EC%A7%84.md

 

반응형

댓글()