I recently wrote a class library that includes some objects that model certain types of files. For example, there is an abstract Document
class, with derived classes PdfDocument
(concrete) and OfficeDocument
(abstract, with concrete derived classes such as WordDocument
and ExcelDocument
), etc.
Currently the way clients create a new object is by selecting the appropriate derived class and passing it the byte array. So for example, if I have a byte array of a PdfDocument and a WordDocument, I would do something like:
var wordDocument = new WordDocument(wordDocumentByteArray);
var pdfDocument = new PdfDocument(pdfDocumentByteArray);
Is this acceptable design, that the client must know what derived class to use? Or would I be better off hiding all but the abstract Document
class and using something such as an abstract factory pattern to return the correct derived type? e.g.:
var wordDocument = DocumentFactory.GetDocument(wordDocumentByteArray, "docx");
// pass file extension so we know what the file is
Note that the derived types do not add additional properties/methods to the abstract class, they just implement the abstract methods in different ways.
Document
class have everything that the end user will ever need to do with a givenDocument
, or will they occasionally (or frequently) need access to functionality that's specific to a more derived type? – TilloDocument
class has one public abstract method. All derived classes consist only of protected and private helper methods (plus the overridden public method) with the sole purpose of implementing the one public method. – Reproduction