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

发布于 2025-02-12 21:25:18 字数 3232 浏览 1 评论 0 原文

我有自己自称为复合模式的设计,尽管我不完全确定。因此,我正在为您提供有关此的声明。

这是一个界面,共同描述了所有这些界面,

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> 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);

I have this design which I self-proclaim to be Composite Pattern, though I'm not entirely sure about that. So I'm aksing for your statement on this.

This is the interface which collectively describes all of them

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);

}

This one describes DomainResourceBuilder as a pair

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);
        }
    }
}

Concrete class plays the Leaf role

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;
    }

}

Can I call this a Composite Pattern?

This is how I use it. Assumming I have the following hierarchy.

AbstractEntity
|___User
    |___Customer

Now I want to implement different logics on each class, so with the design I'll create specific logic for those class and then ultimately compose them into one, which is still one of their kind. Something like this.

// 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2025-02-19 21:25:18

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

在我看来,看起来它使用 builder> builder 与可以处理实体结构的通用物质。

更新:

在我的视图中,它不是对象组:

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

如果将上述对象放入集合中,则可以将其称为一组对象。以我的荣誉意见,这只是对象变量。

在以下几行中, fluent interface 可以将设计模式视为可以被视为可以链接的:

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

让我展示一个示例,其中可以将对象组作为一个整体对待。

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

想象您正在构建浏览器您想显示控件。您的任务是显示在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()}");

I do not think that it looks like Composite pattern as:

In my view, it looks like it uses Builder pattern with generics that can treat hierarchy of entities.

UPDATE:

In my view it is not group of objects:

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

If the above objects will be put in collection, then it can be called as group of objects. In my honour opinion, it is just object variables.

In the following lines of code, Fluent interface design pattern can be seen as methods can be chained:

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

Let me show an example where group of objects can be treat as a whole.

The following literature has great examples and explanations:

Imagine you are building browser and you want to show controls. Your task is to show values of all of your controls placed in DOM browser.

So example code would look like this.

We need some base class for controls:

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();
}

and its operations:

public interface IControlOperations
{
    void Add(ControlBase gift);
    void Remove(ControlBase gift);
}

Then we create a composite control where we can have group of objects:

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(
quot;{name} has the following values:");

        foreach (var gift in _controls)
        {
            allValues.AppendLine(gift.ShowValue());
        }

        return allValues.ToString();
    }
}

And our UI controls:

public class TextBox : ControlBase
{
    public TextBox(string name, string value)
        : base(name, value)
    {
    }

    public override string ShowValue()
    {
        Console.WriteLine(
quot;{name} has {value}");

        return value;
    }
}

public class CheckBox : ControlBase
{
    public CheckBox(string name, string value)
        : base(name, value)
    {
    }

    public override string ShowValue()
    {
        Console.WriteLine(
quot;{name} with value {value}");

        return value;
    }
}   

And then we can call code like this:

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(
quot;Total values of this composite control " +
    
quot;is: {divContainer.ShowValue()}");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文