Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would recommend creating a normal class the contains this behavior, and then let that class implement an interface extracted from the class' members.</p> <p>Whenever you need to call those methods, you inject the <strong>interface</strong> (not the concrete class) into the consumer. This lets you vary the two independently of each other.</p> <p>This may sound like more work, but is simply the <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow noreferrer">Strategy</a> design pattern applied.</p> <p>This will also make it much easier to unit test the code, because the code is more loosely coupled.</p> <hr> <p>Here's an example in C#.</p> <p>Interface:</p> <pre><code>public interface ITimeMachine { IStopwatch CreateStopwatch(); DateTimeOffset GetNow(); } </code></pre> <p>Production implementation:</p> <pre><code>public class RealTimeMachine : ITimeMachine { #region ITimeMachine Members public IStopwatch CreateStopwatch() { return new StopwatchAdapter(); } public DateTimeOffset GetNow() { return DateTimeOffset.Now; } #endregion } </code></pre> <p>and here's a consumer of the interface:</p> <pre><code>public abstract class PerformanceRecordingSession : IDisposable { private readonly IStopwatch watch; protected PerformanceRecordingSession(ITimeMachine timeMachine) { if (timeMachine == null) { throw new ArgumentNullException("timeMachine"); } this.watch = timeMachine.CreateStopwatch(); this.watch.Start(); } public abstract void Record(long elapsedTicks); public virtual void StopRecording() { this.watch.Stop(); this.Record(this.watch.ElapsedTicks); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThis means I should create class for handling custom functions + class for each of these functions and then simply call just execute()? So, for example, function "timeToString" would become a separate class with just execute() function, implementing interface. And I'd call it through new CustomFunction( new TimeToString ); ... $custom_function->execute() ? Is this how Strategy pattern works or do I misunderstand the concept? Isn't it a bit of overkill to create basically class for every single function?
      singulars
    2. COYou don't have to create a class for each function. You can bundle related operations in a single class. That's what encapsulation and cehesion is all about, after all. Those operations can all be defined on the same interface, thus giving you the ability to pass around a single cohesive set of operations in a bundle.
      singulars
    3. COCould you, please, edit your post with a short example (in any language)? As I still fail to understand how exactly to do this. Should I simply create a class with multiple functions, implement an interface extracted from members and then inject this interface where ever I need it and call all those functions through it? I think what confuses me is that you mentioned Strategy pattern and I'm trying to implement the solutions exactly by it. I also don't understand why would I have an interface that is implemented by just one class. Just to prevent tight couplings by calling concrete class?
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload