From 5fbbfbcb980e5811e98f6208813cd5b4a42df390 Mon Sep 17 00:00:00 2001 From: Edward Date: Fri, 1 May 2020 23:43:34 +0800 Subject: [PATCH] add composite mod codes --- README.md | 24 +++--- behavior/13_composite/README.md | 7 -- behavior/13_composite/composite.go | 91 ----------------------- behavior/13_composite/composite_test.go | 29 -------- images/composite.png | Bin 0 -> 6565 bytes structure/13_composite/README.md | 25 +++++++ structure/13_composite/composite.go | 26 +++++++ structure/13_composite/composite_test.go | 7 ++ structure/18_flyweight/flyweight_test.go | 3 +- structure/20_decorator/README.md | 6 +- 10 files changed, 74 insertions(+), 144 deletions(-) delete mode 100644 behavior/13_composite/README.md delete mode 100644 behavior/13_composite/composite.go delete mode 100644 behavior/13_composite/composite_test.go create mode 100644 images/composite.png create mode 100644 structure/13_composite/README.md create mode 100644 structure/13_composite/composite.go create mode 100644 structure/13_composite/composite_test.go diff --git a/README.md b/README.md index 7a432d8..19dfa2b 100644 --- a/README.md +++ b/README.md @@ -24,24 +24,24 @@ Go常用的、面向工程化和最佳实践的模式套路,包含常见的23 + [x] [外观模式(Facade)](./structure/01_facade) + [x] [适配器模式(Adapter)](./structure/02_adapter) + [x] [代理模式(Proxy)](./structure/09_proxy) -+ [组合模式(Composite)](./structure/13_composite) -+ [享元模式(Flyweight)](./structure/18_flyweight) -+ [装饰模式(Decorator)](./structure/20_decorator) ++ [ ] [复合模式(Composite)](./structure/13_composite) ++ [x] [享元模式(Flyweight)](./structure/18_flyweight) ++ [ ] [装饰模式(Decorator)](./structure/20_decorator) + [x] [桥模式(Bridge)](./structure/22_bridge) ## 行为型模式 + [x] [中介者模式(Mediator)](./behavior/08_mediator) -+ [观察者模式(Observer)](./behavior/10_observer) -+ [命令模式(Command)](./behavior/11_command) -+ [迭代器模式(Iterator)](./behavior/12_iterator) -+ [模板方法模式(Template Method)](./behavior/14_template_method) ++ [ ] [观察者模式(Observer)](./behavior/10_observer) ++ [ ] [命令模式(Command)](./behavior/11_command) ++ [ ] [迭代器模式(Iterator)](./behavior/12_iterator) ++ [ ] [模板方法模式(Template Method)](./behavior/14_template_method) + [x] [策略模式(Strategy)](./behavior/15_strategy) -+ [状态模式(State)](./behavior/behavior16_state) -+ [备忘录模式(Memento)](./behavior/17_memento) -+ [解释器模式(Interpreter)](./behavior/19_interpreter) -+ [职责链模式(Chain of Responsibility)](./behavior/21_chain_of_responsibility) -+ [访问者模式(Visitor)](./behavior/23_visitor) ++ [ ] [状态模式(State)](./behavior/behavior16_state) ++ [ ] [备忘录模式(Memento)](./behavior/17_memento) ++ [ ] [解释器模式(Interpreter)](./behavior/19_interpreter) ++ [ ] [职责链模式(Chain of Responsibility)](./behavior/21_chain_of_responsibility) ++ [ ] [访问者模式(Visitor)](./behavior/23_visitor) + [x] [闭包选项模式(Function Option)](./behavior/26_option) ## Go More diff --git a/behavior/13_composite/README.md b/behavior/13_composite/README.md deleted file mode 100644 index a91c266..0000000 --- a/behavior/13_composite/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# 组合模式 - -这里说到的组合模式不同于go本身的组合设计理念,go本身的组合宽泛的多,用于结构于结构、结构于接口、接口与接口的组合. - -这里说的组合模式是为统一对象和对象集,使得外部可以使用相同接口使用对象和对象集。 - -组合模式常用于树状结构,用于统一叶子节点和树节点的访问,并且可以用于应用某一操作到所有子节点。 diff --git a/behavior/13_composite/composite.go b/behavior/13_composite/composite.go deleted file mode 100644 index 0e3e87c..0000000 --- a/behavior/13_composite/composite.go +++ /dev/null @@ -1,91 +0,0 @@ -package composite - -import "fmt" - -type Component interface { - Parent() Component - SetParent(Component) - Name() string - SetName(string) - AddChild(Component) - Print(string) -} - -const ( - LeafNode = iota - CompositeNode -) - -func NewComponent(kind int, name string) Component { - var c Component - switch kind { - case LeafNode: - c = NewLeaf() - case CompositeNode: - c = NewComposite() - } - - c.SetName(name) - return c -} - -type component struct { - parent Component - name string -} - -func (c *component) Parent() Component { - return c.parent -} - -func (c *component) SetParent(parent Component) { - c.parent = parent -} - -func (c *component) Name() string { - return c.name -} - -func (c *component) SetName(name string) { - c.name = name -} - -func (c *component) AddChild(Component) {} - -func (c *component) Print(string) {} - -type Leaf struct { - component -} - -func NewLeaf() *Leaf { - return &Leaf{} -} - -func (c *Leaf) Print(pre string) { - fmt.Printf("%s-%s\n", pre, c.Name()) -} - -type Composite struct { - component - childs []Component -} - -func NewComposite() *Composite { - return &Composite{ - childs: make([]Component, 0), - } -} - -func (c *Composite) AddChild(child Component) { - child.SetParent(c) - c.childs = append(c.childs, child) -} - -func (c *Composite) Print(pre string) { - fmt.Printf("%s+%s\n", pre, c.Name()) - pre += " " - for _, comp := range c.childs { - comp.Print(pre) - } -} diff --git a/behavior/13_composite/composite_test.go b/behavior/13_composite/composite_test.go deleted file mode 100644 index f1d207f..0000000 --- a/behavior/13_composite/composite_test.go +++ /dev/null @@ -1,29 +0,0 @@ -package composite - -func ExampleComposite() { - root := NewComponent(CompositeNode, "root") - c1 := NewComponent(CompositeNode, "c1") - c2 := NewComponent(CompositeNode, "c2") - c3 := NewComponent(CompositeNode, "c3") - - l1 := NewComponent(LeafNode, "l1") - l2 := NewComponent(LeafNode, "l2") - l3 := NewComponent(LeafNode, "l3") - - root.AddChild(c1) - root.AddChild(c2) - c1.AddChild(c3) - c1.AddChild(l1) - c2.AddChild(l2) - c2.AddChild(l3) - - root.Print("") - // Output: - // +root - // +c1 - // +c3 - // -l1 - // +c2 - // -l2 - // -l3 -} diff --git a/images/composite.png b/images/composite.png new file mode 100644 index 0000000000000000000000000000000000000000..8f29a9080acc47fbc70f600720e6075052a002e7 GIT binary patch literal 6565 zcmc(E_di?j|2~>}QPmc$s;x%a5~^wwTDw-!)`;~MY9yg%RYbIXQQNDwTCLGgBVxpg zP?cCMHDlCXMF?ug_@m1LyqCwsbOUar``}41^Lh@a-{hTYy=3fET(-O_)Q`Ln*M%MN-80Sl z{m%*$OHGN|^>*{YW^KACY47B8i*fwu0+_S{Og`RteWsYz#giG%o)ONj!DbWO%=Lf$ z^2{7Q(3#xczfZ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{2=a_c!a`+g1z`Omm{%-(|6O_*-H8Bbg8QMmSZ8YgVqUUd+;2y(B zR}gK8{acbqkzg2关系的,或者遇到可以用树状逻辑梳理和表达的问题时候,复合模式可能就是最佳的问题解决思路了 + +引申一下,在我们处理算法的时候,往往是一个输入一个输出,但是在处理过程中往往是要将问题进行分类,所以复合模式思想也可强化算法思路. + +符合模式中的核心重点是一个 :复合对象,这个复合对象,能同时代表叶子节点和非叶子节点的对接。 + + +现实生活中服务装供应商库存盘点中的衣物分类就是一个很好的例子, + +- 服装 + - 男装 + - 女装 + - 上套裙 + - 短吊带 + - 鞋帽 --->f鞋帽,从最基础通用性上来讲,不分男女 + +![一个示例](../../images/Composite.png) diff --git a/structure/13_composite/composite.go b/structure/13_composite/composite.go new file mode 100644 index 0000000..abde553 --- /dev/null +++ b/structure/13_composite/composite.go @@ -0,0 +1,26 @@ +package composite + +//ItemType 具体物品的类型 +type ItemType int + +//定义两种数据类型,叶子和复合节点 +const ( + ManCloth ItemType = iota + WomanCloth + HatOrShose +) + +//IWearingItem 定义复合类型的接口,表示穿戴物品 +type IWearingItem interface { + GetWearingType() ItemType +} + +//CompositedComponet 复合类型,复合类型知道所有的具体类型 +type CompositedComponet struct { +} + +//NewComponent return a concrate +func NewComponent(kind int, name string) IWearingItem { + + return nil +} diff --git a/structure/13_composite/composite_test.go b/structure/13_composite/composite_test.go new file mode 100644 index 0000000..143a6bf --- /dev/null +++ b/structure/13_composite/composite_test.go @@ -0,0 +1,7 @@ +package composite + +import "testing" + +func TestComposite(t *testing.T) { + +} diff --git a/structure/18_flyweight/flyweight_test.go b/structure/18_flyweight/flyweight_test.go index f2bd3ab..8b25354 100644 --- a/structure/18_flyweight/flyweight_test.go +++ b/structure/18_flyweight/flyweight_test.go @@ -3,7 +3,7 @@ * @Author: Edward * @Date: 2020-05-01 17:24:28 * @Last Modified by: Edward - * @Last Modified time: 2020-05-01 19:45:32 + * @Last Modified time: 2020-05-01 19:46:30 */ package flyweight @@ -24,6 +24,7 @@ func TestDeliveryPackets(t *testing.T) { deliver2 := dc.GetDeliver("lily") + //一次送货任务 dc.DeliverTask("lily", []string{"box1", "box2"}) dc.DeliverTask("lily", []string{"box6", "box7", "box8"}) diff --git a/structure/20_decorator/README.md b/structure/20_decorator/README.md index 242cf68..9d39ead 100644 --- a/structure/20_decorator/README.md +++ b/structure/20_decorator/README.md @@ -1,7 +1,5 @@ # 装饰模式 -装饰模式使用对象组合的方式动态改变或增加对象行为。 +装饰模式就是比较简单了,就是在不改变原有对象和类型的情况下,对其进行装饰,扩展,以达到公共强化和扩展的目的。 -Go语言借助于匿名组合和非入侵式接口可以很方便实现装饰模式。 - -使用匿名组合,在装饰器中不必显式定义转调原对象方法。 +Go语言借助于匿名组合和非入侵式接口可以很方便实现装饰模式。使用匿名组合,在装饰器中不必显式定义转调原对象方法。