Provider (Strategy) Pattern
Used heavily for defining contracts. Use specific suffixes to clarify intent:
Provider suffix: Use when the contract primarily allows the caller to get something.Reader and Writer interfaces: If a contract allows both getting and setting, split it into two interfaces to clarify intent.
Creational Patterns
Use patterns when object creation involves significant logic:
- Factory Pattern: When logic doesn't fit in a constructor.
- Create semantics: For items built in a single step.
- Build semantics: For items built up over multiple steps.
Null Object Pattern
When providing an implementation of an interface that does nothing, use the Null Object Pattern instead of returning null.
// Decomposing a Provider into Reader/Writer
public interface ICacheReader
{
bool TryGetObject(string key, out object value);
}
public interface ICacheWriter
{
void RemoveObjects(string keys);
void RemoveObject(string key);
void SetObject(string key, object obj);
void InsertObject(string key, object value, DateTime absoluteExpiration, TimeSpan slidingExpiration);
}
// Null Object Pattern
public interface ISomething
{
DateTime GetDateTime(string parameter);
}
public class NullSomething : ISomething
{
public DateTime GetDateTime(string parameter) => default(DateTime);
}