Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I structure this class inheritance?

I'm developing an application for transferring databases and directories of user-uploaded images/documents from our production server to the development server. The application is written in Coldfusion, but I don't think the language is relevant to this question - it's more of a structural/architecture question than a question specific to the language.

I'll be writing pseudo code for my examples, so please don't pick apart the syntax.

When I've seen class inheritance demonstrated, it's usually something simple like class Student extends Person() {}. Obviously, a Student is a more specialized Person, so this makes sense.

In my application, I have a class Site() which contains relevant information such as the DSN, file upload directory, etc. I perform my SQL exports in one class and my file upload exports in another class, both of which are called from within the site class(pseudo-code):

class Site {
    public function exportSql() {
        sqlExport = new sqlExport(this.dsn);
        sqlExport.createDumpAndZipItAndStuff();
    }
    public function exportUploads() {
        uploadsExport = new uploadsExport(this.uploadDirectory);
        uploadsExport.copyAndZipFilesAndStuff();
    }
}

The Site class doesn't do anything other than control the flow of traffic that is requested from the front-end of the application, everything else is handed off to one of the export classes.

This works fine for me, but I'd like to structure it properly. Right now, I have to pass Site's properties to the constructor of the export classes, and then set the export class's properties with those arguments. This causes a lot of duplication.

Would it be appropriate to have my export classes inherit the Site class so that I could access the properties directly? The export classes are not a more specialized Site, as in the Person/Student example I gave earlier. Rather, they just perform the heavy lifting for the Site class.

If this is not an appropriate situation for inheritance, how should I structure the application? As I said earlier, the way I'm doing it right now works, but I would like to take this opportunity to learn more about design patterns and write my code in a way that makes sense.

like image 262
Sean Walsh Avatar asked May 18 '11 19:05

Sean Walsh


People also ask

Can a structure inherit a class?

Summary: Yes, a struct can inherit from a class. The difference between the class and struct keywords is just a change in the default private/public specifiers.

How do you inherit a structure?

Using inheritance one class/structure can inherit the properties of another class/structure i.e the functions and data members of one class/structure are inherited by another. The derived class/structure inherits from the base class/structure also known as parent class/structure.

How do you show inheritance in class?

To show inheritance in a UML diagram, a solid line from the child class to the parent class is drawn using an unfilled arrowhead.


1 Answers

Inheritance should be use when something is a kind of another thing.

Like a dog is kind of animal, therefor it should inherit from animal.

Export is not a kind of Site, so it shouldn't inherit from it.

What you're looking for is Composition. Export should hold a reference to a Site which it exports. Then you can pass the Site object to it on contruction, and it can get the data from the site whenever you need.

like image 68
Yochai Timmer Avatar answered Oct 10 '22 13:10

Yochai Timmer