Gere
Gere

Reputation: 12717

Place for a method dealing with two classes?

I often have methods which need to know the structure of two classes. For example some class Table which can be written to a class ExcelDoc.

The method to write tables to excel documents needs to know the structure of both. Both can change and maybe even have multiple implementations Table1, Table2, Excel1, Excel2 which all provide some basic interface.

Now I could either implement

ExcelDoc.write_data(table)

or

Table.write_to(excel)

or I could even make an intermediate class

TableToExcel.write(excel, table)

or I could derive from classes and...

What do you think is the best design if I want to the optimal design for future further development and features?

Upvotes: 1

Views: 35

Answers (1)

Shash316
Shash316

Reputation: 2218

If you can represent data to be written in some intermediate form, then both of your classes i.e. Excel and Table can use the same representation. i.e. Excel.write_data(data_in_intermediate_form) and Table.write_data(data_in_intermediate_form).

Or else Table and Excel have a known interface for reading / writing data, then you can implement interfaces for each other in Table and Excel classes. To get data from Table to Excel, you would do ExcelObject.import(dataFromTable). Makes sense ? (There are whole bunch of import examples for achieving simialr goals, right ?)

Shash

Upvotes: 1

Related Questions