Skip to content

Instantly share code, notes, and snippets.

@121jigowatts
Created June 17, 2015 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 121jigowatts/4255e9619eb52cd8976c to your computer and use it in GitHub Desktop.
Save 121jigowatts/4255e9619eb52cd8976c to your computer and use it in GitHub Desktop.
[C#実践開発手法]プロファイリングデコレーターより
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;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProfilingInterfaces
{
public interface ITimeKeeper
{
void Start();
long Stop();
}
}
namespace ServiceInterfaces
{
public interface IUserService
{
string GetName();
}
}
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();
}
}
}
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;
}
}
}
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;
}
}
}
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);
}
}
}
using ServiceInterfaces;
namespace ServiceImplementations
{
public class UserService : IUserService
{
public string GetName()
{
System.Threading.Thread.Sleep(3000);
return "BoB";
}
}
}
@121jigowatts
Copy link
Author

参考にさせていただいたコードの~Componentという記述で混乱したので自分なりに読み替えてみたけど、余計わかりにくくなったかも。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment