- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
以下代码打破了 Law of Demeter :
public class Student extends Person {
private Grades grades;
public Student() {
}
/** Must never return null; throw an appropriately named exception, instead. */
private synchronized Grades getGrades() throws GradesException {
if( this.grades == null ) {
this.grades = createGrades();
}
return this.grades;
}
/** Create a new instance of grades for this student. */
protected Grades createGrades() throws GradesException {
// Reads the grades from the database, if needed.
//
return new Grades();
}
/** Answers if this student was graded by a teacher with the given name. */
public boolean isTeacher( int year, String name ) throws GradesException, TeacherException {
// The method only knows about Teacher instances.
//
return getTeacher( year ).nameEquals( name );
}
private Grades getGradesForYear( int year ) throws GradesException {
// The method only knows about Grades instances.
//
return getGrades().getForYear( year );
}
private Teacher getTeacher( int year ) throws GradesException, TeacherException {
// This method knows about Grades and Teacher instances. A mistake?
//
return getGradesForYear( year ).getTeacher();
}
}
public class Teacher extends Person {
public Teacher() {
}
/**
* This method will take into consideration first name,
* last name, middle initial, case sensitivity, and
* eventually it could answer true to wild cards and
* regular expressions.
*/
public boolean nameEquals( String name ) {
return getName().equalsIgnoreCase( name );
}
/** Never returns null. */
private synchronized String getName() {
if( this.name == null ) {
this.name == "";
}
return this.name;
}
}
问题
最佳答案
我认为这里有两个问题:
Grades
逻辑与 Student
混合太多。它应该在 Grades
类中完成Teacher
的逻辑放在Student
中。结论:Student对Teacher和Grades的内部结构和逻辑了解太多,破坏了LoD
关于java - 这段代码如何违反 Demeter 法则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2609236/
在 SO解释了为什么像 scalaz、cats (Scala) 或 Arrow (Kotlin) 中的 Validation 不能是 monad。 据我所知,这是因为他们已经根据应用仿函数对 mona
给定例如像 这样的类型 data Tree a = Branch (Tree a) (Tree a) | Leaf a 我可以轻松地为 Functor、Applicative、
从这里引用:https://en.wikipedia.org/wiki/Law_of_Demeter More formally, the Law of Demeter for functions r
以下代码打破了 Law of Demeter : public class Student extends Person { private Grades grades; public Stu
我有一个简单的 Store 类,其中包含一个 Inventory。 Inventory 包含一个 Item 列表。为了修改 Inventory 中的其中一个 Item,我必须这样写: Store st
public class BigPerformance { public decimal Value { get; set; } } public class Performance
当我需要多态行为时,我经常发现自己在 C++ 中使用唯一指针。我通常实现如下所示的纯抽象类: class A { public: virtual A* clone() const = 0; /
5 规则指出,如果一个类有一个用户声明的析构函数、复制构造函数、复制赋值构造函数、移动构造函数或移动赋值构造函数,那么它必须有其他 4 个。 但今天我突然明白了:你什么时候需要用户定义的析构函数、复制
编译器或库的更“ native ”部分(IO 或可以访问黑魔法和实现的函数)是否对这些定律做出假设?打破它们会导致不可能的事情发生吗? 或者它们只是表达了一种编程模式——也就是说,你唯一会因为破坏它们
我有点想用 Java 8 流编写 Selenium 页面对象,如下面的代码所述,并收到评论说我的代码违反了 Demeter 法则,因为我在一行中执行了很多操作。我被建议将代码分解为第一个流以收集列表并
我对如何避免一对多关联违反得墨忒耳法则感到困惑。假设我有一个这样的模型: class Organization < ActiveRecord::Base has_one :address ha
如果我有一个对象的 ArrayList,那么任何时候我需要调用 ArrayList 成员的任何方法时,我都需要这样做: list.get(i).doSomething(); 这看起来很可疑地违反了 D
我在我的 Purescript 代码中广泛使用了来自库和我自己的类型类。每个类型类法则似乎都提供了一个很好的测试。目前,我正在为每个类(class)和法律单独编写测试。有没有办法部分自动化?也许像 H
我看到提到过 ListT is a classic example of a buggy monad transformer that doesn't satisfy the monad laws.
当我审查一些代码时,我看到了这个片段。 List users = /* Some code that initializes the list */; users.stream() .fil
我是一名优秀的程序员,十分优秀!