专门为稍微不同的数据结构定义子类

Defining a subclass exclusively for a slightly different data structure

我决定开始使用 OOP 使我的代码更加模块化,但我仍然需要学习很多关于什么是最佳实践。

当前数据结构

在这种特殊情况下,我试图定义一个 class 层次结构来处理(在最低级别)各种传感器的二进制示波器数据。独立于传感器特性,我定义了 class ScopeData,它只读取二进制文件和 returns x 和 y 数据以及一些通用特性:

classdef ScopeData

    properties
        Filename
        xdata
        ydata
    end

    properties (Access = private)
        % Some properties only used to extract data from the binary file
    end

    properties (Access = protected)
        fid = -1 % used in reading data and closing upon destruction

        % Properties that are required to quickly navigate to the data
        % locations in the binary file
    end

    properties (Dependent = true)
        Raw % raw data 
    end

    %% Methods related to constructing/destructing
    methods
        function obj = ScopeData(filepath)

            % Input argument checking is done here

            % Assign the filename
            obj.Filename = filepath;

            % Open the file and read its contents
            obj.Open;
            obj.Read;
        end


        function obj = Open(obj)
            % Opens obj.Filename and extracts all info required to read out
            % the data
        end

        function obj = Read(obj)
            rawdata = obj.Raw; % get the raw data

            % Some minimal processing and averaging to get xdata and ydata
            obj.xdata = SomeFunction(rawdata);
            obj.ydata = SomeOtherFunction(rawdata);

        end
    end

    %% Get/set methods
    methods
        function rawdata = get.Raw(obj)
            % uses the private properties to efficiently extract the raw data
        end
    end

end

我为每个不同类型的传感器创建了继承自 ScopeData 的子classes SensorXYZData。它们的附加属性都是特定传感器独有的,用于进一步处理数据,例如:

classdef SensorXData < ScopeData

    properties
        % Sensor-specific properties
        SensorLocation
        SensorConfiguration
    end

    properties (Dependent = true)
        % Sensor-specific dependent properties
    end

    methods
        function obj = SensorXData(filepath, SensorLocation, SensorConfiguration)
            % Initialize object
            obj = obj@ScopeData(filepath);

            % Assign sensor-specific properties

            % Do some additional processing

        end
    end

    methods
        % Get/set methods for the dependent properties
    end

end

上述数据结构允许我为每个读取单个二进制文件的传感器定义单独的对象。我目前在 class 文件之外为每个传感器系列制作对象数组。未存储原始数据,但可以轻松请求。

我想做什么

在不同的传感器系列中,只有 SensorX 系列会同时处理单个数据文件(默认)和每个传感器的多个数据文件(不同类型的实验)。 在每个文件的基础上,后者将需要与前者完全相同的处理,但还有多文件情况独有的其他属性和处理步骤。

我目前正在尝试通过继承自 SensorXData:

的附加子class SensorXArrayData 来实现这种特殊情况
classdef SensorXArrayData < SensorXData

    properties
        ExtraProperties
    end

    properties (Dependent = true)
        % Dependent variables specific to this data
    end

    methods
        function obj = SensorXArrayData(MultipleFilepaths, SensorLocation, SensorConfiguration, ExtraProperties)
            % Initialize object - I am only allowed to call obj@SensorXData once, so this currently doesn't work.
            obj = obj@SensorXData(MultipleFilepaths, SensorLocation, SensorConfiguration);

            % Assign the extra properties

            % More processing that is specific

        end
    end

    methods
        % Get/set methods for the dependent variables
    end

end

为了使上述工作正常,需要采取一些额外的步骤,我正在尝试找出最合适的方法。

其中一个选项似乎是使 ScopeData superclass 与多文件输入兼容并从那里开始工作。还有一种方法可以让 ScopeDataSensorXData 保持不变吗?

我认为您正在寻找的是 class(Java 术语)中的“实现接口”。它在 MATLAB 中的工作方式是使用 multiple inheritance,它看起来像这样:

classdef ClassName < SuperClass1 & SuperClass2

我相信这几年OOP的共识就是所谓的“composition over inheritance”,基本上就是说你应该专注于你class 做的事情 而不是你的 class .

如何将此应用于您的问题?

添加几个 class 如下所示:

classdef (Abstract) SomeFeatureInterface < handle
% This is a specialized interface for doing operation XYZ.
  properties (Abstract = true, Access = protected, Constant = true)
    SOME_CONSTANT;
  end  

  methods (Abstract = true, Access = public) % This doesn't have to be abstract, you can add a
    outName = methodName(obj)                % default implementation if relevant.
  end
  
end

那么你派生的 classes 的声明将看起来像购物清单,也就是说,你可以通过继承相关接口(抽象)从 class 声明中添加任何需要的功能classes:

classdef SensorXData < ScopeData & SingleFileInterface & MultiFileInterface

classdef SensorYData < ScopeData & SingleFileInterface

这样,SensorXData将包含处理多个文件的相关函数,而SensorYData则不会。此外,这将确保浅继承,这被认为是好的。