应该如何修改这个类才能遵循 DIP(依赖注入原则)?
如何修改此类以遵循 DIP(依赖倒置原则),以便删除构造函数中的两个 ArrayList 依赖项?接口应该如何?
让我困惑的一件事是,新引用指向 ArrayList
package mvc.controllers;
import java.util.ArrayList;
import mvc.models.AbstractModel;
import mvc.views.AbstractViewPanel;
public abstract class AbstractController {
private ArrayList<AbstractViewPanel> registeredViews;
private ArrayList<AbstractModel> registeredModels;
public AbstractController() {
registeredViews = new ArrayList<AbstractViewPanel>();
registeredModels = new ArrayList<AbstractModel>();
}
public void addModel(AbstractModel model) {
registeredModels.add(model);
model.addPropertyChangeListener(this);
}
public void removeModel(AbstractModel model) {
registeredModels.remove(model);
model.removePropertyChangeListener(this);
}
public void addView(AbstractViewPanel view) {
registeredViews.add(view);
}
public void removeView(AbstractViewPanel view) {
registeredViews.remove(view);
}
...
}
How I can modify this class to follow DIP (Dependency Inversion Principle) in order to remove the two ArrayList dependencies in the constructor? How should the interfaces be?
One thing that confuses me is that the new references points to an ArrayList<type>
not just the constructor of a class. And I don't know how to handle that situation...
package mvc.controllers;
import java.util.ArrayList;
import mvc.models.AbstractModel;
import mvc.views.AbstractViewPanel;
public abstract class AbstractController {
private ArrayList<AbstractViewPanel> registeredViews;
private ArrayList<AbstractModel> registeredModels;
public AbstractController() {
registeredViews = new ArrayList<AbstractViewPanel>();
registeredModels = new ArrayList<AbstractModel>();
}
public void addModel(AbstractModel model) {
registeredModels.add(model);
model.addPropertyChangeListener(this);
}
public void removeModel(AbstractModel model) {
registeredModels.remove(model);
model.removePropertyChangeListener(this);
}
public void addView(AbstractViewPanel view) {
registeredViews.add(view);
}
public void removeView(AbstractViewPanel view) {
registeredViews.remove(view);
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要以依赖关系反转方式执行此操作,您可以执行以下操作之一:
在构造函数中给出对列表的依赖关系:
为列表添加修改器(setter):
顺便说一句,我将 ArrayList 更改为 List。无需引入对 List 实现的依赖。
To do it in Dependency Inversion -way, you could do one of the following:
Dependencies to the lists are given in the constructor:
Add mutators (setters) for the lists:
Btw, I changed the ArrayLists to Lists. There's no need to introduce a dependency on the List implementation.
目前尚不清楚您要删除什么依赖项,但如果您想删除显式 ArrayList 实例化,您可以使用构造函数注入:
It's not really clear what dependency you're trying to remove, but if you want to remove the explicit ArrayList instantiation, you could use constructor injection:
我不会将列表注入到对象中,从而破坏封装,只是为了通过模拟列表来对其进行单元测试。该列表不是外部依赖项。它是班级内部的一部分。
如果您想对此类进行单元测试,请测试使用列表中存储的对象的方法是否确实按应有的方式使用它们。您还应该测试当您更改已添加到控制器的模型的属性时是否调用 propertyChange 方法。
或者,您可以添加
getView()
和getModels()
方法(可能受保护),以测试添加是否按预期工作。I wouldn't inject a List into an object, and thus break encapsulation, just for the sake of unit testing it by mocking the list. The List is not an external dependency. It's part of the internals of the class.
If you want to unit-test this class, test that the method who uses the objects stored in the list do actually use them as they should. You should also test that the propertyChange method is called when you change a property of a model that you've added to the controller.
Or you could add a
getView()
and agetModels()
method (potentially protected), to test that the addition worked as expected.