lundi 21 août 2017

Code sharing when using composition

Let's say we have the following class (simplified):

class AudioRecorder {
    private func onRecord(data: [Float]) {
        self.delegate?.audioRecorder(self, didRecordFrame: audioData)

    public func play() { ... }
    public func stop() { ... }
}

So it records frame and notifies it's delegate about it. Now I need to extend it with the following capabilities:

  1. Buffering - Buffer X number of frames
  2. Interval recording - Record - stop 1s - Record - stop 1s ...
  3. Interval buffering - record in intervals and buffer

I can't use inheritance for this because if IntervalRecorder and BufferingRecorder are subclasses then I have problem implementing IntervalBufferingRecorder.

On another hand, when using composition I need to duplicate play pause methods in all of IntervalRecorder, BufferingRecorder and IntervalBufferingRecorder and just forward to underlying object.

What would be good approach to this problem? Language is Swift but I guess that is not very important.

Aucun commentaire:

Enregistrer un commentaire