oop - Code design: Who's responsible for changing object data? -
assuming have kind of data structure work on (for example images) want pre- , postprocess in different ways make further processing steps easier. what's best way implement responsibility oop language c++?
further assuming have lot of different processing algorithms inherent complexity want encapsulate them in dedicated classes. means though algorithm implementations externally have set kind of info in data indicate having been processed. , doesn't clean design me because having been processed seems info associated data , data object should determine , set on own.
it looks common source of error in complex applications: implements processing algorithm, forgets set flags in data appropriately, in different parts of application won't work expected , have lots of fun spotting error.
can outline general structure of , fail-save way implement sth this?
to make sure understand asking, here assumptions based on reading of question:
- the data kind of binary format (presumably image anything) can represented array of bytes
- there number of processing steps (i'll refer them transformations) can applied data
- some transformations depend on other such that, example, avoid applying transformation if pre-requisite has not been applied. robust, attempting apply illegal transformation detected , prevented.
and question how in object-oriented way avoids future bugs complexity of program increases.
one way have image data object, encapsulates both binary data , record of transformations have been applied it, responsible executing transformation through transformation object delegate; , transformation objects implement both processing algorithm , knowledge of whether can applied based on previous transformations.
so might define following (excuse java-like naming style; it's been long time since i've done c++):
- an enumerated type called transformationtype
- an abstract class called transformer, following methods:
- a method called 'gettype' returns transformationtype
- a method called 'cantransform' accepts list of transformationtype , returns boolean. list indicates transformations have been applied data, , boolean indicates whether ok execute transformation.
- a method called 'transform' accepts array of bytes , returns array of (presumably modified) bytes
- a class called binarydata, containing byte array , list of transformationtype. class implements method 'void transform(transformer t)' following:
- query transformer's 'cantransform' method, passing list of transformation types; either throw exception or return if cantransform returns false
- replace byte array results of invoking t.transform(data)
- add transfomer's type list
i think accomplishes want - image transformation algorithms defined polymorphically in classes, actual application of transformations still 'controlled' data object. hence not have trust external code right thing wrt setting / checking flags, etc.
Comments
Post a Comment