抽象隐藏实现的是谁?

huangapple go评论83阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月22日 13:56:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63533026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定