Go언어에서의 DDD

Reference https://dev.to/stevensunflash/using-domain-driven-design-ddd-in-golang-3ee5 https://github.com/victorsteven/food-app-server DDD(Domain Driven Design) DDD의 4Layers Domain : domain이 위치하고, 애플리케이션의 비즈니스로직이 정의된 곳 Infrastructure : 애플리케이션과 독립적인 모든 것이 존재하는 곳(외부 라이브러리, 데이터베이스 엔진 등) Application : domain과 interface 사이의 통로. interface layer에서 domain layer로 요청을 보내고 응답을 반환 Interface : 웹 서비스, RIM 인터페이스 웹 애플리케이션, 배치 프로세스 등 다른 시스템과 상호작용 하는 모든 것이 위치 시작 이 프로젝트는 구조는 아래와 같다....

October 28, 2021 · 10 min · icecat471

[디자인패턴] Decorator 패턴

Decorator Pattern 출처: https://golangbyexample.com/decorator-pattern-golang/ 개요 개체를 변경하지 않고 기능을 추가할 수 있음. => 이미 테스트가 끝난 코드를 수정하면 안됨 (Open-Closed Principle에 위배됨) 두가지 피자가 존재한다고 가정해보자. vegge mania pizza peppy tofu pizza UML 다이어그램 코드 pizza.go package main type pizza interface { getPrice() int } peppyPaneer.go package main type peppyPaneer struct { } func (p *peppyPaneer) getPrice() int { return 20 } veggeMania.go package main type veggeMania struct { } func (p *veggeMania) getPrice() int { return 15 } 토핑을 추가하기 위해 위쪽에 만들어진 구조체들은 더 이상 수정하면 안됨....

April 21, 2021 · 2 min · icecat471

[디자인패턴] Composite 패턴

Composite Pattern 출처: https://golangbyexample.com/composite-design-pattern-golang/ 개요 ‘composite’라고 불리는 개체그룹이 단일개체와 유사한 방식으로 처리되기를 원할때 사용. 트리구조로 객체들을 엮는다. UML 다이어그램 OS의 파일시스템에는 폴더와 파일 두가지 유형의 개체가 있는데, 폴더와 파일은 동일하게 취급받는 경우가 있다. Mapping Component interface component.go Composite folder.go Leaf file.go client main.go 코드 component.go package main type component interface { search(string) } folder....

April 20, 2021 · 2 min · icecat471

[디자인패턴] Bridge 패턴

Bridge Pattern 출처: https://golangbyexample.com/bridge-design-pattern-in-go/ 개요 구현부에서 추상층을 분리하여 각자 독립적으로 변형할 수 있게 하는 패턴이다.이 패턴은 큰 클래스를 두개의 개별 계층으로 나누는것을 제안한다. Abstraction: interface. Implementation에 대한 참조가 포함됨. Abstraction의 자식을 Refined Abstraction 이라고 부름. Implementation: interface. Implementation의 자식을 Concrete Implementation 이라고 부름. UML 다이어그램 2가지 유형의 컴퓨터 Mac과 Windows가 있다고 가정. 2가지 유형의 프린터 epson과 hp가 있다고 가정. 2*2의 조합의 4개의 구조체를 만드는 대신 2개의 계층을 만든다....

April 19, 2021 · 2 min · icecat471

[디자인패턴] Adapter 패턴

Adapter Pattern 한 클래스의 인터페이스를 클라이언트에서 사용하고자하는 다른 인터페이스로 변환한다.어댑터를 이용하면 인터페이스 호환성 문제 때문에 같이 쓸 수 없는 클래스들을 연결해서 쓸 수 있다.코드 출처: https://refactoring.guru/design-patterns/adapter/go/example 윈도우에는 USB, 맥북에는 thunderbolt 포트가 있다.윈도우 PC에 thunderbolt포트를 연결하기 위해 adapter가 필요하다client.go package main import "fmt" type client struct { } func (c *client) insertLightningConnectorIntoComputer(com computer) { fmt.Println("Client inserts Lightning connector into computer.") com.insertIntoLightningPort() } computer.go package main type computer interface { insertIntoLightningPort() } mac....

April 18, 2021 · 1 min · icecat471

[디자인패턴] Singleton 패턴

Singleton 패턴 하나의 클래스에 대해 하나의 인스턴스만 존재하는 패턴go에서는 sync.Once를 통해 구현 가능하다. package main import ( "fmt" "sync" ) type User struct { uid int name string level int } type userDatabase struct { users map[int]*User } var once sync.Once var instance *userDatabase func (d *userDatabase) GetUser(uid int) (*User, bool) { user, ok := d.users[uid] return user, ok } func GetUserDatabase() *userDatabase { // sync.Once.Do()는 딱 한번만 실행 once....

April 18, 2021 · 2 min · icecat471

[디자인패턴] Prototype 패턴

프로토타입 패턴 프로토타입 패턴(prototype pattern)은 생성할 객체들의 타입이 프로토타입인 인스턴스로부터 결정되도록 하며, 인스턴스는 새 객체를 만들기 위해 자신을 복제(clone)하게 된다.얕은 복사(shallow copy) vs 깊은 복사(deep copy) 얕은 복사 객체를 복사할때 참조값을 가진 멤버는 참조값만 복사 됨. type Status struct { str int dex int wis int } type Monster struct { Name string Status *Status } func main() { slime := Monster{ "slime", &Status{1,1,1}, } goblin := slime goblin.Name = "goblin" goblin....

April 17, 2021 · 2 min · icecat471

[디자인패턴] Factory 패턴

Factory Pattern 1. factory method pattern 객체를 생성하기 위한 인터페이스를 정의하는데, 어떤 클래스의 인스턴스를 만들지는 서브클래스에서 결정한다. package main import ( "errors" "fmt" ) type Character interface { Attack() } const ( warrior = iota mage ) type Warrior struct { } func (w *Warrior) Attack() { fmt.Println("sword attack") } type Mage struct { } func (m *Mage) Attack() { fmt.Println("magic attack") } Factory struct type CharacterFactory struct { } func (f *CharacterFactory) Create(job int) (Character, error) { switch job { case warrior: return &Warrior{}, nil case mage: return &Mage{}, nil default: return nil, errors....

April 16, 2021 · 2 min · icecat471

[디자인패턴] Builder 패턴

1. Builder Pattern Person 객체를 예로 들어보자 type Person struct { name string age int job string height float32 weight float32 } func NewPerson(name string, age int, job string, height float32, weight float32) *Person { return &Person{ name, age, job, height, weight, } } func main() { p := NewPerson("홍길동", 22, "developer", 177.2, 72.9) } 위처럼 모든 정보를 입력하지 않아도 될때도 있고, 가독성도 좋지않으며 parameter의 순서도 맞춰주어야한다.또 객체에 새로운 정보가 추가된다면 NewPerson()을 계속 수정해주어야 한다....

April 16, 2021 · 2 min · icecat471

[Go언어] VSCode에서 gopls 에러가 날때

vscode에서 아래와 같은 에러 발생시 gopls requires a module at the root of your workspace. You can work with multiple modules by opening each one as a workspace folder. settings.json 파일에 아래와 같이 추가하고 vscode를 재실행한다. "gopls": { "experimentalWorkspaceModule": true, },

April 16, 2021 · 1 min · icecat471