Created
June 17, 2015 10:22
-
-
Save 121jigowatts/4255e9619eb52cd8976c to your computer and use it in GitHub Desktop.
[C#実践開発手法]プロファイリングデコレーターより
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ProfilingInterfaces; | |
using ServiceInterfaces; | |
namespace ServiceImplementations | |
{ | |
public class CompleateProfilingUserService : IUserService | |
{ | |
private readonly IUserService _service; | |
private readonly ITimeKeeper _timeKeeper; | |
public CompleateProfilingUserService(IUserService service, ITimeKeeper timeKeeper) | |
{ | |
this._service = service; | |
this._timeKeeper = timeKeeper; | |
} | |
public string GetName() | |
{ | |
_timeKeeper.Start(); | |
var result = _service.GetName(); | |
_timeKeeper.Stop(); | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace ProfilingInterfaces | |
{ | |
public interface ITimeKeeper | |
{ | |
void Start(); | |
long Stop(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ServiceInterfaces | |
{ | |
public interface IUserService | |
{ | |
string GetName(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using ProfilingImplementations; | |
using ProfilingInterfaces; | |
using ServiceImplementations; | |
namespace Profiling.UI | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var nomalController = new UserController(new UserService()); | |
nomalController.Display(); | |
var profilingController = new UserController( | |
new CompleateProfilingUserService( | |
new UserService(), | |
new TimeKeeper( | |
new StopwatchAdapter( | |
new Stopwatch() | |
) | |
) | |
) | |
); | |
profilingController.Display(); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using ProfilingInterfaces; | |
namespace ProfilingImplementations | |
{ | |
public class StopwatchAdapter : ITimeKeeper | |
{ | |
private readonly Stopwatch _stopwatch; | |
public StopwatchAdapter(Stopwatch stopwatch) | |
{ | |
this._stopwatch = stopwatch; | |
} | |
public void Start() | |
{ | |
_stopwatch.Start(); | |
} | |
public long Stop() | |
{ | |
_stopwatch.Stop(); | |
var time = _stopwatch.ElapsedMilliseconds; | |
_stopwatch.Reset(); | |
return time; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using ProfilingInterfaces; | |
namespace ProfilingImplementations | |
{ | |
public class TimeKeeper : ITimeKeeper | |
{ | |
private readonly ITimeKeeper _timekeeper; | |
public TimeKeeper(ITimeKeeper timekeeper) | |
{ | |
this._timekeeper = timekeeper; | |
} | |
public void Start() | |
{ | |
Console.WriteLine("Process Start..."); | |
_timekeeper.Start(); | |
} | |
public long Stop() | |
{ | |
var time = _timekeeper.Stop(); | |
Console.WriteLine("\t実行時間 {0} seconds", TimeSpan.FromMilliseconds(time).TotalSeconds); | |
return time; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using ServiceInterfaces; | |
namespace Profiling.UI | |
{ | |
internal class UserController | |
{ | |
private readonly IUserService _service; | |
public UserController(IUserService service) | |
{ | |
this._service = service; | |
} | |
internal void Display() | |
{ | |
var name = _service.GetName(); | |
Console.WriteLine("Hi {0} !", name); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ServiceInterfaces; | |
namespace ServiceImplementations | |
{ | |
public class UserService : IUserService | |
{ | |
public string GetName() | |
{ | |
System.Threading.Thread.Sleep(3000); | |
return "BoB"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考にさせていただいたコードの~Componentという記述で混乱したので自分なりに読み替えてみたけど、余計わかりにくくなったかも。。。