Skip to content

Instantly share code, notes, and snippets.

@jchannon
Forked from ianbattersby/gist:2159466
Created March 23, 2012 14:21
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 jchannon/2171067 to your computer and use it in GitHub Desktop.
Save jchannon/2171067 to your computer and use it in GitHub Desktop.
Factory Pattern FTW!
public class MainEntryPointClass : BaseClass
{
private ILogicFactory LogicFactory;
public MainEntryPointClass(ILogicFactory LogicFactory)
{
LogicFactory = LogicFactory;
}
public MainEntryPointClass() : this(new LogicFactory())
{
}
public SetupProperties()
{
base.ConfigProperties.Add(typeof(string), "DirectoryPath", "C:\\");
}
public Run()
{
ConstructConditionalDependencies();
}
private void ConstructConditionalDependencies()
{
this.Logic = LogicFactory.CreateLogic(base.ConfigProperties);
}
}
public interface ILogicFactory
{
ILogic CreateLogic();
}
public class LogicFactory : ILogicFactory
{
public ILogic CreateLogic(Properties props)
{
var timer = new Timer(props.Find("interval").Value);
var logger = new FileLogger(props.Find("directorypath").Value);
var logic = new Logic(timer,logger);
logic.RandomProperty = props.Find(..).Value();
return logic;
}
}
/// From this point downwards we're all good clean DI + ctor injection.
public class Logic : ILogic
{
private ITimer Timer;
private ILogger Logger;
public class Logic(ITimer timer, ILogger logger)
{
this.Timer = timer;
this.Logger = logger;
}
public SomeMethod()
{
this.Logger.Write("Data"); /******* How does DirectoryPath get assigned? *******/
}
}
public interface IFileLogger
{
void WriteData(string data);
}
public class FileLogger : IFileLogger
{
public FileLogger(string directoryPath)
{
this.DirectoryPath = directoryPath;
}
public void WriteData(string data)
{
}
public string DirectoryPath { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment