oop - Where to put methods used by multiple classes? -


What is the question about showing an example:

I currently have a PHP project I'm working on I keep in mind a method that will be used for many classes (in this case UI-MVC model), but I'm not sure how to represent such methods in OO design. Do the first thing that came to my mind, a class with those static works Nana was that I had to call and wish them. Although I'm not sure that this is the right thing.

To be more precise, I want to work, for example, over time. That's why I will need many methods that handle time. I was thinking about making a time class, where I work, it checks that time is in the right format etc.

Some people can say that I should not use the class for this, because I can still use the procedural code in PHP but I am more interested in the answer that I have in OP / OOD How to highlight the attitude of such situations.

So the real questions are: how to represent such methods? Is the static function approach good enough or should I reconsider some more?

I recommend creating a normal class in this behavior, and then class that class Apply the extracted interface to the members.

Whenever you need to call those methods, then you inject the interface (the concrete square) in the consumer, it separates you from each other freely gives.

It may look like more work, but it's just the design pattern applied.

This is also the unit test code, because the code is more loose coupled.


Here is an example in C #.

Interface:

  Public Interface ITimeMachine {eStatWatchWatchStopWatch); DateTime Offset GETO (); }  

Production implementation:

  Public class Realtime MacKin: ITimeMachine {#region ITimeMachine Member Public Isabatwatch CreateStopwatch () {new stopwatchEdper (); } Public DateTime Offset GetNow () {DateTime Offset.Now; } #endregion}  

And here is the user of the interface:

  Public intangible class PerformanceRecording session: IDisposable {Personal Readonly IStopwatch Clock; Secure DisplayRaking session (ITimeMachine timeMachine) {if (Timemachin == blank) {New argument NullException ("Timematchin"); } This.watch = timeMachine.CreateStopwatch (); This.watch.Start (); } Public abstract zero records (long time passed); Public Virtual Zero StopRecording () {this.watch.Stop (); This.Record (this.watch.ElapsedTicks); }}  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -