Spring Framework 책에서 처음 접했는데, 디자인 패턴 중 하나로 분류하기도 한다

Maze 클래스의 인스턴스 멤버인 room, wall을 외부에서 설정할 수 있다 (의존성 주입, dependency injection)

dependency = room, wall

injection = 생성자 또는 메서드를 통해 외부(클라이언트 코드)에서 설정


<code example>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Maze:
    def __init__(self, room, wall):
        self.room = room
        self.wall = wall
    
class Room:
    pass
 
class MagicRoom(Room):
    pass
    
class Wall:
    pass
    
class MagicWall(Wall):
    pass
 
if __name__ == '__main__':
    maze = Maze(Room(), Wall())
    magic_maze = Maze(MagicRoom(), MagicWall())
cs



WRITTEN BY
hojongs
블로그 옮겼습니다 https://hojongs.github.io/