Forked from franreyes/ExtractAndOverrideFactoryMethod.cs
Last active
March 24, 2025 18:10
-
-
Save trikitrok/6824a2e13c25cae3a03e7aa4f34fa77c to your computer and use it in GitHub Desktop.
Example extract and override factory method
This file contains hidden or 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
class FileTransactionsRepository : ITransactionsRepository | |
{ | |
// more code... | |
} | |
class Account | |
{ | |
private ITransactionsRepository _transactionsRepository; | |
public Account() | |
{ | |
var filePath = Config.GetTransactionsFileLocation(); // <- side effect in a constructor!! x( | |
var xmlPath = Config.GetTransactionsXmlLocation(); // <- side effect in a constructor!! x( | |
var persister = new FileXmlPersister(xmlPath); | |
this._transactionsRepository = new FileTransactionsRepository(filePath, persister); | |
// more code... | |
} | |
// more code... | |
} | |
////////////////////////////////////////////// | |
// After extracting the factory method, | |
// you can apply Subclass & Override Method to override the factory method | |
class Account | |
{ | |
private ITransactionsRepository _transactionsRepository; | |
public Account() | |
{ | |
// extracted a factory method | |
this._transactionsRepository = CreateTransactionsRepository(); | |
// more code... | |
} | |
// factory method | |
private ITransactionsRepository CreateTransactionRepository() | |
{ | |
var filePath = Config.GetTransactionsFileLocation(); // <- side effect in a constructor!! x( | |
var xmlPath = Config.GetTransactionsXmlLocation(); // <- side effect in a constructor!! x( | |
var persister = new FileXmlPersister(xmlPath); | |
this._transactionsRepository = new FileTransactionsRepository(filePath, persister); | |
} | |
// more code... | |
} | |
////////////////////////////////////////////// | |
// After applying Subclass & Override Factory Method | |
class Account | |
{ | |
private ITransactionsRepository _transactionsRepository; | |
public Account() | |
{ | |
this._transactionsRepository = CreateTransactionsRepository(); | |
// more code... | |
} | |
// made protected for testing :( | |
protected virtual ITransactionsRepository CreateTransactionRepository() | |
{ | |
var filePath = Config.GetTransactionsFileLocation(); // <- side effect in a constructor!! x( | |
var xmlPath = Config.GetTransactionsXmlLocation(); // <- side effect in a constructor!! x( | |
var persister = new FileXmlPersister(xmlPath); | |
this._transactionsRepository = new FileTransactionsRepository(filePath, persister); | |
} | |
// more code... | |
} | |
////////////////////////////////////////////// | |
// In some test we'll override the factory method | |
// to return a test double of ITransactionsRepository | |
class ForTestingAccount : Account | |
{ | |
protected override ITransactionsRepository CreateTransactionRepository() | |
{ | |
return new FakeTransactionRepository(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment