如何消除对Java Bean的严格依赖 [英] How to eliminate hard dependecies on Java Beans

查看:95
本文介绍了如何消除对Java Bean的严格依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对DIP原则有疑问。其中一项准则规定,我们不应保留对具体类的引用(如果更改,则我将必须修改所有使用该类的客户端)。因此,当我使用POJO时可以遵循什么准则?例如:



我有一个带有某些属性的Bean'Foo'(它可以表示一个Domain对象)

  class Foo {
private字符串一;
私有字符串二;

//获取器和设置器
}

多个客户实例化例如,将此对象保留在数据库中

 类Client1 {

私有FooDao dao ;

Client1(FooDao dao){
this.dao = dao;
}

public voidpersist(){
//硬编码
Foo foo = new Foo();
foo.setOne( something ...);
dao.save(foo); }
}

类Client2 {

私人FooDao岛;

Client2(FooDao dao){
this.dao = dao;

}}

public voidpersist(){
Foo foo = new Foo();
foo.setOne( something ...);
foo.setTwo( something ...)
dao.save(foo);
}
}

如果我将任何属性添加或更改为 Foo每个客户都必须更改班级,因此请遵循此指南,如何避免这种情况?



谢谢!

解决方案

@chrylis的评论很明确。罗伯特·马丁(Robert Martin)在清洁代码:对象和数据结构的第6章中对此进行了介绍。


对象将其数据隐藏在抽象之后并公开可在其上运行的函数该数据。数据结构公开其数据,并且没有有意义的功能。 (第95页)


OOP的定义是天真的,这里的一切都是对象,没有数据结构。


成熟的程序员知道,一切都是对象的想法是一个神话。有时候,您确实确实想要简单的数据结构以及对其进行操作的过程。 (第97页)


那么暴露数据和行为的类又如何呢?


混淆有时会导致不幸的混合结构,即一半对象和一半数据结构。它们具有执行重要功能的函数,也具有公共变量或公共访问器和更改器,它们出于所有意图和目的,将私有变量公开,并诱使其他外部函数以程序程序将其用作对象的方式使用这些变量。数据结构。
这样的混合体很难添加新功能,但是也很难添加新的数据结构。他们是两全其美的。避免创建它们。 (第99页)


对于原始问题:依赖倒置原则适用于对象,而不适用于Java Beans等数据结构。 p>

I've a question about DIP Principle. One of the guidelines says that we should not hold references to a concrete class (if it changes then I'll have to modify all clients that use it). So, what can I follow this guideline when I use POJOs ? For Example:

I have a Bean 'Foo' with some attributes (it could represent a Domain object)

class Foo {  
   private String one;   
   private String two;

  //getters and setters
}

Multiple clients instantiate this object, for example, to persist it in the Database

   class Client1 {   

      private FooDao dao;

      Client1(FooDao dao){     
         this.dao = dao;
      }  

      public void persist() {    
         //hard coding
         Foo foo = new Foo();   
         foo.setOne("something...");    
         dao.save(foo); }     
       } 

       class Client2 {   

         private FooDao dao;

          Client2(FooDao dao){   
            this.dao = dao;

           }  

        public void persist() {   
           Foo foo = new Foo();   
           foo.setOne("something...");   
           foo.setTwo("something...")    
           dao.save(foo); 
         }
       }

If I add or change any attribute to 'Foo' class every client would have to change, so follow this guideline how can I avoid that?

Thanks!

解决方案

The comment from @chrylis is spot on. Robert Martin covers this in chapter 6 of Clean Code: Objects and Data Structures.

Objects hide their data behind abstractions and expose functions that operate on that data. Data structures expose their data and have no meaningful functions. (page 95)

The definition of OOP where everything is an object, and there are no data structures, is naive.

Mature programmers know that the idea that everything is an object is a myth. Sometimes you really do want simple data structures with procedures operating on them. (page 97)

So what about classes that expose both data and behavior?

Confusion sometimes leads to unfortunate hybrid structures that are half object and half data structure. They have functions that do significant things, and they also have either public variables or public accessors and mutators that, for all intents and purposes, make the private variables public, tempting other external functions to use those variables the way a procedural program would use a data structure. Such hybrids make it hard to add new functions but also make it hard to add new data structures. They are the worst of both worlds. Avoid creating them. (page 99)

To the original question: the Dependency Inversion Principle applies to objects, not to data structures like Java Beans.

这篇关于如何消除对Java Bean的严格依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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