-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# adapter | ||
インターフェイスに互換性のないクラス同士を組み合わせる | ||
継承や移譲を利用した手法がある | ||
|
||
### メリット | ||
* 既存のクラスに対して修正を加えること無くインターフェイスを変更出来る | ||
* 一貫したインターフェイスの提供 | ||
|
||
### デメリット | ||
* unit testしづらい | ||
* 安易に使い回すとスパゲティになるかも | ||
* 小さいパーツに、シンプルに、疎結合にすること | ||
|
||
### Goのadapter | ||
Goはinterfaceがあり、structの埋め込みがあるのでadapterはそれで事足りる |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package adapter | ||
|
||
import "fmt" | ||
|
||
type FruitPrice interface { | ||
getPrice() | ||
} | ||
|
||
type Fruit struct { | ||
Name string | ||
Cost int | ||
} | ||
|
||
type FruitAdapter struct { | ||
Fruit | ||
} | ||
|
||
func (s *FruitAdapter) GetPrice() { | ||
s.Fruit.GetPrice() | ||
} | ||
|
||
func (s *Fruit) GetPrice() { | ||
fmt.Printf("%s: %d\n", s.Name, s.Cost) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package adapter | ||
|
||
import "testing" | ||
|
||
func TestAdapterExtends(t *testing.T) { | ||
f1 := &Fruit{"Apple", 100} | ||
f2 := &FruitAdapter{*f1} | ||
|
||
f1.GetPrice() | ||
f2.GetPrice() | ||
} |