mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-21 20:36:01 +03:00
composite pattern
This commit is contained in:
parent
4f002c767b
commit
98163f1179
@ -26,7 +26,7 @@ A curated collection of idiomatic design & application patterns for Go language.
|
|||||||
| Pattern | Description | Status |
|
| Pattern | Description | Status |
|
||||||
|:-------:|:----------- |:------:|
|
|:-------:|:----------- |:------:|
|
||||||
| [Bridge](/structural/bridge/main.go) | Decouples an interface from its implementation so that the two can vary independently | ✔ |
|
| [Bridge](/structural/bridge/main.go) | Decouples an interface from its implementation so that the two can vary independently | ✔ |
|
||||||
| [Composite](/structural/composite.md) | Encapsulates and provides access to a number of different objects | ✘ |
|
| [Composite](/structural/composite/main.go) | Encapsulates and provides access to a number of different objects | ✔ |
|
||||||
| [Decorator](/structural/decorator.md) | Adds behavior to an object, statically or dynamically | ✔ |
|
| [Decorator](/structural/decorator.md) | Adds behavior to an object, statically or dynamically | ✔ |
|
||||||
| [Facade](/structural/facade.md) | Uses one type as an API to a number of others | ✘ |
|
| [Facade](/structural/facade.md) | Uses one type as an API to a number of others | ✘ |
|
||||||
| [Flyweight](/structural/flyweight.md) | Reuses existing instances of objects with similar/identical state to minimize resource usage | ✘ |
|
| [Flyweight](/structural/flyweight.md) | Reuses existing instances of objects with similar/identical state to minimize resource usage | ✘ |
|
||||||
|
68
structural/composite/main.go
Normal file
68
structural/composite/main.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
// 组合模式 composite pattern.
|
||||||
|
// 用于表示树形的结构,这里以一个web静态目录为例
|
||||||
|
package main
|
||||||
|
|
||||||
|
type File struct {
|
||||||
|
IsDir bool
|
||||||
|
Name string
|
||||||
|
ChildFile []*File
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *File)AddChild(file ...*File){
|
||||||
|
f.ChildFile = append(f.ChildFile ,file...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkFile(file *File) {
|
||||||
|
println("into dir ->" ,file.Name)
|
||||||
|
for _ ,v := range file.ChildFile{
|
||||||
|
if v.IsDir{
|
||||||
|
checkFile(v)
|
||||||
|
}else{
|
||||||
|
println("dir ->" ,file.Name ,".fileName ->" ,v.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main(){
|
||||||
|
/*
|
||||||
|
static|
|
||||||
|
-js|
|
||||||
|
-jquery.js
|
||||||
|
-main|
|
||||||
|
-index.js
|
||||||
|
-login.js
|
||||||
|
-css|
|
||||||
|
-bootstrap.css
|
||||||
|
*/
|
||||||
|
static := &File{true ,"static" ,make([]*File ,0)}
|
||||||
|
js := &File{true ,"js" ,make([]*File ,0)}
|
||||||
|
css := &File{true ,"css" ,make([]*File ,0)}
|
||||||
|
static.AddChild(js ,css)
|
||||||
|
|
||||||
|
jquery := &File{false ,"jquery.js" ,nil}
|
||||||
|
mjs := &File{true ,"main" ,make([]*File ,0)}
|
||||||
|
js.AddChild(jquery ,mjs)
|
||||||
|
|
||||||
|
injs := &File{false ,"index.js" ,nil}
|
||||||
|
lojs := &File{false ,"login.js" ,nil}
|
||||||
|
mjs.AddChild(injs ,lojs)
|
||||||
|
|
||||||
|
|
||||||
|
btstrap := &File{false ,"bootstrap.css" ,nil}
|
||||||
|
css.AddChild(btstrap)
|
||||||
|
|
||||||
|
checkFile(static)
|
||||||
|
/*
|
||||||
|
output:
|
||||||
|
into dir -> static
|
||||||
|
into dir -> js
|
||||||
|
dir -> js .fileName -> jquery.js
|
||||||
|
into dir -> main
|
||||||
|
dir -> main .fileName -> index.js
|
||||||
|
dir -> main .fileName -> login.js
|
||||||
|
into dir -> css
|
||||||
|
dir -> css .fileName -> bootstrap.css
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user