简体   繁体   English

面向对象设计:哪种更好的做法?

[英]Object-Oriented Design: Which is better practice?

I have a confusion in different ways the objects are created which I have seen in java docs and other programming books. 我在Java文档和其他编程书籍中已经看到过以不同的方式创建对象的困惑。 For eg 例如

Assuming there is a base class and a derived class 假设有一个base class和一个derived class

1- I have a variable of type Base which refers to the object of type Derived 1-我有一个类型为Base的变量,它引用类型为Derived的对象

List<String> list = new ArrayList<String>();

2- I have a variable of type Derived which refers to the object of type Derived 2-我有类型的变量Derived是指类型的对象Derived

ArrayList<String> arrList = new ArrayList<String>();

My question is what should be my thinking while choosing between 1 and 2? 我的问题是,在1和2之间进行选择时应该怎么想? Is it to take advantage of Polymorphism in general Base-Derived scenarios? 是否可以在一般的基本派生方案中利用Polymorphism

Is there a better practice while choosing between 1 and 2 that I am not aware of or is it just a personal decision ? 在我不知道的1到2之间进行选择时,是否有better practice ,还是只是personal decision

EDIT: 编辑:

Sorry, List is an interface. 抱歉,列表是一个接口。 Another question : Will my decision change if I use a Type parameter? 另一个问题 :如果使用Type参数,我的决定会改变吗?

ArrayList<T> list = new ArrayList<T>();

Update Answer: This is actually called "Programming to the interface" . 更新答案:这实际上称为"Programming to the interface" Thanks Code-Guru. 感谢Code-Guru。 Exactly what I was looking for is explained in very simple terms in one of the answers to this question - What does it mean to "program to an interface"? 在这个问题的答案之一中,用非常简单的术语确切地解释了我要寻找的内容- “编程为接口”是什么意思?

  1. List is not a base class it is an interface and therefore should be used wherever possible. List不是基类,它是一个interface ,因此尽可能使用它。

    Using the interface that a class implements allows you to work with all classes that implement the interface, not just the specific class. 使用类实现的接口,您可以处理实现该接口的所有类,而不仅仅是特定的类。

  2. String is a concrete class so it is much clearer to use it directly. String是一个具体的类,因此直接使用它更加清晰。

    Sometimes, however, even though String implements the CharSequence interface it is unnecessary to use CharSequence because it would just be confusing. 但是,有时即使String实现了CharSequence接口,也不必使用CharSequence因为这会造成混淆。 However, take a look at StringBuilder , it uses CharSequence almost exclusively. 但是,看看StringBuilder ,它几乎完全使用CharSequence

In summary - there is no better there is just appropriate . 总结-没有更好的选择才是合适的

Choosing the base type allows you to at some point in the future change between using an ArrayList to say a LinkedList, without changing the rest of the code. 选择基本类型可让您在将来的某个时候使用ArrayList表示LinkedList之间进行更改,而无需更改其余代码。 This gives you more flexibility to refactor later on. 这样可以为以后的重构提供更大的灵活性。 Likewise, your public methods should return a List instead of the specific type of List implementation for the same reason -- so that you may change your internal implementation to optimize performance without breaking the contract to your callers. 同样,出于相同的原因,您的公共方法应返回List而不是List实现的特定类型-以便您可以更改内部实现以优化性能而不会破坏与调用方的合同。

In the case where List is a class ( just assume for the purpose of answering your question - "Shall we use object of Parent class representing child class or use object of type derived class ( actula class) ? " 在List是一个类的情况下(为回答您的问题而假设- “我们应该使用代表子类的Parent类的对象还是使用派生类(actula类)的对象?”

1) You were correct, its solely for the purpose of polymorphism. 1)您是正确的,仅出于多态性目的。

2) Mostly its used for Passing the object around different class methods. 2)通常用于将对象传递给不同的类方法。 Other classes' methods may be accepting parent class a input. 其他类的方法可能正在接受父类的输入。 you have to use the casting in those places. 您必须在那些地方使用铸件。

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

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