之前我们介绍了工厂设计模式,现在我们再看一下抽象工厂设计模式。抽象工程模式顾名思义就是对工厂模式的一层抽象,也是创建型模式的一种,通常用来创建一组存在相关性的对象。
UML类图大致如下:
类图比较复杂,最好用个例子来说明。比方说,国内有两家生产运动服的工厂:nike和adidas 。现在我们想购买一套运动服,包含短裤(short)和运动鞋(shoe)。通常我们都会在同一家工厂购买整套的运动服。这时抽象工厂模式就可以发挥作用了。
现在我们需要的产品有两种:短裤(short)和运动鞋(shoe)。
生产这些产品的工厂有两家:nike和adidas。
nike和adidas都是运动服厂商,可以视为是 iSportsFactory
接口的实现
然后,我们还要为具体的产品定义两个接口:
-
iShort
,它有两个实现,分别是nikeShort
和adidasShort
-
iShoe
,它也有两个实现,分别是nikeShoe
和adidasShoe
看下具体的代码:
iSportsFactory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package main
import "fmt"
type iSportsFactory interface {
makeShoe() iShoe
makeShort() iShort
} func getSportsFactory(brand string) (iSportsFactory, error) {
if brand == "adidas" {
return &adidas{}, nil
}
if brand == "nike" {
return &nike{}, nil
}
return nil, fmt.Errorf( "Wrong brand type passed" )
} |
iShort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package main
type iShort interface {
setLogo(logo string)
setSize(size int)
getLogo() string
getSize() int
} type short struct {
logo string
size int
} func (s *short) setLogo(logo string) {
s.logo = logo
} func (s *short) getLogo() string {
return s.logo
} func (s *short) setSize(size int) {
s.size = size
} func (s *short) getSize() int {
return s.size
} |
iShoe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package main
type iShoe interface {
setLogo(logo string)
setSize(size int)
getLogo() string
getSize() int
} type shoe struct {
logo string
size int
} func (s *shoe) setLogo(logo string) {
s.logo = logo
} func (s *shoe) getLogo() string {
return s.logo
} func (s *shoe) setSize(size int) {
s.size = size
} func (s *shoe) getSize() int {
return s.size
} |
nike.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package main
type nike struct {
} type nikeShoe struct {
shoe
} type nikeShort struct {
short
} func (n *nike) makeShoe() iShoe {
return &nikeShoe{
shoe: shoe{
logo: "nike" ,
size: 14,
},
}
} func (n *nike) makeShort() iShort {
return &nikeShort{
short: short{
logo: "nike" ,
size: 14,
},
}
} |
adidas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package main
type adidas struct {
} type adidasShoe struct {
shoe
} type adidasShort struct {
short
} func (a *adidas) makeShoe() iShoe {
return &adidasShoe{
shoe: shoe{
logo: "adidas" ,
size: 14,
},
}
} func (a *adidas) makeShort() iShort {
return &adidasShort{
short: short{
logo: "adidas" ,
size: 14,
},
}
} |
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package main
import "fmt"
func main() {
adidasFactory, _ := getSportsFactory( "adidas" )
nikeFactory, _ := getSportsFactory( "nike" )
nikeShoe := nikeFactory.makeShoe()
nikeShort := nikeFactory.makeShort()
adidasShoe := adidasFactory.makeShoe()
adidasShort := adidasFactory.makeShort()
printShoeDetails(nikeShoe)
printShortDetails(nikeShort)
printShoeDetails(adidasShoe)
printShortDetails(adidasShort)
} func printShoeDetails(s iShoe) {
fmt.Printf( "Logo: %s" , s.getLogo())
fmt.Println()
fmt.Printf( "Size: %d" , s.getSize())
fmt.Println()
} func printShortDetails(s iShort) {
fmt.Printf( "Logo: %s" , s.getLogo())
fmt.Println()
fmt.Printf( "Size: %d" , s.getSize())
fmt.Println()
} |
代码已上传至GitHub:zhyea / go-patterns / abstract-factory-pattern
End!
仅是学习笔记,难免出错,望不吝指点
之前我们介绍了工厂设计模式,现在我们再看一下抽象工厂设计模式。抽象工程模式顾名思义就是对工厂模式的一层抽象,也是创建型模式的一种,通常用来创建一组存在相关性的对象。
UML类图大致如下:
类图比较复杂,最好用个例子来说明。比方说,国内有两家生产运动服的工厂:nike和adidas 。现在我们想购买一套运动服,包含短裤(short)和运动鞋(shoe)。通常我们都会在同一家工厂购买整套的运动服。这时抽象工厂模式就可以发挥作用了。
现在我们需要的产品有两种:短裤(short)和运动鞋(shoe)。
生产这些产品的工厂有两家:nike和adidas。
nike和adidas都是运动服厂商,可以视为是 iSportsFactory
接口的实现
然后,我们还要为具体的产品定义两个接口:
-
iShort
,它有两个实现,分别是nikeShort
和adidasShort
-
iShoe
,它也有两个实现,分别是nikeShoe
和adidasShoe
看下具体的代码:
iSportsFactory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package main
import "fmt"
type iSportsFactory interface {
makeShoe() iShoe
makeShort() iShort
} func getSportsFactory(brand string) (iSportsFactory, error) {
if brand == "adidas" {
return &adidas{}, nil
}
if brand == "nike" {
return &nike{}, nil
}
return nil, fmt.Errorf( "Wrong brand type passed" )
} |
iShort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package main
type iShort interface {
setLogo(logo string)
setSize(size int)
getLogo() string
getSize() int
} type short struct {
logo string
size int
} func (s *short) setLogo(logo string) {
s.logo = logo
} func (s *short) getLogo() string {
return s.logo
} func (s *short) setSize(size int) {
s.size = size
} func (s *short) getSize() int {
return s.size
} |
iShoe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package main
type iShoe interface {
setLogo(logo string)
setSize(size int)
getLogo() string
getSize() int
} type shoe struct {
logo string
size int
} func (s *shoe) setLogo(logo string) {
s.logo = logo
} func (s *shoe) getLogo() string {
return s.logo
} func (s *shoe) setSize(size int) {
s.size = size
} func (s *shoe) getSize() int {
return s.size
} |
nike.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package main
type nike struct {
} type nikeShoe struct {
shoe
} type nikeShort struct {
short
} func (n *nike) makeShoe() iShoe {
return &nikeShoe{
shoe: shoe{
logo: "nike" ,
size: 14,
},
}
} func (n *nike) makeShort() iShort {
return &nikeShort{
short: short{
logo: "nike" ,
size: 14,
},
}
} |
adidas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package main
type adidas struct {
} type adidasShoe struct {
shoe
} type adidasShort struct {
short
} func (a *adidas) makeShoe() iShoe {
return &adidasShoe{
shoe: shoe{
logo: "adidas" ,
size: 14,
},
}
} func (a *adidas) makeShort() iShort {
return &adidasShort{
short: short{
logo: "adidas" ,
size: 14,
},
}
} |
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package main
import "fmt"
func main() {
adidasFactory, _ := getSportsFactory( "adidas" )
nikeFactory, _ := getSportsFactory( "nike" )
nikeShoe := nikeFactory.makeShoe()
nikeShort := nikeFactory.makeShort()
adidasShoe := adidasFactory.makeShoe()
adidasShort := adidasFactory.makeShort()
printShoeDetails(nikeShoe)
printShortDetails(nikeShort)
printShoeDetails(adidasShoe)
printShortDetails(adidasShort)
} func printShoeDetails(s iShoe) {
fmt.Printf( "Logo: %s" , s.getLogo())
fmt.Println()
fmt.Printf( "Size: %d" , s.getSize())
fmt.Println()
} func printShortDetails(s iShort) {
fmt.Printf( "Logo: %s" , s.getLogo())
fmt.Println()
fmt.Printf( "Size: %d" , s.getSize())
fmt.Println()
} |
代码已上传至GitHub:zhyea / go-patterns / abstract-factory-pattern
End!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:GoLang设计模式03 – 抽象工厂模式 - Python技术站