- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有实体:
@Entity
@Table(name = "CARDS")
public class Card {
@ManyToOne
@JoinColumn(name = "PERSON_ID", referencedColumnName = "ID", nullable = false)
private Person person;
@OneToMany(mappedBy = "card")
private List<CardStatus> cardStatuses;
我的服务可以调用下一个代码:
public Card getCardBlaBlaBla(Long id){
Card card = cardRepository.findaCard(id);
Something something = card.getCardStatuses().get(0).getSomething();
}
card.getCardStatuses().get(0).getSomething() - getCardStatuses
是 LAZY
我的问题 - 此挑战是否违反 Dimetra 法律?
The Law of Demeter There is a well-known heuristic called the Law of Demeter2 that says a module should not know about the innards of the objects it manipulates. As we saw in the last section, objects hide their data and expose operations. This means that an object should not expose its internal structure through accessors because to do so is to expose, rather than to hide, its internal structure. More precisely, the Law of Demeter says that a method f of a class C should only call the methods of these:
• C
• An object created by f
• An object passed as an argument to f
• An object held in an instance variable of CThe method should not invoke methods on objects that are returned by any of the allowed functions. In other words, talk to friends, not to strangers. The following code appears to violate the Law of Demeter (among other things) because it calls the getScratchDir() function on the return value of getOptions() and then calls getAbsolutePath() on the return value of getScratchDir(). final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();
Card
- 是数据结构,不违反 Dimetra 法则,但 LAZY
方法具有 logik(选择数据库)。
此挑战是否违反 Dimetra 法律?如果是的话我该如何正确使用它?
我确实有很多代码,例如:
entityOblect.getChield().getChield().getSomething();
编辑
摘自《Clean Code》书:
编辑2
(c) Robert C. Martin - 干净的代码
Active Record Active Records are special forms of DTOs. They are data structures with public (or beanaccessed) variables; but they typically have navigational methods like save and find. Typically these Active Records are direct translations from database tables, or other data sources. Unfortunately we often find that developers try to treat these data structures as though they were objects by putting business rule methods in them. This is awkward because it creates a hybrid between a data structure and an object. The solution, of course, is to treat the Active Record as a data structure and to create separate objects that contain the business rules and that hide their internal data (which are probably just instances of the Active Record).
最佳答案
这违反了德墨忒耳定律:
Something something = card.getCardStatuses().get(0).getSomething();
在一个语句中,您可以浏览所有这些对象:
Card
-> List<CardStatus>
-> CardStatus
-> Something
。
理想情况下,您应该从客户端进行此通信:
Card
-> Something
.
在Card
中类,Something
return 可以通过执行此导航的方法来实现:
List<CardStatus>
-> CardStatus
-> Something
。
在某种程度上,最后一个也违反了德米特定律,但在我们可以认为可以接受的水平上,因为我们不应该考虑这种关系 List<CardStatus>
-> CardStatus
作为陌生人关系。
它可以给:
Something something = card.getSomethingOfCardStatuses(0);
或者避免使用 get 前缀:
Something something = card.findSomethingOfCardStatuses(0);
和findSomethingOfCardStatuses()
可以在 Card
中定义如:
public Something findSomethingOfCardStatuses(int statusNumber){
// add some check for the index if required
// you could return Optional or an Exception according to your requirements
return cardStatuses.get(0).getSomething();
}
我知道许多开发人员喜欢在实体字段中手动挖掘,但我个人避免这样做,并且尝试为他们提供一些具有有意义的名称和行为的逻辑方法。它使核心更加清晰。
关于java - 实体惰性方法和 Dimetra 法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51439148/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
我有实体: @Entity @Table(name = "CARDS") public class Card { @ManyToOne @JoinColumn(name = "PERSON_I
我正在尝试计算二维多边形的表面法线。我正在使用 OpenGL wiki 中的 Newell 方法来计算表面法线。 https://www.opengl.org/wiki/Calculating_a_S
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 7 年前。 Improve
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我这里有以下 XML: Visa, Mastercard, , , , 0, Discover, American Express siteonly, Buyer Pay
即将发生的 Google 政策变更迫使我们实现一个对话框,以通知欧盟用户有关 Cookie/设备标识符用于广告和分析的情况。我只想向欧盟用户显示此对话框。我不想使用额外的权限(例如 android.p
本文分享自华为云社区《华为大咖说 | 企业应用AI大模型的“道、法、术” ——道:认知篇》,作者:华为云PaaS服务小智。 本期核心观点 上车:AGI是未来5~10年内,每个人都无法回避的技
我有一个与酒精相关的网站,需要先验证年龄,然后才能让他们进入该网站。我使用 HttpModule 来执行此操作,该模块检查 cookie,如果未设置,我会将它们重定向到验证页面。我验证他们的年龄并存储
在欧盟,我们有一项法律,要求网页请求存储 cookie 的许可。我们大多数人都了解 cookie 并同意它们,但仍然被迫在任何地方明确接受它们。所以我计划编写这个附加组件(ff & chrome),它
以下在 C 和/或 C++ 中是否合法? void fn(); inline void fn() { /*Do something here*/ } 让我担心的是,第一个声明看起来暗示函数将被定义
我是一名优秀的程序员,十分优秀!