Last active
November 26, 2015 07:25
-
-
Save osya/7884ccd1e7d72622850b to your computer and use it in GitHub Desktop.
Composition & Aggregation #CSharp
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
class CompositeCustomService | |
{ | |
// Композиция | |
private readonly CustomRepository _repository | |
= new CustomRepository(); | |
public void DoSomething() | |
{ | |
// Используем _repository | |
} | |
} | |
class AggregatedCustomService | |
{ | |
// Агрегация | |
private readonly AbstractRepository _repository; | |
public AggregatedCustomService(AbstractRepository repository) | |
{ | |
_repository = repository; | |
} | |
public void DoSomething() | |
{ | |
// Используем _repository | |
} | |
} | |
internal interface IRepositoryFactory | |
{ | |
AbstractRepository Create(); | |
} | |
class CustomService | |
{ | |
// Композиция | |
private readonly IRepositoryFactory _repositoryFactory; | |
public CustomService(IRepositoryFactory repositoryFactory) | |
{ | |
_repositoryFactory = repositoryFactory; | |
} | |
public void DoSomething() | |
{ | |
var repository = _repositoryFactory.Create(); | |
// Используем созданный AbstractRepository | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment