Skip to content

Commit 6087c40

Browse files
committed
add go for abstract
1 parent a254787 commit 6087c40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+901
-69
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package src
2+
3+
import (
4+
"./shop"
5+
6+
"./vehicle"
7+
)
8+
9+
/*
10+
意图:提供一个创建一系列相互依赖的接口,而无需指定它们具体的类。
11+
主要作用:主要解决接口选择的问题。在一个对象家族系统里面里面,抽象定义多个产品对象。
12+
何时使用:产品很多,形成产品系列,而系统只需要处理其中某一类的产品。
13+
如何解决:在一个产品系列里面,定义多个产品对象。由抽象类来负责创建产品工厂。
14+
*/
15+
16+
// 抽象工厂类,用来生成具体的工厂。Go里没有抽象类,用接口代替。
17+
type AbstractFactory interface {
18+
GetVehicle(vehicleType string, name string) vehicle.Vehicle
19+
20+
GetShop(shopType string, name string) shop.Shop
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package src
2+
3+
type FactoryCreator struct {
4+
}
5+
6+
// 工厂生成器,返回同一业务不同种类的产品工厂
7+
func (f *FactoryCreator) GetFactory(factoryType string) AbstractFactory {
8+
switch factoryType {
9+
case "vehicle":
10+
return &VehicleFactory{}
11+
case "shop":
12+
return &ShopFactory{}
13+
default:
14+
return nil
15+
}
16+
}
17+
18+
// 工厂生成器,返回同一业务不同种类的产品工厂,go没有static,可以用一个全局函数
19+
// func CreateFactory(name string) AbstractFactory {
20+
// switch name {
21+
// case "vehicle":
22+
// return new(VehicleFactory)
23+
// case "shop":
24+
// return new(ShopFactory)
25+
// default:
26+
// return nil
27+
// }
28+
// }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package src
2+
3+
import (
4+
"./shop"
5+
"./vehicle"
6+
)
7+
8+
type ShopFactory struct {
9+
}
10+
11+
func (s *ShopFactory) GetShop(shopType string, name string) shop.Shop {
12+
switch shopType {
13+
case "AgencyShop":
14+
return &shop.AgencyShop{
15+
Name: name,
16+
}
17+
case "DirectSaleShop":
18+
return &shop.DirectSaleShop{
19+
Name: name,
20+
}
21+
case "SupermarketShop":
22+
return &shop.SupermarketShop{
23+
Name: name,
24+
}
25+
default:
26+
return &shop.AgencyShop{
27+
Name: name,
28+
}
29+
}
30+
}
31+
32+
func (s *ShopFactory) GetVehicle(vehicleType string, name string) vehicle.Vehicle {
33+
// ignored, this function for Vehicle
34+
return nil
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package src
2+
3+
import (
4+
"./shop"
5+
6+
"./vehicle"
7+
)
8+
9+
const (
10+
VehicleType_BUS = "bus"
11+
VehicleType_CAR = "car"
12+
VehicleType_MOTORCYCLE = "motorcycle"
13+
VehicleType_VAN = "van"
14+
)
15+
16+
type VehicleFactory struct {
17+
}
18+
19+
func (v *VehicleFactory) GetVehicle(vehicleType string, name string) vehicle.Vehicle {
20+
switch vehicleType {
21+
case VehicleType_BUS:
22+
return &vehicle.Bus{
23+
Name: name,
24+
}
25+
case VehicleType_CAR:
26+
return &vehicle.Car{
27+
Name: name,
28+
}
29+
case VehicleType_MOTORCYCLE:
30+
return &vehicle.Motorcycle{
31+
Name: name,
32+
}
33+
case VehicleType_VAN:
34+
return &vehicle.Van{
35+
Name: name,
36+
}
37+
default:
38+
return nil
39+
}
40+
}
41+
42+
func (v *VehicleFactory) GetShop(shopType string, name string) shop.Shop {
43+
// ignored, this function for Shop
44+
return nil
45+
}

abstract-factory/go/src/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module src
2+
3+
go 1.15
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package shop
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type AgencyShop struct {
8+
Name string
9+
}
10+
11+
func (a *AgencyShop) GetName() string {
12+
return a.Name
13+
}
14+
15+
func (a *AgencyShop) Greetings() {
16+
fmt.Println("AgencyShop::Greetings [Name=" + a.GetName() + "]")
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package shop
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type DirectSaleShop struct {
8+
Name string
9+
}
10+
11+
func (d *DirectSaleShop) GetName() string {
12+
return d.Name
13+
}
14+
15+
func (d *DirectSaleShop) Greetings() {
16+
fmt.Println("DirectSaleShop::Greetings [Name=" + d.GetName() + "]")
17+
}
18+
19+
func (d *DirectSaleShop) Welcome() {
20+
fmt.Println("DirectSaleShop::Welcome [Name=" + d.GetName() + "]")
21+
}

abstract-factory/go/src/shop/Shop.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package shop
2+
3+
// 具体产品接口类,规范某类产品的行为
4+
type Shop interface {
5+
GetName() string
6+
Greetings()
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package shop
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type SupermarketShop struct {
8+
Name string
9+
}
10+
11+
func (s *SupermarketShop) GetName() string {
12+
return s.Name
13+
}
14+
15+
func (s *SupermarketShop) Greetings() {
16+
fmt.Println("SupermarketShop::Greetings [Name=" + s.GetName() + "]")
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package vehicle
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// 具体产品实现类
8+
type Bus struct {
9+
Name string
10+
}
11+
12+
func (b Bus) GetName() string {
13+
return b.Name
14+
}
15+
16+
func (b *Bus) Run() {
17+
fmt.Println("Bus::Run() [Name=" + b.Name + "]")
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package vehicle
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// 具体产品实现类
8+
type Car struct {
9+
Name string
10+
}
11+
12+
func (c Car) GetName() string {
13+
return c.Name
14+
}
15+
16+
func (c *Car) Run() {
17+
fmt.Println("Car::Run() [Name=" + c.Name + "]")
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package vehicle
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// 具体产品实现类
8+
type Motorcycle struct {
9+
Name string
10+
}
11+
12+
func (m Motorcycle) GetName() string {
13+
return m.Name
14+
}
15+
16+
func (m *Motorcycle) Run() {
17+
fmt.Println("Motorcycle::Run() [Name=" + m.Name + "]")
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package vehicle
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// 具体产品实现类
8+
type Van struct {
9+
Name string
10+
}
11+
12+
func (v Van) GetName() string {
13+
return v.Name
14+
}
15+
16+
func (v *Van) Run() {
17+
fmt.Println("Van::Run() [Name=" + v.Name + "]")
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package vehicle
2+
3+
// 具体产品接口类,规范某类产品的行为
4+
type Vehicle interface {
5+
GetName() string
6+
Run()
7+
}

abstract-factory/go/test/test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"../src"
7+
"../src/shop"
8+
)
9+
10+
// main包下的main入口方法
11+
func main() {
12+
fmt.Println("test start:")
13+
/**
14+
* 抽象工厂就是把生产产品的工厂也根据统一的抽象工厂来创建,
15+
* 这样不同类型的工厂可以在统一的约束下,整体上看更新加清晰。
16+
* 当声明工厂时可以通过抽象类型或具体工厂来声明,然后依据工厂来生产不同的产品。
17+
*/
18+
19+
// 通过工厂建造类获得指定工厂
20+
var FactoryCreator = &src.FactoryCreator{}
21+
var vehicleFactory = FactoryCreator.GetFactory("vehicle")
22+
23+
// 获取Bus对象,并调用它的 run 方法
24+
var bus = vehicleFactory.GetVehicle(src.VehicleType_BUS, "bus")
25+
bus.Run()
26+
27+
// 获取Car对象,并调用它的 run 方法
28+
var car = vehicleFactory.GetVehicle(src.VehicleType_CAR, "car")
29+
car.Run()
30+
31+
// 获取Motorcycle对象,并调用它的 run 方法
32+
var motorcycle = vehicleFactory.GetVehicle(src.VehicleType_MOTORCYCLE, "motorcycle")
33+
motorcycle.Run()
34+
35+
// 获取Motorcycle对象,并调用它的 run 方法
36+
var van = vehicleFactory.GetVehicle(src.VehicleType_VAN, "van")
37+
van.Run()
38+
39+
// /*********************** 分割线 ******************************************/
40+
41+
// 再次声明商店工厂
42+
var shopFactory = FactoryCreator.GetFactory("shop")
43+
// 从商店工厂获取商店对象
44+
var supermarketShop = shopFactory.GetShop("SupermarketShop", "shop")
45+
// 调用商店的方法
46+
supermarketShop.Greetings()
47+
48+
// /*********************** 分割线 ******************************************/
49+
50+
// 再声明一个商店工厂
51+
var shopFactory2 = FactoryCreator.GetFactory("shop")
52+
var directSaleShop = shopFactory2.GetShop("DirectSaleShop", "directSaleShop")
53+
directSaleShop.Greetings()
54+
// 直接类型转换失败
55+
// var directSaleShop1 = shop.DirectSaleShop(directSaleShop)
56+
// directSaleShop1.Welcome()
57+
// 通过类型断言实现类型转换
58+
var directSaleShop1 = directSaleShop.(*shop.DirectSaleShop)
59+
directSaleShop1.Welcome()
60+
}
61+
62+
/*
63+
jarry@jarrys-MacBook-Pro go % go build src/*.go
64+
jarry@jarrys-MacBook-Pro go % go run test/test.go
65+
test start:
66+
Bus::Run() [Name=bus]
67+
Car::Run() [Name=car]
68+
Motorcycle::Run() [Name=motorcycle]
69+
Van::Run() [Name=van]
70+
SupermarketShop::Greetings [Name=shop]
71+
DirectSaleShop::Greetings [Name=directSaleShop]
72+
DirectSaleShop::Welcome [Name=directSaleShop]
73+
*/

abstract-factory/java/src/shop/AgencyShop.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package src.shop;
22

3+
// 具体产品类,实现抽象产品接口
34
public class AgencyShop implements Shop {
45
@Override
56
public void greetings() {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package src.shop;
2+
3+
// 具体产品类,实现抽象产品接口
24
public class DirectSaleShop implements Shop {
35
@Override
46
public void greetings() {
57
System.out.println("DirectSaleShop::greetings().");
68
}
9+
10+
public void welcome() {
11+
System.out.println("DirectSaleShop::welcome().");
12+
}
713
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package src.shop;
2+
// 具体产品的接口,约束具体产品的行为
23
public interface Shop {
34
void greetings();
45
}

abstract-factory/java/src/shop/SupermarketShop.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package src.shop;
2+
3+
// 具体产品类,实现抽象产品接口
24
public class SupermarketShop implements Shop {
35
@Override
46
public void greetings() {

0 commit comments

Comments
 (0)