Skip to content

Commit fa13f07

Browse files
Merge pull request #284 from mohamed-abdelrhman/master
feat: added abstract factory to design patterns
2 parents e0e5eaa + 12da497 commit fa13f07

File tree

10 files changed

+205
-0
lines changed

10 files changed

+205
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package abstractfactorry
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
// test client code and how to use the factory
9+
10+
func TestAbstractFactory(t *testing.T) {
11+
12+
t.Run("Test Nike Factory", func(t *testing.T) {
13+
nikeFactory, _ := getSportsFactory("nike")
14+
nikeShoe := nikeFactory.makeShoe()
15+
nikeShirt := nikeFactory.makeShirt()
16+
if !reflect.DeepEqual(nikeShoe.getLogo(), "nike") {
17+
t.Errorf("got: %v, want: %v", nikeShoe.getLogo(), "nike")
18+
}
19+
if !reflect.DeepEqual(nikeShoe.getSize(), 14) {
20+
t.Errorf("got: %v, want: %v", nikeShoe.getSize(), 14)
21+
}
22+
if !reflect.DeepEqual(nikeShirt.getLogo(), "nike") {
23+
t.Errorf("got: %v, want: %v", nikeShirt.getLogo(), "nike")
24+
}
25+
if !reflect.DeepEqual(nikeShirt.getSize(), 14) {
26+
t.Errorf("got: %v, want: %v", nikeShirt.getLogo(), 14)
27+
}
28+
})
29+
30+
t.Run("Test Adidas Factory", func(t *testing.T) {
31+
adidasFactory, _ := getSportsFactory("adidas")
32+
adidasShoe := adidasFactory.makeShoe()
33+
adidasShirt := adidasFactory.makeShirt()
34+
35+
if !reflect.DeepEqual(adidasShoe.getLogo(), "adidas") {
36+
t.Errorf("got: %v, want: %v", adidasShoe.getLogo(), "adidas")
37+
}
38+
if !reflect.DeepEqual(adidasShoe.getSize(), 14) {
39+
t.Errorf("got: %v, want: %v", adidasShoe.getSize(), 14)
40+
}
41+
if !reflect.DeepEqual(adidasShirt.getLogo(), "adidas") {
42+
t.Errorf("got: %v, want: %v", adidasShirt.getLogo(), "adidas")
43+
}
44+
if !reflect.DeepEqual(adidasShirt.getSize(), 14) {
45+
t.Errorf("got: %v, want: %v", adidasShirt.getLogo(), 14)
46+
}
47+
})
48+
49+
}
50+
51+
52+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package abstractfactorry
2+
3+
// Concrete factory
4+
type adidas struct {
5+
}
6+
7+
func (a *adidas) makeShoe() iShoe {
8+
return &adidasShoe{
9+
shoe: shoe{
10+
logo: "adidas",
11+
size: 14,
12+
},
13+
}
14+
}
15+
16+
func (a *adidas) makeShirt() iShirt {
17+
return &adidasShirt{
18+
shirt: shirt{
19+
logo: "adidas",
20+
size: 14,
21+
},
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package abstractfactorry
2+
3+
// Concrete product
4+
type adidasShirt struct {
5+
shirt
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package abstractfactorry
2+
3+
// Concrete product
4+
type adidasShoe struct {
5+
shoe
6+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package abstractfactorry
2+
3+
// Abstract product
4+
5+
type iShirt interface {
6+
setLogo(logo string)
7+
setSize(size int)
8+
getLogo() string
9+
getSize() int
10+
}
11+
12+
type shirt struct {
13+
logo string
14+
size int
15+
}
16+
17+
func (s *shirt) setLogo(logo string) {
18+
s.logo = logo
19+
}
20+
21+
func (s *shirt) getLogo() string {
22+
return s.logo
23+
}
24+
25+
func (s *shirt) setSize(size int) {
26+
s.size = size
27+
}
28+
29+
func (s *shirt) getSize() int {
30+
return s.size
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package abstractfactorry
2+
3+
// Abstract product
4+
type iShoe interface {
5+
setLogo(logo string)
6+
setSize(size int)
7+
getLogo() string
8+
getSize() int
9+
}
10+
11+
type shoe struct {
12+
logo string
13+
size int
14+
}
15+
16+
func (s *shoe) setLogo(logo string) {
17+
s.logo = logo
18+
}
19+
20+
func (s *shoe) getLogo() string {
21+
return s.logo
22+
}
23+
24+
func (s *shoe) setSize(size int) {
25+
s.size = size
26+
}
27+
28+
func (s *shoe) getSize() int {
29+
return s.size
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package abstractfactorry
2+
3+
import "fmt"
4+
5+
// Abstract factory interface
6+
type iSportsFactory interface {
7+
makeShoe() iShoe
8+
makeShirt() iShirt
9+
}
10+
11+
func getSportsFactory(brand string) (iSportsFactory, error) {
12+
if brand == "adidas" {
13+
return &adidas{}, nil
14+
}
15+
16+
if brand == "nike" {
17+
return &nike{}, nil
18+
}
19+
20+
return nil, fmt.Errorf("Wrong brand type passed")
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package abstractfactorry
2+
3+
// Concrete factory
4+
type nike struct {
5+
}
6+
7+
func (n *nike) makeShoe() iShoe {
8+
return &nikeShoe{
9+
shoe: shoe{
10+
logo: "nike",
11+
size: 14,
12+
},
13+
}
14+
}
15+
16+
func (n *nike) makeShirt() iShirt {
17+
return &nikeShirt{
18+
shirt: shirt{
19+
logo: "nike",
20+
size: 14,
21+
},
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package abstractfactorry
2+
3+
// Concrete product
4+
type nikeShirt struct {
5+
shirt
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package abstractfactorry
2+
3+
// Concrete product
4+
5+
type nikeShoe struct {
6+
shoe
7+
}

0 commit comments

Comments
 (0)