ビルダー
// Builder encapsulates construction of other object. Building of the object can be done in multiple steps (methods)
public class ConfigurationBuilder
{
// Each method adds some configuration part to internally created Configuration object
void AddDbConfiguration(...);
void AddSmtpConfiguration(...);
void AddWebServicesConfiguration(...);
void AddWebServerConfiguration(...);
// Returns built configuration
Configuration GetConfiguration();
}
工場方式
// Factory method is declared in base class or interface. Subclass defines what type is created by factory method.
public interface ICacheProvider
{
ISession CreateCache(); // Don't have to return new instance each time - such decission is part of implementation in derived class.
}
public class InMemoryCacheProvider : ICacheProvider
{ ... }
public class DbStoredCacheProvider : ICacheProvider
{ ... }
// Client code
ICacheProvider provider = new InMemoryCacheProvider
ICache cache = provider.CreateCache();
抽象工場
// Abstract factory defines families of platform classes - you don't need to specify each platform class on the client.
public interface IDbPlatform
{
// It basically defines many factory methods for related classes
IDbConnection CreateConnection();
IDbCommand CreateCommand();
...
}
// Abstract factory implementation - single class defines whole platform
public class OraclePlatfrom : IDbPlatform
{ ... }
public class MySqlPlatform : IDbPlatform
{ ... }
// Client code:
IDbPlatform platform = new OraclePlatform();
IConnection connection = platform.CreateConnection(); // Automatically Oracle related
...