抽象工厂模式

The Abstract Factory Design Pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.

In simple worlds, we can say that the Abstract Factory is a super factory that creates other factories. This Abstract Factory is also called the Factory of Factories.That means the Abstract Factory design pattern provides an interface for creating families of related or depedent products but leaves the actual object creation to the concrete factory classes.

 

UML Class Diagram

 

Structure Code IN C#

 public interface IVehicle
    {
        void Run();
    }
    public interface IBike : IVehicle
    {
    }

    /// <summary>
    /// Concrete products are going to be created by corresponding concrete factories.
    /// </summary>
    public class RegularBike : IBike
    {
        public void Run()
        {
            Console.WriteLine($"This is {typeof(RegularBike)} runing");
        }
    }

    /// <summary>
    /// Concrete products are going to be created by corresponding concrete factories.
    /// </summary>
    public class SportBike : IBike
    {
        public void Run()
        {
            Console.WriteLine($"This is {typeof(SportBike)} runing");
        }
    }

    public interface ICar : IVehicle
    {
    }


    /// <summary>
    /// Concrete products are going to be created by corresponding concrete factories.
    /// </summary>
    public class RegularCar : ICar
    {
        public void Run()
        {
            Console.WriteLine($"This is {typeof(RegularCar)} runing");
        }
    }

    /// <summary>
    /// Concrete products are going to be created by corresponding concrete factories.
    /// </summary>
    public class SportCar : ICar
    {
        public void Run()
        {
            Console.WriteLine($"This is {typeof(SportCar)}  runing");
        }
    }
Product
// <summary>
    /// The AbstractFactory interface
    /// The Abstract Factory interface declares a set of methods that return different abstract products.
    /// These products are called a family
    /// A family of products my have server variants.
    /// </summary>
    public interface IVehicleFactory
    {
        IBike CreateBike();
        ICar CreateCar();
    }

    /// <summary>
    /// Concrete Factories produce a familiy of products that belong to a single variant.
    /// </summary>
    public class RegularVehicleFactory : IVehicleFactory
    {
        public IBike CreateBike()
        {
            return new RegularBike();
        }

        public ICar CreateCar()
        {
            return new RegularCar();
        }
    }

    /// <summary>
    /// Concrete Factories produce a familiy of products that belong to a single variant.
    /// </summary>
    public class SportVehicleFactory : IVehicleFactory
    {
        public IBike CreateBike()
        {
            return new SportBike();
        }

        public ICar CreateCar()
        {
            return new SportCar();
        }
    }
Factory
var regular = new RegularVehicleFactory();
var regularBike = regular.CreateBike();
regularBike.Run();

var sport  =new SportVehicleFactory();
var sportCar = sport.CreateCar();
sportCar.Run();
Client

 

Point To Remember:

  • Abstract Factory Design Pattern provides an interface for creating families of related dependet objects without specifying their concrete classes.
  • The Abstract Fatory Design Pattern provides a way to encapsulate a group of individual factories that have a common theme wihout specifiying their concrete classes.
  • The Abstract Factory Design Pattern is merely an extension of the factory method pattern or factory pattern, which allows you to create objects wihout being concreted about the actual class of the object being created.
  • Abstract Factory Design Pattern hiding some information and factory means which produces the products and pattern means a design.So, the Abstract factory design pattern is a software design pattern that provides a way to encapsulates a group of individual factories that have a common theme.

When to use Abstract Factory Design Pattern in Application Development?

  • An application where the Client is indenpedent or unaware of how the product classes are created and composed.
  • An application where we have to define multiple families of products.
  • An application where we have to define a group of classes that are desgined to be used together.

Differences between Abstract Factory and Factory Method Design Pattern:

  • Abstract Factory Design Pattern adds a layer of abstraction to the Factory Method Design Pattern.
  • The Abstract Factory Design Pattern implementation can have multiple factory methods.
  • Similiar products of a factory implementation are grouped in the Abstract factory.
  • The Abstract Factory Pattern uses object composition to decouple applications from specific implementations.
  • The Factory Method Pattern uses inheritance to decouple applications from specific implementations.

工厂方法模式是定义一个用于创建对象的接口,让子类决定实例化哪一个类。

抽象工厂模式(abstract Factory):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

优点:

    它让具体的创建过程与客户端分离,客户端是通过它们的抽象接口操作实例,产品的具体类名也被具体工厂的实现分离,不会出现在客户端。

 

所有在使用简单工厂的地方,都可以考虑用反射技术去除switch或if,解除分支判断带来的耦合。

 

一个程序员如果没有熬夜写程序的经历,不能算是一个好程序员,因为他没有痴迷过,所以他不会有很大成就。

编程是一门技术,更是一门艺术。

posted @ 2019-05-22 22:15  云霄宇霁  阅读(115)  评论(0编辑  收藏  举报