04_抽象工厂模式
抽象工厂模式是一种创建型设计模式,它提供一个接口用于创建一系列相关或相互依赖对象的工厂,而不需要指定具体的类。这种模式通过提供一个抽象的工厂接口,使得客户端可以创建一系列产品对象而无需关心具体的实现细节。
在抽象工厂模式中,通常会定义一个抽象工厂接口,其中包含多个方法用于创建不同类型的产品。具体的工厂类会实现这个接口,并实现具体的产品创建逻辑。客户端代码通过调用工厂方法来创建产品对象,而不需要了解具体的产品类。
抽象工厂模式适用于需要创建一系列相关对象并且这些对象之间存在一定的依赖关系的场景。它可以帮助降低代码耦合度,提高代码的可维护性和扩展性。
总的来说,抽象工厂模式可以帮助我们实现将对象的创建与使用分离,提供一种灵活的方式来创建一系列相关对象。
Product
/// <summary> /// 手机 /// </summary> public interface IPhone { /// <summary> /// 数量 /// </summary> int Amount { get; } /// <summary> /// 生产 /// </summary> /// <param name="amount"></param> /// <param name="days"></param> /// <returns></returns> int Produce(int amount,int days); /// <summary> /// 打印 /// </summary> void Write(); }
/// <summary> /// 电视 /// </summary> public interface ITV { /// <summary> /// 数量 /// </summary> int Amount { get; } /// <summary> /// 生产 /// </summary> /// <param name="amount"></param> /// <param name="days"></param> /// <returns></returns> int Produce(int amount, int days); /// <summary> /// 打印 /// </summary> void Print(); }
ConcreteProduct
苹果手机
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class AppleTV : ITV { public int Amount { get; private set; } public void Print() { Console.WriteLine($"苹果电视一共生产了{Amount}台"); } public int Produce(int amount, int days) { Amount = amount * days * 128; return Amount; } }
苹果电视
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class ApplePhone : IPhone { public int Amount {get;private set;} public int Produce(int amount, int days) { Amount = amount * days * 1000; return Amount; } public void Write() { Console.WriteLine($"苹果手机一共生产了{Amount}台"); } }
三星手机
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class SamsungPhone : IPhone { public int Amount {get;private set;} public int Produce(int amount, int days) { Amount = amount * days * 555; return Amount; } public void Write() { Console.WriteLine($"三星手机一共生产了{Amount}台"); } }
三星电视
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
internal class SamsungTV : ITV { public int Amount { get; private set; } public void Print() { Console.WriteLine($"三星电视一共生产了{Amount}台"); } public int Produce(int amount, int days) { Amount = amount * days * 250; return Amount; } }
Creator
/// <summary> /// 抽象工厂 /// </summary> public interface IFactory { ITV GetTV(); IPhone GetPhone(); }
ConcreteCreator
/// <summary> /// 苹果工厂 /// </summary> public class AppleFactory : IFactory { public IPhone GetPhone() { return new ApplePhone(); } public ITV GetTV() { return new AppleTV(); } }
/// <summary> /// 三星工厂 /// </summary> public class SamsungFactory : IFactory { public IPhone GetPhone() { return new SamsungPhone(); } public ITV GetTV() { return new SamsungTV(); } }
调用
public class Client { public void Start() { IFactory appleFactory = new AppleFactory(); DoStuff(appleFactory); IFactory samsungFactory = new SamsungFactory(); DoStuff(samsungFactory); } private void DoStuff(IFactory factory) { Console.WriteLine($"{factory.GetType().Name} 开始生产"); var phone = factory.GetPhone(); phone.Produce(99, 90); phone.Write(); var tv = factory.GetTV(); tv.Produce(88, 90); tv.Print(); Console.WriteLine($"{factory.GetType().Name} 生产结束"); } }
static void Main(string[] args) { new Client().Start(); Console.ReadKey(); }