Code design: who is responsible for modifying object data?

Assuming I have some data structure to work with (like images) that I want to pre-process and post-process in different ways to facilitate further processing steps. What's the best way to implement this responsibility with an OOP language like C ++?

Assuming I have many different processing algorithms with inherent complexity, I most likely want to encapsulate them in dedicated classes. This means, however, that outsider implementations of the algorithms must set some information in my data to indicate that it has been processed. And it also doesn't look like a clean design to me, because the processed information is like data-bound information, and therefore something that the data object itself has to define and set on its own.

It also looks like a very common source of bugs in complex applications: someone implements a different processing algorithm, forgets to set flags correctly in the data, something in completely different parts of the application won't work as expected, and someone will have a lot of fun discovering error.

Can someone describe the general structure of a good and bad way to implement sth like this?

+3


source to share


1 answer


To understand what you are asking, here are my assumptions based on my reading of the question:

  • The data is a kind of binary format (presumably an image, but as you say it could be anything) that can be represented as an array of bytes
  • There are several processing steps (I'll call them transformations) that can be applied to data.
  • Some transformations depend on others, for example, you would like to avoid applying a transform if it has not been previously applied. You would like it to be reliable, so that an illegal transformation attempt is detected and prevented.

And the question is how to do it in an object-oriented way that avoids future errors as the complexity of the program increases.

One way is for an image data object that encapsulates both binary data and a record of transformations that have been applied to it is responsible for performing the transformation through the Transformation object's delegate; and Transformation objects implement both the processing algorithm and the knowledge of whether it can be applied based on previous transformations.



So, you can define the following (excuse my Java-like naming style), I've been doing C ++ for a long time):

  • An enumerated type called TransformationType
  • An abstract class called Transformer with the following ways:
    • Method called 'getType' that returns TransformationType
    • A method called "canTransform" that takes a TransformationType list and returns a boolean value. The list indicates transformations that have already been applied to the data, and a boolean value indicates whether it is okay to perform this transformation.
    • A method called "transform" that takes an array of bytes and returns an array of (presumably modified) bytes
  • A class called BinaryData that contains an array of bytes and a TransformationType list. This class implements the "void transform (Transformer t)" method to do the following:
    • Request the 'canTransform' transformer method, passing in a list of transformation types; either throw an exception or return if canTransform returns false
    • Replace the byte array with the result of calling t.transform (data)
    • Add transfomer type to the list

I think this does what you want - the image transformation algorithms are defined polymorphically in the classes, but the actual application of the transforms is still "controlled" by the data object. Hence, we don't need to trust the external code to do the right thing, setting / checking flags, etc.

0


source







All Articles