当前位置: 首页 > news >正文

wap网站代码内江 网站建设

wap网站代码,内江 网站建设,音乐中文网站模板,方一凡和磊儿做家教的网站组合模式(Composite Pattern)是一种结构型设计模式,它允许将对象组合成树形结构来表示“部分-整体”的层次关系。组合模式能够让客户端以统一的方式对待单个对象和对象集合,使得客户端在处理复杂树形结构的时候,可以以…

组合模式(Composite Pattern)是一种结构型设计模式,它允许将对象组合成树形结构来表示“部分-整体”的层次关系。组合模式能够让客户端以统一的方式对待单个对象和对象集合,使得客户端在处理复杂树形结构的时候,可以以相同的方式对待单个对象和多个对象组合。

主要组成部分:

  1. 抽象组件(Component)

    • 定义了 leaf 和 composite 的对象共同实现的接口。在这里你可以定义接口的方法。
  2. 叶子(Leaf)

    • 实现了抽象组件,代表组合中的叶子节点。叶子节点没有子节点。
  3. 组合(Composite)

    • 也实现了抽象组件,代表可以有子节点的树节点。组合节点可以包含叶子或其他组合节点。
  4. 客户端(Client)

    • 使用组合结构的代码,通常通过接口与树结构交互。

优点:

  1. 一致性:客户端可以以一致的方式对待所有组成部分,无论是单个对象还是组合对象。
  2. 易于添加新组件:可以轻松地增加新的叶子或组合,无需修改现有代码。
  3. 简化客户端代码:客户端代码可以简单地使用组合结构,不需要关注部分和整体的区别。

使用场景:

  • 需要表示对象的树形结构。
  • 客户端希望以相同的方式处理单个对象和组合对象。
  • 需要在运行时增加或删除对象。

JAVA:

创建一个文件系统的结构

// 文件系统-抽象组件
public abstract class FileSystemComponent {protected String name; //名称//构造public FileSystemComponent(String name){this.name = name;}//抽象文件详情方法public abstract void showDetails();
}
// 叶子类-文件
public class File extends FileSystemComponent{public File(String name) {super(name);}@Overridepublic void showDetails() {System.out.println("File: " + name);}
}
// 组合类
public class Folder extends FileSystemComponent{private List<FileSystemComponent> components = new ArrayList<>();public Folder(String name) {super(name);}// 添加文件/文件夹public void addComponent(FileSystemComponent component) {components.add(component);}// 删除文件public void removeComponent(FileSystemComponent component) {components.remove(component);}@Overridepublic void showDetails() {System.out.println("Folder: " + name);for (FileSystemComponent component : components) {component.showDetails();}}
}
@Test(description = "组合模式")public void compositeTest(){// 创建文件和文件夹File file1 = new File("File1.txt");File file2 = new File("File2.txt");Folder folder1 = new Folder("Folder1");folder1.addComponent(file1);folder1.addComponent(file2);File file3 = new File("File3.txt");Folder folder2 = new Folder("Folder2");folder2.addComponent(file3);// 创建根文件夹Folder rootFolder = new Folder("RootFolder");rootFolder.addComponent(folder1);rootFolder.addComponent(folder2);// 显示文件夹结构rootFolder.showDetails();}

GO:

公司的人员组织就是一个典型的树状的结构,现在假设我们现在有部分,和员工,两种角色,一个部门下面可以存在子部门和员工,员工下面不能再包含其他节点。
我们现在要实现一个统计一个部门下员工数量的功能

package composite// IOrganization 组织接口,都实现统计人数的功能
type IOrganization interface {Count() int
}// Employee 员工
type Employee struct {Name string
}// Count 统计人数
func (e Employee) Count() int {return 1
}// Department 部门
type Department struct {Name             stringSubOrganizations []IOrganization
}// Count 人数统计
func (d Department) Count() int {c := 0for _, org := range d.SubOrganizations {c += org.Count()}return c
}// AddSub 添加子节点
func (d *Department) AddSub(o IOrganization) {d.SubOrganizations = append(d.SubOrganizations, o)
}// NewOrganization 构建组织架构 demo
func NewOrganization() IOrganization {root := &Department{Name: "root"}for i := 0; i < 10; i++ {root.AddSub(&Employee{})root.AddSub(&Department{Name: "sub", SubOrganizations: []IOrganization{&Employee{}}})}return root
}
package compositeimport ("github.com/stretchr/testify/assert""testing"
)func TestComposite(t *testing.T) {got := NewOrganization().Count()assert.Equal(t, 20, got)
}

http://www.yayakq.cn/news/896804/

相关文章:

  • 金币交易网站开发wordpress默认原始图片
  • 自己做网站的软件厦门做网站哪家公司好
  • 关于怎么做网站广州营销网站制作
  • 网站开发心得都匀网站制作
  • 电商平台介绍网站模板网站视频要vip怎么看
  • 百度统计网站速度诊断网页设计与制作学什么
  • 微信游戏网站开发河南省汝州文明建设门户网站
  • vps网站管理软件注册公司咨询
  • 一个空间可以放两个网站吗网站建设与程序编辑
  • 怎样取消网站备案wordpress 文章标题调用
  • google浏览器官网下载网站优化连云港哪家强?
  • 用asp怎么做网站更改wordpress语言设置
  • 广州网站运营十年乐云seo鄂尔多斯seo
  • 如何让百度新闻收录网站文章学做网站的书哪些好
  • 做汽车销售要了解的网站营销型网站建设方法
  • 如何破解网站后台账号和密码太原网络营销
  • 杭州网站建设价格装饰设计网站模板
  • 大型公司网站建设.net 网站 iis 配置
  • 哈尔滨城市宣传片乌市seo网络营销流程
  • 如何把网站放到域名上wordpress上传后不见了
  • 安徽安庆天气预报海东地区谷歌seo网络优化
  • 易优建站网站快备
  • 东莞网站建站公司云vps怎么搭建网站
  • 物流 网站 模板自己建私人网站做外贸不好做
  • 安徽住房和建设厅网站传奇网页游戏开服
  • ps制作网站过程大型网站制作都有哪些
  • 智冠宝企业网站管理系统找兼职做酒店网站
  • 在网站上做的h5如何发到微信上网站建设税收编码
  • 做网站哪便宜专做外贸的网站
  • 外贸商城网站建网站需要什么要求