我可以称其为复合模式的实现吗?



我有这个设计,我自称是复合模式,虽然我不完全确定。所以我对你的声明表示赞赏。

这个接口集合了所有这些

public interface DomainResourceBuilder<T extends DomainResource> {
T buildInsertion(T persistence, EntityManager entityManager) throws Exception;
T buildUpdate(T model, T persistence, EntityManager entityManger);
<E extends T> DomainResourceBuilder<E> and(DomainResourceBuilder<E> next);
}

这个将DomainResourceBuilder描述为一对

public abstract class AbstractDomainResourceBuilder<D extends 
DomainResource> implements DomainResourceBuilder<D> {
@Override
public <E extends D> DomainResourceBuilder<E> 
and(DomainResourceBuilder<E> next) {
return new CompositeDomainResourceBuilder<>(this, next);
}
private class CompositeDomainResourceBuilder<T extends D> 
extends AbstractDomainResourceBuilder<T> {
private final DomainResourceBuilder<D> parentBuilder;
private final DomainResourceBuilder<T> childBuilder;
public 
CompositeDomainResourceBuilder(DomainResourceBuilder<D> 
parentBuilder,
DomainResourceBuilder<T> childBuilder) {
super();
this.parentBuilder = parentBuilder;
this.childBuilder = childBuilder;
}
@SuppressWarnings("unchecked")
@Override
public T buildInsertion(T model, EntityManager 
entityManager) throws Exception {
return childBuilder.buildInsertion((T) 
parentBuilder.buildInsertion(model, entityManager), 
entityManager);
}
@SuppressWarnings("unchecked")
@Override
public T buildUpdate(T model, T persistence, 
EntityManager entityManger) {
return childBuilder.buildUpdate(model, (T) 
parentBuilder.buildUpdate(model, persistence, 
entityManger), entityManger);
}
}
}

混凝土类起Leaf作用

public class CustomerBuilder extends AbstractDomainResourceBuilder<Customer> {
@Override
public
Customer buildInsertion(Customer persistence, EntityManager entityManager) throws Exception {
return persistence;
}
@Override
public
Customer buildUpdate(Customer model, Customer persistence, EntityManager entityManger) {
return persistence;
}
}

我可以称它为复合模式吗?

我是这样使用它的。假设我有以下层次结构:

AbstractEntity
|___User
|___Customer

现在我想在每个类上实现不同的逻辑,所以在设计中,我将为这些类创建特定的逻辑,然后最终将它们组合成一个,这仍然是它们的同类之一。像这样。

// group of objects
DomainResourceBuilder<AbstractEntity> abstractEntityBuilder = new AbstractEntityBuilder<>();
DomainResourceBuilder<User> userBuilder = new UserBuilder<>();
DomainResourceBuilder<Customer> customerBuilder = new CustomerBuilder<>();
// treat them as a whole (unify them all)
DomainResourceBuilder<Customer> compositeCustomerBuilder = 
abstractEntityBuilder
.and(userBuilder)
.and(customerBuilder);

我不认为它看起来像Composite模式:

  • 没有一个地方可以将一组对象作为同一类型对象的单个实例来处理。

在我看来,它看起来像是使用了Builder模式和泛型,可以处理实体的层次结构。

更新:

在我看来,它不是一组对象:

// group of objects
DomainResourceBuilder<AbstractEntity> abstractEntityBuilder = new AbstractEntityBuilder<>();
DomainResourceBuilder<User> userBuilder = new UserBuilder<>();
DomainResourceBuilder<Customer> customerBuilder = new CustomerBuilder<>();

如果上述对象将放在集合中,则可以将其称为对象组。在我看来,它只是对象变量。

在以下代码行中,可以看到Fluent接口设计模式可以被链接为方法:

// treat them as a whole (unify them all)
DomainResourceBuilder<Customer> compositeCustomerBuilder = 
abstractEntityBuilder
.and(userBuilder)
.and(customerBuilder);

让我展示一个例子,其中组对象可以被视为一个整体

以下文献有很好的例子和解释:

  • 这个美丽,非常酷和有趣的书由埃里克·弗里曼
  • 这篇关于复合模式的文章在wiki
  • 关于codemaze中复合模式的文章

假设您正在构建浏览器,并且希望显示控件。您的任务是显示放置在DOM浏览器中的所有控件的值。

示例代码是这样的:

我们需要一些控件的基类:

public abstract class ControlBase
{
protected string name;
protected string value;
public ControlBase(string name, string value)
{
this.name = name;
this.value = value;
}
public abstract string ShowValue();
}

及其操作:

public interface IControlOperations
{
void Add(ControlBase gift);
void Remove(ControlBase gift);
}
然后我们创建一个复合控件,其中我们可以有对象组:
public class CompositeControl : ControlBase, IControlOperations
{
// group of objects
private List<ControlBase> _controls;
public CompositeControl(string name, string value)
: base(name, value)
{
_controls = new List<ControlBase>();
}
public void Add(ControlBase gift)
{
_controls.Add(gift);
}
public void Remove(ControlBase gift)
{
_controls.Remove(gift);
}
// group of objects can be treat as a whole
public override string ShowValue()
{
StringBuilder allValues = new StringBuilder();
Console.WriteLine($"{name} has the following values:");
foreach (var gift in _controls)
{
allValues.AppendLine(gift.ShowValue());
}
return allValues.ToString();
}
}

和我们的UI控件:

public class TextBox : ControlBase
{
public TextBox(string name, string value)
: base(name, value)
{
}
public override string ShowValue()
{
Console.WriteLine($"{name} has {value}");
return value;
}
}
public class CheckBox : ControlBase
{
public CheckBox(string name, string value)
: base(name, value)
{
}
public override string ShowValue()
{
Console.WriteLine($"{name} with value {value}");
return value;
}
}   

然后我们可以这样调用代码:

var textBox_Phone = new TextBox("textBox_Phone", "1");
textBox_Phone.ShowValue();
Console.WriteLine();
//composite control
var divContainer = new CompositeControl("divContainer", string.Empty);
var textBox_Name = new TextBox("textBox_Name", "Joseph");
var textBox_Surname = new TextBox("textBox_Surname", "Albahari");
divContainer.Add(textBox_Name);
divContainer.Add(textBox_Surname);
Console.WriteLine($"Total values of this composite control " +
$"is: {divContainer.ShowValue()}");

相关内容

  • 没有找到相关文章

最新更新