是否有任何面向对象的方法或模式来处理表格数据



使用表格数据所需的操作:

  • 用行切换列
  • 在特定位置添加列/行
  • 将列/行/单元格标记为属于同一组并获取特定组
  • 更改列/行顺序
  • 按行/列计算合计
  • 转换为透视表
  • 将结果输出为HTML/JSON

使用表格数据时,什么是好的面向对象方法?

我认为这里首先需要的是一个好的/合适的数据结构来保存/操作表数据。这取决于您可能具有的特定要求,例如表的大小、性能要求等。

假设您将使用某个Matrix类,该类对表提供低级操作(设置单元格值、添加/删除行、转置等)。

该类将使用基本数据结构进行操作,例如,它可能具有get_row方法,该方法将返回数字的list。例如,现在我们可以获得该列表中值的摘要,但我们不能只更改某些列表项并将此更改反映在父Matrix中(行数据与父矩阵结构断开连接)。

现在我们可以在这个Matrix类上构建我们的结构,我们的两个目标是:

1) 为了让用户更方便,一些"便利"可以是:

  • 行、列和单元格对象都连接到父Table对象(如果我们修改行项目,它将反映在父表和其他行/列/单元格obejct中)
  • 我们可以生成相同数据的不同视图(如透视表)
  • 我们可以使用特殊的列/行寻址系统(例如对行使用1、2、3…,对列使用A、B、C…)

2)隐藏我们用于存储表数据的实际数据结构

  • 通过这种方式,我们可以在不破坏用户代码的情况下独立地实现Matrix
  • 我们甚至可以用不同的结构来替换它(也许我们找到了更快或占用更少内存的东西),或者我们可以针对不同的情况使用不同的实现(例如桌面和移动应用程序)

这是我开始使用的近似类结构(类似python的伪代码):

class Matrix:
"""The base data structure, implementation detail."""
get_cell(int x, int y):
"""Returns cell value by x/y column and row indexes."""
set_cell(int x, int y, value):
"""Sets cell value by x/y column and row indexes."""
get_row(int index) -> list:
"""Returns `list` of values in the `index` row."""
get_column(int index) -> list;
"""Returns `list` of values in the `index` column."""

Matrix类是一个低级数据结构,不应该是公共接口的一部分。

公共接口由Table类和以下其他相关类表示:

class Table:
"""The user-level interface to work with table data."""
constructor():
"""Initializes Matrix object."""
# The "_data" object is private, only to be used internally.
self._data = Matrix()
row(int number) -> Row:
"""Returns `Row` object by row number (1, 2, 3, ...)."""
row = Row(self, number)
self.attach(row)
return row
column(string name) -> Column:
"""Returns `Column` object by string name (A, B, C, ...)."""
column = Column(self, name)
self.attach(column)
return column
cell(int row_number, string col_name) -> Cell:
"""Returns `Cell` object by string name (A, B, C, ...)."""
cell = Cell(self, row_number, col_name)
self.attach(cell)
return column
attach(Observer observer):
"""Register an observer to be notified when Table state was changed."""
self.observers.append(observer)
_notify():
"""Notify all dependent objects about the state change."""
for observer in self.observers:
observer.update()
...

为了保持TableRow /Column /Cell对象同步,我们可以使用Observer模式。

这里,TableSubjectRow/Column/CellObservers。一旦Table(和底层数据)的状态发生更改,我们就可以更新所有依赖对象。

class Row(Observable):
"""Table row object."""
constructor(Table parent, int index):
self.parent = parent
self.index = index
self._data = None
self.update()
update()
"""Update row data.
Fetches the `list` or row values from the `Matrix` object.
"""
# Note: we have two choices here - the `Row`, `Column` and `Cell` objects
# can either access `Table._data` property directly, or `Table` can provide
# proxy methods to modify the data (like `_get_value(x, y)`); in both cases
# there is a private interface to work with data used by `Table`, `Row`,
# `Column` and `Cell` classes and the implementation depends on the language,
# in C++ these classes can be friends, in python this can be just a documented
# agreement on how these classes should work.
# See also the comment in the `remove` method below.
self._data = parent._data.get_row(index)
sum():
"""Returns sum of row items."""
sum = 0
for value in self._data:
sum += value
return sum
cell(string col_name):
"""Returns cell object."""
return parent.cell(self.index, col_name)
remove():
"""Removes current row."""
# Here we access `parent._data` directly, so we also have to
# call `parent._notify` here to update other objects.
# An alternative would be a set of proxy methods in the `Table` class
# which would modify the data and then call the `_notify` method, in such case 
# we would have something like `self.parent._remove_row(self.index)` here.
self.parent._data.remove_row(self.index)
self.parent._notify()
self.parent.detach(self)

ColumnCell类相似,Column将保存列数据,Cell将封装单元格值。用户级别的使用情况如下:

table = Table()
# Update table data
table.cell(1, "A").set(10)
table.cell(1, "B").set(20)
table.row(1).cell("C").set(30)
# Get row sum
sum = table.row(1).sum()
# Get the table row
row = table.row(1)
# The `remove` operation removes the row from the table and `detaches` it,
# so it will no longer observe the `table` changes.
row.remove()
# Now we have the detached row and we can put it into another table,
# so basically we cut-and-pasted the row from one table to another
another_table.add_row(row)

使用这种方法,您可以很容易地实现复制、剪切、粘贴等操作。您也可以在这里应用命令模式,并将这些操作提取到小类中。通过这种方式,实现撤消和重做也将非常容易。

CCD_ 25表也可以是一种特殊类型的CCD_。根据数据透视表功能的要求,您可能会发现Builder模式对配置数据透视表很有用。类似这样的东西:

pivotBuilder = PivotBuilder(table)
# Group by column "A" and use `SumAggregator` to aggregate grouped values.
pivotBuilder.group_by_column("A", SumArggregator())  # or maybe pivotBuilder.groupBy(table.column("A"))
pivotTable := pivotBuilder.get_result()

将表导出为不同格式的类可能不必是可观察的,所以它们只需包装Table对象并将其转换为适当的格式:

json_table = JsonTable(table)
data = json_table.export()

当然,以上只是许多可能的实现选项之一,根据您的具体需求,将它们视为一些有用(或不有用)的想法。

你可以在GoF模式书中找到更多的想法。

最新更新