go-pattern-examples/structure/13_composite/composite_test.go

61 lines
1.2 KiB
Go
Raw Normal View History

2020-05-01 18:43:34 +03:00
package composite
import "testing"
2020-05-02 12:14:20 +03:00
//我们目的是:使用一致性的方式访问一类具有共性并相互组合起来的对象
func TestCompositeconnections(t *testing.T) {
box1 := &Box{Cargo: Cargo{1, "Big Box"}, InnerSpace: 130000}
box2 := &Box{Cargo{2, "Middle Box"}, 80000, nil}
box1.PutInCargo(box2)
cargo1 := &SingleCargo{Cargo{3, "Hat"}, "CN", "CA"}
box1.PutInCargo(cargo1)
cargo2 := &SingleCargo{Cargo: Cargo{4, "Men Cloth"}, From: "China", To: "UK"}
cargo3 := &SingleCargo{Cargo: Cargo{5, "Women Cloth"}, From: "HK", To: "TW"}
box2.PutInCargo(cargo2)
box2.PutInCargo(cargo3)
box1.ShowContent()
//Box1
// -Box2
// -Cargo 2
// -Cargo 3
// -Cargo1
}
2020-05-01 18:43:34 +03:00
func TestComposite(t *testing.T) {
2020-05-02 12:14:20 +03:00
box1 := Box{Cargo: Cargo{1, "Big Box"}, InnerSpace: 130000}
box1.ShowContent()
box2 := Box{Cargo{2, "Middle Box"}, 80000, nil}
box2.ShowContent()
cargo1 := SingleCargo{Cargo{1, "Hat"}, "CN", "CA"}
cargo1.ShowContent()
cargo2 := SingleCargo{Cargo: Cargo{2, "Men Cloth"}, From: "China", To: "UK"}
cargo2.ShowContent()
cargo3 := &SingleCargo{Cargo: Cargo{2, "Women Cloth"}, From: "HK", To: "TW"}
cargo3.ShowContent()
//Call base class
cargo3.Cargo.ShowContent()
2020-05-01 18:43:34 +03:00
}