Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sioncojp committed Nov 12, 2021
1 parent 72aa48e commit 1c13d9c
Show file tree
Hide file tree
Showing 27 changed files with 323 additions and 165 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,29 @@
- [observer](./observer)
- [strategy](./strategy)
- [visitor](./visitor)

## 実装例
|pattern|example|
|:---|:---|
|abstract_factory||
|builder||
|factory_method||
|prototype||
|singleton||
|adapter||
|bridge||
|composite||
|decorator||
|facade||
|flyweight||
|proxy||
|chain_of_repository||
|iterator||
|Memento||
|state||
|template_method||
|command||
|mediator||
|observer||
|strategy||
|visitor||
9 changes: 7 additions & 2 deletions abstract_factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
factory: 生成しなければならないオブジェクトを事前に知ることができない場合
abstractFactory: オブジェクトを選ぶ場合

### 例題
### 例題1
- ドアを取り付ける
- 木製のドアと木製ドア取り付け職人
- 鉄製のドアと鉄製ドア取り付け職人
- 鉄製のドアと鉄製ドア取り付け職人


### 例題2
- スポーツキット
- 靴とシャツを購入する必要がある
8 changes: 4 additions & 4 deletions adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
Go は interface があり、struct の埋め込みがあるので adapter はそれで事足りる

### 例題

* 太郎くんはクラスを楽しくさせる力がある
* 太郎くんを学級委員にさせるが、太郎くんは能力がないと言う
* 成長した太郎くんは能力を身につけることができた
- オブジェクト(Lightningポート)
- 同じ機能を異なるインターフェイス(USBポート)で提供するadaptee(Windowsラップトップ)と呼ばれる別のオブジェクト
- クライアントが期待するのと同じインターフェース(Lightningポート)に準拠
- アダプターはLightningコネクターを受け入れ、その信号をUSB形式に変換して、WindowsラップトップのUSBポートに渡す
23 changes: 9 additions & 14 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package adapter

type ChairPerson interface {
OrganizeClass()
}

type TaroV2 struct {
Taro
}
func main() {
client := &client{}
mac := &mac{}

type Taro struct{}
client.insertLightningConnectorIntoComputer(mac)

func (s *TaroV2) OrganizationClass() {
println("クラスの面倒みるよ")
s.Taro.EnjoyWithClassmate()
}
windowsMachine := &windows{}
windowsMachineAdapter := &windowsAdapter{
windowMachine: windowsMachine,
}

func (s *Taro) EnjoyWithClassmate() {
println("わいわい")
client.insertLightningConnectorIntoComputer(windowsMachineAdapter)
}
6 changes: 1 addition & 5 deletions adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ package adapter
import "testing"

func TestAdapter(t *testing.T) {
t1 := &Taro{}
t1.EnjoyWithClassmate()

t2 := &TaroV2{*t1}
t2.OrganizationClass()
main()
}
10 changes: 10 additions & 0 deletions adapter/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package adapter

import "fmt"

type client struct{}

func (c *client) insertLightningConnectorIntoComputer(com computer) {
fmt.Println("Client inserts Lightning connector into computer.")
com.insertIntoLightningPort()
}
5 changes: 5 additions & 0 deletions adapter/computer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package adapter

type computer interface {
insertIntoLightningPort()
}
10 changes: 10 additions & 0 deletions adapter/mac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package adapter

import "fmt"

type mac struct {
}

func (m *mac) insertIntoLightningPort() {
fmt.Println("Lightning connector is plugged into mac machine.")
}
9 changes: 9 additions & 0 deletions adapter/windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package adapter

import "fmt"

type windows struct{}

func (w *windows) insertIntoUSBPort() {
fmt.Println("USB connector is plugged into windows machine.")
}
12 changes: 12 additions & 0 deletions adapter/windowsAdapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package adapter

import "fmt"

type windowsAdapter struct {
windowMachine *windows
}

func (w *windowsAdapter) insertIntoLightningPort() {
fmt.Println("Adapter converts Lightning signal to USB.")
w.windowMachine.insertIntoUSBPort()
}
9 changes: 8 additions & 1 deletion factory_method/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@
- DIContainer = Factoryの集合体
- factoryはconrainerパッケージを用意するなり、配列で管理するなりしたほうが、一箇所に集約されて相互importの心配をしなくてよい

### 例題
### 例題1
- TemplateMethodを応用して、
- 太郎くんはウィングガンダムが作りたいと言った
- 次郎くんはゴッドガンダムが作りたいと言った
- この際、どのガンダム作るかは彼らに任せよう!

### 例題2
- 前提として、クラスや継承などのOOP機能がないため、Goで従来のファクトリメソッドパターンを実装することは不可能
- パターンの基本バージョンであるSimpleFactoryは引き続き実装できる
- さまざまな種類の武器を作成
- iGun銃が持つべきすべてのメソッドを定義するインターフェースを作成
- ak47, musket
12 changes: 7 additions & 5 deletions prototype/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
* オブジェクトのコピーを生成することが複雑だったりする

### 例題
* あなたは雪の結晶を100枚飾ります
* 雪の結晶の形を紙にきれいに描く
* 書いた線にあわせて紙を切り抜く
* 実際にきれいに雪の結晶を描くには時間がかかり、さらに何枚も用意するとなるととんでもない
* 最初に作成したものと同じものを作ろう
- OSファイルシステム
- フォルダにはファイルとフォルダが含まれ、ファイルやフォルダが含まれる
- 各ファイルとフォルダーは、inodeインターフェイスで表すことができる
- inodeインターフェースにもclone機能がある
- fileとfolder構造体はどちらもタイプ
- printとclone関数のinode typeを実装
-
15 changes: 15 additions & 0 deletions prototype/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package prototype

import "fmt"

type file struct {
name string
}

func (f *file) print(indentation string) {
fmt.Println(indentation + f.name)
}

func (f *file) clone() inode {
return &file{name: f.name + "_clone"}
}
26 changes: 26 additions & 0 deletions prototype/folder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package prototype

import "fmt"

type folder struct {
children []inode
name string
}

func (f *folder) print(indentation string) {
fmt.Println(indentation + f.name)
for _, i := range f.children {
i.print(indentation + indentation)
}
}

func (f *folder) clone() inode {
cloneFolder := &folder{name: f.name + "_clone"}
var tempChildren []inode
for _, i := range f.children {
copy := i.clone()
tempChildren = append(tempChildren, copy)
}
cloneFolder.children = tempChildren
return cloneFolder
}
6 changes: 6 additions & 0 deletions prototype/inode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package prototype

type inode interface {
print(string)
clone() inode
}
59 changes: 20 additions & 39 deletions prototype/prototype.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,24 @@ package prototype

import "fmt"

type cloneabler interface {
Get() string
clone() cloneabler
}

type Person struct {
cloneable cloneabler
}

type Pepper struct {
Figure string
}

func Draw(s string) *Pepper {
fmt.Printf("%sを書くよ\n", s)
return &Pepper{Figure: s}
}

func Cut(s string) *Pepper {
pepper := Draw(s)
fmt.Printf("%sの形に切るお\n", pepper.Figure)
return pepper
}

func (s *Person) Register(c cloneabler) {
s.cloneable = c
}

func (s *Person) CreateClone() cloneabler {
return s.cloneable.clone()
}

func (s *Pepper) Get() string {
return s.Figure
}

// スライスならdeep copyをする必要がある
func (s *Pepper) clone() cloneabler {
return &Pepper{s.Figure}
func main() {
file1 := &file{name: "File1"}
file2 := &file{name: "File2"}
file3 := &file{name: "File3"}

folder1 := &folder{
children: []inode{file1},
name: "Folder1",
}

folder2 := &folder{
children: []inode{folder1, file2, file3},
name: "Folder2",
}
fmt.Println("\nPrinting hierarchy for Folder2")
folder2.print(" ")

cloneFolder := folder2.clone()
fmt.Println("\nPrinting hierarchy for clone Folder")
cloneFolder.print(" ")
}
12 changes: 1 addition & 11 deletions prototype/prototype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,5 @@ package prototype
import "testing"

func TestPrototype(t *testing.T) {
figure := "雪の結晶"
pepper := Cut(figure)

you := &Person{}
you.Register(pepper)

cloned := you.CreateClone()

if cloned.Get() != pepper.Get() {
t.Errorf("failed")
}
main()
}
33 changes: 0 additions & 33 deletions template_method/1/template_method.go

This file was deleted.

8 changes: 0 additions & 8 deletions template_method/1/template_method_test.go

This file was deleted.

32 changes: 0 additions & 32 deletions template_method/2/template_method.go

This file was deleted.

8 changes: 0 additions & 8 deletions template_method/2/template_method_test.go

This file was deleted.

Loading

0 comments on commit 1c13d9c

Please sign in to comment.