-
-
Save jchannon/2171067 to your computer and use it in GitHub Desktop.
Factory Pattern FTW!
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
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