gpt4 book ai didi

c# - 嵌套菜单生成器永远不会在 c# 中正确结束

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:13 27 4
gpt4 key购买 nike

在这里,我正在构建具有 n 个叶和分支的菜单生成器,现在我因无法正确返回生成器的引用而无能为力。

这是代码

public interface IMenuBuilder
{
IMenuBuilder AddLeaf(string name);
ISubMenuBuilder AddBranch(string name);
void Build();
}

public interface ISubMenuBuilder : IMenuBuilder
{
ISubMenuBuilder AddLeaf(string name);
INestSubMenuBuilder AddBranch(string name);
IMenuBuilder Build();
}

public interface INestSubMenuBuilder : ISubMenuBuilder
{
INestSubMenuBuilder AddLeaf(string name);
INestSubMenuBuilder AddBranch(string name);
ISubMenuBuilder Build();
}

public class MenuBuilder : IMenuBuilder
{
List<Menu> menus = new List<Menu>();
public IMenuBuilder AddLeaf(string name)
{
var menu = new RootMenu { Name = name };
menus.Add(menu);
return this;
}

public ISubMenuBuilder AddBranch(string name)
{
var menu = new RootMenu { Name = name };
menus.Add(menu);
return new SubMenuBuilder(this, menu);
}

public void Build()
{
}
}

public class SubMenuBuilder : ISubMenuBuilder
{
private IMenuBuilder menuBuilder;
private ISubMenuHolder menu;

public SubMenuBuilder(IMenuBuilder menuBuilder, ISubMenuHolder menu)
{
this.menuBuilder = menuBuilder;
this.menu = menu;
}

protected SubMenuBuilder(ISubMenuHolder menu)
{
this.menu = menu;
}

public ISubMenuBuilder AddLeaf(string name)
{
var leafMenu = new LeafMenu { Name = name };
leafMenu.ParentId = (leafMenu as Menu).Id;
menu.AddSubMenu(leafMenu);
return this;
}

public INestSubMenuBuilder AddBranch(string name)
{
var branchMenu = new BranchMenu { Name = name };
branchMenu.ParentId = (branchMenu as Menu).Id;
menu.AddSubMenu(branchMenu);
return new NestSubMenuBuilder(this, branchMenu);
}

public IMenuBuilder Build()
{
return menuBuilder;
}


IMenuBuilder IMenuBuilder.AddLeaf(string name)
{
this.AddLeaf(name);
return this;
}

ISubMenuBuilder IMenuBuilder.AddBranch(string name)
{
this.AddBranch(name);
return this;
}

void IMenuBuilder.Build()
{
}
}

public class NestSubMenuBuilder : INestSubMenuBuilder
{
private ISubMenuBuilder subMenuBuilder;
private ISubMenuHolder branchMenu;

public NestSubMenuBuilder(ISubMenuBuilder subMenuBuilder, ISubMenuHolder branchMenu)
{
this.subMenuBuilder = subMenuBuilder;
this.branchMenu = branchMenu;
}

public INestSubMenuBuilder AddLeaf(string name)
{
var leafMenu = new LeafMenu { Name = name };
leafMenu.ParentId = (leafMenu as Menu).Id;
branchMenu.AddSubMenu(leafMenu);
return this;
}

public ISubMenuBuilder Build()
{
return subMenuBuilder;
}

public INestSubMenuBuilder AddBranch(string name)
{
var menu = new BranchMenu { Name = name };
menu.ParentId = (branchMenu as Menu).Id;
branchMenu.AddSubMenu(menu);
return new NestSubMenuBuilder(this, menu);
}

ISubMenuBuilder ISubMenuBuilder.AddLeaf(string name)
{
this.AddLeaf(name);
return this;
}

IMenuBuilder ISubMenuBuilder.Build()
{
return subMenuBuilder;
}

IMenuBuilder IMenuBuilder.AddLeaf(string name)
{
this.AddLeaf(name);
return this;
}

ISubMenuBuilder IMenuBuilder.AddBranch(string name)
{
this.AddBranch(name);
return this;
}

void IMenuBuilder.Build()
{
}
}

这是我的构建器代码,这是我在使用此构建器 API 时遇到问题的示例。

        builder
.AddLeaf("Leaf 4")
.AddBranch("Branch 2") // subMenuBuilder
.AddLeaf("Branch 2 -> Leaf 1")
.AddBranch("Branch 2 -> Branch 1") //nestSubMenuBuilder 1
.AddLeaf("Branch 2 -> Branch 1 -> Leaf 3")
.AddBranch("Branch 2 -> Branch 1 -> Branch 1") //nestSubMenuBuilder 2
.AddLeaf("Branch 2 -> Branch 1 -> Branch 1 -> Leaf 1")
.Build() // nestSubMenuBuilder 1
.AddLeaf("Branch 2 -> Leaf 4")
.Build() // subMenuBuilder
.AddLeaf("Branch 2 -> Leaf 2")
.Build();// menubuilder

这里我没有获取最后的菜单构建器引用以添加新的叶子和分支。

最佳答案

您的问题是,您的代码为每个菜单级别返回不同的构建器对象。相反,您应该始终返回相同类型的构建器,并且当达到顶层时,只需从 Build()

返回 null

建议的构建器代码:

public interface IMenuBuilder
{
IMenuBuilder AddLeaf(string name);
IMenuBuilder AddBranch(string name);
IMenuBuilder Build();
}

public class MenuBuilder : IMenuBuilder
{
private IMenuBuilder menuBuilder;
private ISubMenuHolder menu;

/// <summary>
/// Create a new menu with specified root item
/// </summary>
public MenuBuilder(string rootName)
{
this.menuBuilder = null;
this.menu = new RootMenu { Name = rootName };
}

/// <summary>
/// Create a new sub menu with specified parents
/// </summary>
public MenuBuilder(IMenuBuilder parentBuilder, ISubMenuHolder menu)
{
this.menuBuilder = parentBuilder;
this.menu = menu;
}

public IMenuBuilder AddLeaf(string name)
{
var leafMenu = new LeafMenu { Name = name };
leafMenu.ParentId = menu.Id;
menu.AddSubMenu(leafMenu);
return this;
}

public IMenuBuilder AddBranch(string name)
{
var branchMenu = new BranchMenu { Name = name };
branchMenu.ParentId = menu.Id;
menu.AddSubMenu(branchMenu);
return new MenuBuilder(this, branchMenu);
}

public IMenuBuilder Build()
{
return menuBuilder;
}
}

关于c# - 嵌套菜单生成器永远不会在 c# 中正确结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40459312/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com