简体   繁体   English

我此时是否应该为此目的使用抽象基类?

[英]Should I be using an abstract base class for this purpose at this time?

I only very recently began developing professionally. 我最近才开始专业发展。 I studied OOP while at University, but don't feel as if I ever really used it in a "real world" scenario. 我在大学期间学习了OOP,但感觉我并没有真正在“现实世界”的场景中使用它。

Basically, in trying to create/manipulate a specific type of document within the organization, I created a class for it, with the thinking that anytime we wanted to create/manipulate that specific type of document, we could just create an instance of it and give it certain information and have it take care of itself. 基本上,在尝试在组织内创建/操作特定类型的文档时,我为它创建了一个类,认为只要我们想要创建/操作特定类型的文档,我们就可以创建它的实例,给它一定的信息并让它自己照顾。

I know we're planning on working with other types of documents (I guess I should note, when I say types of documents , I mean something like "expense report" or "inventory list", as in I'm referring to the content.) I assumed all of these kinds of documents would share certain kinds of functionality (for example, working with shared strings or defined names in Excel), so I then created an abstract Document class, and thought each type of document we wanted to create could extend this class. 我知道我们正在计划使用其他类型的文件(我想我应该注意,当我说文件的类型时 ,我的意思是“费用报告”或“库存清单”,就像在我指的是内容。)我假设所有这些类型的文档都会共享某些类型的功能(例如,在Excel中使用共享字符串或定义的名称),所以我创建了一个抽象的Document类,并考虑了我们想要创建的每种类型的文档可以扩展这个类。

Of course, I'm still only working with one type of document, so while I sort of have an idea of methods that might be common to all documents, I still at this point only have one abstract class and one that extends it, so it seems sort of unneccesary. 当然,我仍然只使用一种类型的文档,所以虽然我对所有文档可能共有的方法有所了解,但我现在仍然只有一个抽象类和一个扩展它的类,所以这似乎有点不必要。

I guess my questions are: 我想我的问题是:
1) Should I even be using an abstract class here. 1)我是否应该在这里使用抽象类。 Like does this seem like the appropriate use of one or have I completely missed the point? 就像这似乎是适当使用一个或我完全错过了这一点?
2) If I should be using one, should I not be using it this early? 2)如果我应该使用一个,我应该尽早使用吗? I mean, should I wait until I actually have several documents in front of me so I can actually determine what kind of functionality is shared between all of them rather than sort of assume I know for now even though I only have one class implementing it? 我的意思是,我应该等到我实际上有几个文件在我面前所以我实际上可以确定它们之间共享什么样的功能而不是假设我现在知道,即使我只有一个类实现它?

Thanks. 谢谢。

Abstract class sounds about right from your description: there are certain properties and behaviours that are common to all the derived types (some of these may be 'default' behaviours that derived classes may change). 抽象类听起来与您的描述有关:所有派生类型都有一些共同的属性和行为(其中一些可能是派生类可能会改变的'默认'行为)。 However, some of the derived classes may have additional/alternative behaviour from the others. 但是,某些派生类可能具有其他类别的附加/替代行为。 If there were no 'default' behaviours and only a method specification, then an interface would have been more appropriate. 如果没有“默认”行为且只有方法规范,那么接口就更合适了。

As to whether it's too early: How certain are you that you will definitely need more than one derived class. 至于是否为时过早:你肯定需要多个派生类。 I wouldn't bother with setting up abstract base classes until there was no possible doubt that it would be needed. 我不打算设置抽象基类,直到不可能怀疑它是否需要。 This is known as YAGNI (You Aren't Going To Need It); 这被称为YAGNI(你不会需要它); don't create code until the last possible minute, otherwise you might never need it and you've saddled yourself with extra potential maintenance. 不要在最后一分钟之前创建代码,否则你可能永远不需要它而且你已经背负了额外的潜在维护。

As the purpose of an abstract class is to provide a common definition of a base class that derived classes can share. 由于抽象类的目的是提供派生类可以共享的基类的通用定义。 so it is perfect to use here. 所以在这里使用它是完美的。

Should this be abstracted: Yes. 是否应该抽象:是的。

For should you use it this early. 因为你应该尽早使用它。 Yes, it's a simple 1, 2, 3 rule. 是的,这是一个简单的1,2,3规则。 If you have to write something once, ok. 如果你必须写一次,好吧。 Twice, think about making it common depending on its size. 两次,考虑根据它的大小使它变得常见。 Three times definitely make it common. 三次绝对让它变得普遍。

I am a big fan of making things as generic as possible. 我非常喜欢尽可能地制作通用的东西。 I get mad when I have to write the same thing more than once. 当我不得不多次写同样的东西时,我生气了。

I would suggest that it may be helpful to define both an abstract class and one or more interfaces. 我建议定义一个抽象类和一个或多个接口可能会有所帮助。 Generally use parameters and variables of the interface types except in routines which deal with object creation (use the abstract class for those). 通常使用接口类型的参数和变量,除了处理对象创建的例程(使用抽象类)。 The basic document and interface might support some basic functions like GetPageCount, RenderToScreenBitmap, RenderToPrinter, RenderToHtml, GetVersionInfo, etc. A document class which inherits from the abstract document class could use default logic for any of those functions it didn't need to change. 基本文档和接口可能支持一些基本功能,如GetPageCount,RenderToScreenBitmap,RenderToPrinter,RenderToHtml,GetVersionInfo等。继承自抽象文档类的文档类可以使用默认逻辑来处理它不需要更改的任何函数。 Using interface rather than the base type, however, would make it possible for someone to retrofit another document type (which might inherit something completely different) to be usable on your system by adding the necessary interface functions. 但是,使用接口而不是基本类型,可以让某人通过添加必要的接口函数来改进另一种文档类型(可能会继承完全不同的文档类型)以在系统上使用。

Note that it would probably be good to use some sort of adapter pattern for things like printing and rendering, but that's going beyond the scope of the original question. 请注意,对于打印和渲染等内容使用某种适配器模式可能会很好,但这超出了原始问题的范围。 Rendering for screen viewing is different from rendering for printing (there may not be a firm subdivision into "pages" when viewing on-screen), so it's probably sensible to have different methods for those functions, though the details of implementation will require some careful thought. 用于屏幕查看的渲染与用于打印的渲染不同(在屏幕上观看时可能没有牢固地细分为“页面”),因此对于这些功能具有不同的方法可能是明智的,尽管实现的细节将需要一些仔细思想。

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

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