我应该扩展ArrayList中添加属性不为null? [英] Should I extend ArrayList to add attributes that isn't null?

查看:113
本文介绍了我应该扩展ArrayList中添加属性不为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对象的集合添加到一个ArrayList中,只有在特定的属性不为null。

I would like to add a collection of objects to an arrayList ,only if the particular attribute is not null.

我想延长ArrayList和落实儿童类中的检查。

I am thinking of extending the ArrayList and implementing the check inside the child class.

一另一种方法是把它在一个ArrayList中之前检查的属性,但是这意味着,我将不得不驱散,如果检查每一个地方,如果我需要的对象添加到基于逻辑的数组列表。

One alternate way is to check for the the attribute before putting it in a Arraylist, but that would mean , i will have to scatter the if checks every where if i need to add the objects to the arraylist based on the logic.

我想知道你的就可以了的想法...再一想它是一个矫枉过正?

I would like to know your thoughts on it ... on a second thought is it a overkill ?

推荐答案

我真的建议包装的ArrayList 使用详细记录的装饰格局。您只需换你的ArrayList 另一个列表的实现,代表大多数的方法,但增加了验证逻辑:

Decorator pattern

I would actually recommend wrapping ArrayList using well-documented Decorator pattern. You simply wrap your ArrayList with another List implementation that delegates most of the methods but adds validation logic:

public class ValidatingListDecorator extends AbstractList<MyBusinessObject>
{

    private final List<MyBusinessObject> target;

    public ValidatingListDecorator(List<MyBusinessObject> target) {
        this.target = target;
    }

    @Override
    public MyBusinessObject set(int index, MyBusinessObject element)
    {
        validate(element);
        return target.set(index, element);
    }

    @Override
    public boolean add(MyBusinessObject o)
    {
        validate(o);
        return target.add(o);
    }

    //few more to implement

}

优点:


  • 您仍可以访问原始列表未经验证,如果你想要的(但你可以限制此)

  • 更易于堆叠不同的验证,打开和关闭可选择开启他们。

  • 在继承组成通过的 @helios
  • 提高可测试性

  • 不配合你到一个特定的列表实现,可以添加验证的LinkedList 休眠 -backed持续名单。你甚至可以考虑一下通用收藏装饰来验证任何集合。

  • Advantages:

    • You can still access raw list without validation if you want (but you can restrict this)
    • Easier to stack different validations, turn them on and off selectively.
    • Promotes composition over inheritance as noted by @helios
    • Improves testability
    • Does not tie you to a specific List implementation, you can add validation to LinkedList or Hibernate-backed persistent lists. You can even think about generic Collection decorator to validate any collection.
    • 尽管实施记得有相当多的方法,你必须要记住,同时覆盖:添加()的addAll()设置()子列表()(?)等。

      Despite the implementation remember there are quite a lot of methods you have to remember about while overriding: add(), addAll(), set(), subList() (?), etc.

      此外,您的对象必须是不可变的,否则用户可以添加/设置有效的对象并对其进行修改后违反合同。

      Also your object must be immutable, otherwise the user can add/set valid object and modify it afterwards to violate the contract.

      Finaly我写的:

      Finaly I wrote:

      validate(element)
      

      但考虑:

      element.validate()
      

      这是一个更好的设计。

      which is a better design.

      正如前面提到的,如果你想堆栈验证,在一个单一的,独立的类验证每个proprty / apsect,考虑下面的语句:

      As noted before if you want to stack validations, validating each proprty/apsect in a single, separate class, consider the following idiom:

      public abstract class ValidatingListDecorator extends AbstractList<MyBusinessObject>
      {
      
          private final List<MyBusinessObject> target;
      
          public ValidatingListDecorator(List<MyBusinessObject> target) {
              this.target = target;
          }
      
          @Override
          public MyBusinessObject set(int index, MyBusinessObject element)
          {
              validate(element);
              return target.set(index, element);
          }
      
          protected abstract void validate(MyBusinessObject element);
      
      }
      

      ...很少实现:

      ...and few implementations:

      class FooValidatingDecorator extends ValidatingListDecorator {
      
          public FooValidatingDecorator(List<MyBusinessObject> target)
          {
              super(target);
          }
      
          @Override
          protected void validate(MyBusinessObject element)
          {
              //throw if "foo" not met
          }
      }
      
      class BarValidatingDecorator extends ValidatingListDecorator {
      
          public BarValidatingDecorator(List<MyBusinessObject> target)
          {
              super(target);
          }
      
          @Override
          protected void validate(MyBusinessObject element)
          {
              //throw if "bar" not met
          }
      }
      

      要只验证的的?

      List<MyBusinessObject> list = new FooValidatingDecorator(rawArrayList);
      

      要验证这两个的和的的?

      List<MyBusinessObject> list = 
        new BarValidatingDecorator(new FooValidatingDecorator(rawArrayList));
      

      这篇关于我应该扩展ArrayList中添加属性不为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆