简体   繁体   English

与设计模式的“抽象”概念混淆

[英]Confusion with design patterns “abstraction” concept

I was learning about design patterns and I came accross this PDF 我正在学习设计模式,并且偶然发现了这个PDF

On Slide 12, its says the following 3 rules: 在幻灯片12中,它说出以下3条规则:

  1. No variable should hold a reference to a concrete class 任何变量都不应包含对具体类的引用
  2. No class should derive from a concrete class 任何阶级都不应来自具体的阶级
  3. No method should override an implemented method of its base classes 任何方法都不应覆盖其基类的已实现方法

Now Suppose I have an abstract Class say "A". 现在,假设我有一个抽象类,说“ A”。 Then, I have 3 concrete classes that should implement this abstract class, they are classes "B","C","D". 然后,我有3个具体的类应实现此抽象类,它们是“ B”,“ C”,“ D”类。 The issue is that D is a special case of C. D has only one instance variable more. 问题在于D是C的特例。D仅具有一个以上的实例变量。

My question are, 我的问题是

  • First, are the 3 rules above golden rules that guarantees a good design 首先,保证最佳设计的3条以上黄金法则
  • Second, What problem might happen if "D" extends "C" and overrides methods of "C" to add more behaviour 其次,如果“ D”扩展“ C”并覆盖“ C”的方法以添加更多行为,可能会发生什么问题

If you start adding subtypes down the graph, then the abstraction becomes weakened of course. 如果您开始在图下添加子类型,那么抽象当然会变弱。 Why? 为什么? Because people have to learn more, and the footprint of what is required expands. 因为人们必须学习更多,所以所需内容的范围也扩大了。 That said, there could be valid cases, though I am straining to see them in a factory situation. 话虽如此,可能会有有效的案例,尽管我很努力地在工厂中看到它们。 One possibility, to answer your question directly for that dowstream case would be to use Decorator, because it more or less preserves the abstraction but layers in some functionality that was not provided for. 直接针对该dowstream案例回答您的问题的一种可能性是使用Decorator,因为它或多或少保留了抽象,但是某些功能未提供分层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM