Car*_*zer 25 oop class data-structures
在面向对象的编程中,自定义类(如具有Name数据的Person类,地址列表等)保存数据,也可以包含集合对象.数据结构也用于保存数据.那么,概念上是一个被认为是高级数据结构的类吗?在高效系统的设计中(在面向对象的世界和大型系统中),被认为类似于数据结构的类和为高效类设计进行的算法分析以提高效率(在google,facebook等公司)?
inf*_*rno 23
我建议你阅读清洁代码第6章:对象和数据结构.整章都是关于这个......如果你不想买这本书,你可以阅读摘要,可以在这里找到.
据此,您可以通过两种不同的方式有效地使用类.这种现象称为数据/对象反对称.根据您的目标,您必须决定您的课程是否遵循开放/封闭原则.
如果他们遵循OCP,他们将是多态的,他们的实例将被用作对象.因此,它们将隐藏数据和公共接口的实现,并且很容易添加实现该接口的新类型.大多数设计模式都符合OCP,例如MVC,IoC,每个包装器,适配器等......
如果它们不遵循OCP,它们将不是多态的,它们的实例将被用作数据结构.因此,他们将公开数据,并且该数据将由其他类操纵.这也是程序编程的典型方法.有几个例子不使用OCP,例如DTO,例外,配置对象,访客模式等......
当您考虑实现OCP并将代码移动到较低的抽象级别时的典型模式:
class Manipulator {
doSomething(Object dataStructure){
if (dataStructure instanceof MyType1){
// doSomething implementation 1
}
else if (dataStructure instanceof MyType2)
{
// doSomething implementation 2
}
// ...
},
domSomethingElse(Object dataStructure){
if (dataStructure instanceof MyType1){
// domSomethingElse implementation 1
}
else if (dataStructure instanceof MyType2)
{
// domSomethingElse implementation 2
}
// ...
}
}
class MyType1 {}
class MyType2 {}
//if you want to add a new type, every method of the Manipulator will change
Run Code Online (Sandbox Code Playgroud)
修复:将实现移至较低的抽象级别并实现OCP
interface MyType {
doSomething();
domSomethingElse();
}
class MyType1 implements MyType {
doSomething(){
// doSomething implementation 1
},
domSomethingElse(){
// domSomethingElse implementation 1
}
}
class MyType2 implements MyType {
doSomething(){
// doSomething implementation 2
},
domSomethingElse(){
// domSomethingElse implementation 2
}
}
// the recently added new type
class MyType3 implements MyType {
doSomething(){
// doSomething implementation 3
},
domSomethingElse(){
// domSomethingElse implementation 3
}
}
Run Code Online (Sandbox Code Playgroud)
当您考虑违反OCP并将代码移动到更高的抽象级别时的典型模式:
interface MyType {
doSomething();
domSomethingElse();
//if you want to add a new method here, every class which implements this interface, will be modified
}
class MyType1 implements MyType {
doSomething(){
// doSomething implementation 1
},
domSomethingElse(){
// domSomethingElse implementation 1
}
}
class MyType2 implements MyType {
doSomething(){
// doSomething implementation 2
},
domSomethingElse(){
// domSomethingElse implementation 2
}
}
Run Code Online (Sandbox Code Playgroud)
要么
interface MyType {
doSomething();
domSomethingElse();
}
class MyType1 implements MyType {
doSomething(){
// doSomething implementation 1
},
domSomethingElse(){
// domSomethingElse implementation 1
}
}
class MyType2 implements MyType {
doSomething(){
// doSomething implementation 2
},
domSomethingElse(){
// domSomethingElse implementation 2
}
}
//adding a new type by which one or more of the methods are meaningless
class MyType3 implements MyType {
doSomething(){
throw new Exception("Not implemented, because it does not make any sense.");
},
domSomethingElse(){
// domSomethingElse implementation 3
}
}
Run Code Online (Sandbox Code Playgroud)
修复:将实现移至更高的抽象级别并违反OCP
class Manipulator {
doSomething(Object dataStructure){
if (dataStructure instanceof MyType1){
// doSomething implementation 1
}
else if (dataStructure instanceof MyType2)
{
// doSomething implementation 2
}
// ...
},
domSomethingElse(Object dataStructure){
if (dataStructure instanceof MyType1){
// domSomethingElse implementation 1
}
else if (dataStructure instanceof MyType2)
{
// domSomethingElse implementation 2
}
// ...
},
// the recently added new method
doAnotherThing(Object dataStructure){
if (dataStructure instanceof MyType1){
// doAnotherThing implementation 1
}
else if (dataStructure instanceof MyType2)
{
// doAnotherThing implementation 2
}
// ...
}
}
class MyType1 {}
class MyType2 {}
Run Code Online (Sandbox Code Playgroud)
或者将类拆分为子类.
人们通常在方法计数一或二之后遵循OCP,因为重复相同的if-else语句不够干.
我不建议你使用部分完成,部分违反OCP的混合类,因为这样代码将很难维护.您应该根据您遵循的方法决定每种情况.这应该是一个简单的决定,但是如果你犯了一个错误,你仍然可以在以后重构你的代码......
自定义类是否是数据结构取决于您询问的对象.至少,肯定的人会承认,而不是用户定义的数据结构,这种数据结构比数据结构(例如数组,链表或二进制树)更具域特性且不太成熟.对于这个答案,我认为它们截然不同.
虽然很容易将Big O算法分析应用于数据结构,但它对于类来说有点复杂,因为它们包含了许多这些结构,以及其他类的其他实例......但是对类实例的大量操作可以被打破在数据结构上进行基本操作并用Big O表示.作为程序员,您可以通过避免不必要的成员复制并确保方法调用不会经历太多层来努力使您的类更有效.当然,在您的方法中使用高性能算法不言而喻,但这不是特定于OOP的.但是,除非必要,否则不应牺牲功能,设计和清晰度以支持性能.而过早的优化是魔鬼yada yada yada.
我确信某些学者在某个地方试图制定衡量课堂表现的指标,甚至是课堂及其操作的微积分,但我还没有想到.然而,存在QA研究像这样衡量一个项目中的类之间的依赖关系......人可能认为,有依赖关系的数量和方法调用(因此较低级的性能)的分层之间的相关性.但如果有人研究过这个问题,我相信你可以找到一个更加相关的指标,不需要进行彻底的推论.
我会说概念上一个类不是一个数据结构,一个类表示良好,一类对象,而对象是抽象的(在单词的英文含义中,而不是单词的C++或C#含义)实体.
我会说类和对象就像实践背后的理论,实践是使用方法和数据实现对象.数据可以是简单的或复杂的(所谓的高级数据结构).
一个类只是一个数据和方法的集合,可以对这些数据进行操作。您可以使用类来实现数据结构,但它们是不同的东西。
Take the Linked List for example. You can implement a Linked List data structure using a class, and in some languages this is the cleanest and most obvious way of doing it. It is not the only way to implement a Linked List, but it might be the best depending on the language.
A Linked List however, has nothing to do with being a class. A Linked List is instead a way of representing data as separate nodes where each node is linked to the next in some fashion.
A data structure is a conceptual way of modeling data, each different data structure having different properties and use cases. A class is a syntactic way that some languages offer to group data and methods.
Classes often lend themselves to being used to implement data structures, but it would be incorrect to say that a class == a data structure.
归档时间: |
|
查看次数: |
19878 次 |
最近记录: |