# 抽象工厂模式
属于创建型模式。
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
缺点:难以扩展
适用性:
- 一个系统要独立于它的产品的创建、组合和表示时(创建与表示分离)
- 一个系统要由多个产品系列中的一个来配置时(在多个系列中选择一个系列)
- 当你要强调一系列相关的产品对象的设计以便进行联合使用时(同系列中有多种产品)
- 当你提供一个产品类库,而只想显示它们的接口而不是实现时(客户端依赖抽象)
# 类图
- 数据的储存分为客户端和服务端
- 需要储存的数据的类型有很多(本文以 User 和 Character 为例)
# 代码
# 用于数据储存的类
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| public class User |
| { |
| } |
| } |
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| public class Character |
| { |
| } |
| } |
# 需要用工厂创建对象的类
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| public interface IUserDao |
| { |
| |
| |
| |
| |
| void AddUser(User user); |
| } |
| } |
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| |
| |
| |
| |
| |
| public class UserClientDao : IUserDao |
| { |
| public void AddUser(User user) |
| { |
| Console.WriteLine("UserClientDao_AddUser"); |
| } |
| } |
| } |
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| |
| |
| |
| internal class UserSeverDao : IUserDao |
| { |
| public void AddUser(User user) |
| { |
| Console.WriteLine("UserSeverDao_AddUser"); |
| } |
| } |
| } |
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| public interface ICharacterDao |
| { |
| |
| |
| |
| |
| public void AddCharacter(Character character); |
| } |
| } |
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| public class CharacterClientDao : ICharacterDao |
| { |
| public void AddCharacter(Character character) |
| { |
| Console.WriteLine("CharacterClientDao_AddCharacter"); |
| } |
| } |
| } |
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| public class CharacterSeverDao : ICharacterDao |
| { |
| public void AddCharacter(Character character) |
| { |
| Console.WriteLine("CharacterSeverDao_AddCharacter"); |
| } |
| } |
| } |
# 添加数据的方式(枚举)
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| |
| |
| |
| public enum AddTypes |
| { |
| Client, |
| Sever |
| } |
| |
| |
| |
| |
| public class AddType |
| { |
| public static AddTypes CurrentType = AddTypes.Client; |
| } |
| } |
# 抽象工厂
- 利用反射动态创建具体的工厂
- 提供创建对象的方法,让子类实现具体的创建
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| |
| |
| |
| public abstract class DaoFactory |
| { |
| |
| |
| |
| public abstract IUserDao UserDao { get; } |
| |
| |
| |
| |
| public abstract ICharacterDao CharacterDao { get; } |
| |
| |
| |
| |
| private static AddTypes TargetType = AddType.CurrentType; |
| |
| |
| |
| |
| private static DaoFactory instance; |
| |
| |
| |
| public static DaoFactory Instance |
| { |
| get |
| { |
| |
| |
| if (instance == null || TargetType != AddType.CurrentType) |
| { |
| TargetType = AddType.CurrentType; |
| |
| Type type = Type.GetType("CreationalPatterns_AbstractFactoryPattern." + AddType.CurrentType + "DaoFactory"); |
| instance = Activator.CreateInstance(type) as DaoFactory; |
| } |
| |
| return instance; |
| } |
| } |
| |
| } |
| } |
# 具体的工厂
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| |
| |
| |
| public class ClientDaoFactory : DaoFactory |
| { |
| public override IUserDao UserDao |
| { |
| get |
| { |
| return new UserClientDao(); |
| } |
| } |
| |
| public override ICharacterDao CharacterDao |
| { |
| get |
| { |
| return new CharacterClientDao(); |
| } |
| } |
| } |
| } |
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| |
| |
| |
| public class SeverDaoFactory : DaoFactory |
| { |
| public override IUserDao UserDao |
| { |
| get |
| { |
| return new UserSeverDao(); |
| } |
| } |
| |
| public override ICharacterDao CharacterDao |
| { |
| get |
| { |
| return new CharacterSeverDao(); |
| } |
| } |
| } |
| } |
# 测试
| namespace CreationalPatterns_AbstractFactoryPattern |
| { |
| public class Program |
| { |
| private static void Main(string[] args) |
| { |
| |
| DaoFactory daoFactory = DaoFactory.Instance; |
| |
| IUserDao userDao = daoFactory.UserDao; |
| |
| userDao.AddUser(new User()); |
| |
| |
| * 选择系列 DaoFactory.Instance new XXXDaoFactory |
| * 选择数据 DaoFactory.UserDao new UserXXXDao |
| * 选择操作 IUserDao.AddUser () UserXXXDao.Add () |
| */ |
| |
| AddType.CurrentType = AddTypes.Sever; |
| DaoFactory.Instance.CharacterDao.AddCharacter(new Character()); |
| } |
| } |
| } |
运行结果:
UserClientDao_AddUser
CharacterSeverDao_AddCharacter