开发者

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 doc开发者_如何学运维uments 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜