英文:
From whom abstraction hides implementation?
问题
我了解这个架构,我们如何使用接口和抽象类来实现抽象化。
但是我们要把实现隐藏在什么地方呢?
作为开发者,任何人都可以点击那个方法,然后看到为那些抽象方法提供实现的类。
作为用户,他只会面对用户界面,因此无论如何他都不会看到代码。
那么,我的问题是,我们要把实现隐藏在什么地方呢?
英文:
I know the architecture, how we can implement abstraction using the interface and an abstract class.
But from whom we are hiding the implementation?
As a developer, anyone can click on that method and can see the classes which are giving implementations for those abstract methods.
As a user, he will be only facing UI, so he is not going to see the code anyway.
Then my question from whom we are hiding the implementation?
答案1
得分: 2
我们将其对接口的客户端隐藏,以便他们不依赖于可能会改变的内容。接口的客户端是调用接口的一段代码。
假设我编写了一个栈接口:
interface Stack {
push(String s);
String pop();
}
还有一个实现:
class StackImpl {
public List contents;
public void push(String s) {
...
}
...
}
如果客户端使用 StackImpl,他们可能会直接访问 contents
,这意味着我无法更改实现以使用数组,否则将破坏所有客户端。
如果他们只使用 Stack
,我们可以在不影响客户端的情况下改进实现。
英文:
We are hiding it from clients of the interface, so that they don't depend on things which might change. A client of an interface is a piece of code which calls the interface.
Let's say I write a stack interface:
interface Stack {
push(String s);
String pop();
}
and an implementation:
class StackImpl {
public List contents;
public void push(String s) {
...
}
...
}
If clients use StackImpl, they might start accessing the contents
directly, which will mean that I can't change the implementation to use an array without breaking all the clients.
If they only use Stack
, we can improve the implementation without affecting the clients.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论