表格数据所需的操作:
使用表格数据的面向对象方法是什么?
答案 0 :(得分:1)
我认为这里需要的第一件事是保持/操纵表数据的良好/适当的数据结构。 这取决于您可以具备的具体要求,例如表的大小,性能要求等。
假设你将使用一些Matrix
类,它提供对表的低级操作(设置单元格值,添加/删除行,转置等)。
此类将使用基本数据结构,例如,它可能具有get_row
方法,该方法将返回list
个数字。
现在我们可以在此列表中获取值的摘要,但我们不能只更改某个列表项并将此更改反映在父Matrix
中(行数据与父项断开连接)矩阵结构)。
现在我们可以在这个Matrix
类上构建我们的结构,我们的两个目标是:
1)使用户更方便,一些方便"可以是:
Table
对象的行,列和单元格对象(如果我们修改行项,它将反映在父表和其他行/列/单元格对象中)。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()
...
要保持Table
和Row /
Column /
Cell
个对象同步,我们可以使用Observer模式。
此处Table
为Subject
,Row
/ Column
/ Cell
为Observers
。
一旦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)
Column
和Cell
类相似,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)
使用这种方法,您可以非常轻松地实现复制,剪切,粘贴等操作。您也可以在此处应用Command pattern并将这些操作提取到小班级中。这样,实现撤消和重做也很容易。
PivotTable
表也可以是一种特殊的Observable
。
根据对数据透视表功能的要求,您可能会发现Builder pattern对配置数据透视表很有用。
像这样:
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 patterns book中找到更多想法。