设计模式(Design Pattern)

简介

设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。

设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了重用代码、让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦的一块块砖石一样。项目中合理地运用设计模式可以完美地解决很多问题,每种模式在现实中都有相应的原理来与之对应,每种模式都描述了一个在我们周围不断重复发生的问题,以及该问题的核心解决方案,这也是设计模式能被广泛应用的原因。

在 1994 年,由 Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides 四人合著出版了一本名为 Design Patterns - Elements of Reusable Object-Oriented Software(中文译名:设计模式 - 可复用的面向对象软件元素) 的书,该书首次提到了软件开发中设计模式的概念。
四位作者合称 GOF(四人帮,全拼 Gang of Four)。他们所提出的设计模式主要是基于以下的面向对象设计原则。

  • 对接口编程而不是对实现编程。
  • 优先使用对象组合而不是继承。

设计模式的类型

在这里插入图片描述

设计模式之间的关系

在这里插入图片描述

面向对象的六大原则

链接
设计模式:面向对象设计的六大原则

  1. 单一职责原则——SRP
  2. 开闭原则——OCP
  3. 里式替换原则——LSP
  4. 依赖倒置原则——DIP
  5. 接口隔离原则——ISP
  6. 迪米特原则——LOD

创建型模式

工厂模式

在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

意图:定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。
主要解决:主要解决接口选择的问题。
何时使用:我们明确地计划不同条件下创建不同实例时。
如何解决:让其子类实现工厂接口,返回的也是一个抽象的产品。
关键代码:创建过程在其子类执行。

优点:

  1. 一个调用者想创建一个对象,只要知道其名称就可以了。
  2. 扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。
  3. 屏蔽产品的具体实现,调用者只关心产品的接口。

缺点:
每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。这并不是什么好事。
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPattern.FactoryPattern
{
    public interface IShape
    {
        void Draw();
    }
    public class Rectangle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("Rectangle");
        }
    }
    public class Square : IShape
    {
        public void Draw()
        {
            Console.WriteLine("Square");
        }
    }
    public class Circle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("Circle");
        }
    }
    public enum ShapeType
    {
        Rectangle,
        Square,
        Circle,
    }
    public class ShapeFactory
    {
        public IShape GetShape(ShapeType type)
        {
            switch (type)
            {
                case ShapeType.Rectangle:
                    return new Rectangle();
                case ShapeType.Square:
                    return new Square();
                case ShapeType.Circle:
                    return new Circle();
                default:
                    return null;
            }
        }
    }
    class FactoryPatternDemo
    {
        static void Main(string[] args)
        {
            ShapeFactory factory = new ShapeFactory();

            IShape shape1 = factory.GetShape(ShapeType.Rectangle);
            shape1.Draw();
            IShape shape2 = factory.GetShape(ShapeType.Square);
            shape2.Draw();
            IShape shape3 = factory.GetShape(ShapeType.Circle);
            shape3.Draw();
        }
    }
}
Rectangle
Square
Circle
请按任意键继续. . .

抽象工厂模式

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

何时使用:系统的产品有多于一个的产品族,而系统只消费其中某一族的产品。
如何解决:在一个产品族里面,定义多个产品。
优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPattern.AbstractFactoryPattern
{
    public interface IShape
    {
        void Draw();
    }
    public class Rectangle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("Rectangle");
        }
    }
    public class Square : IShape
    {
        public void Draw()
        {
            Console.WriteLine("Square");
        }
    }
    public class Circle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("Circle");
        }
    }
    public interface IColor
    {
        void Fill();
    }
    public class Red : IColor
    {
        public void Fill()
        {
            Console.WriteLine("Red");
        }
    }
    public class Green : IColor
    {
        public void Fill()
        {
            Console.WriteLine("Green");
        }
    }
    public class Blue : IColor
    {
        public void Fill()
        {
            Console.WriteLine("Blue");
        }
    }
    public enum ShapeType
    {
        Rectangle,
        Square,
        Circle,
    }
    public enum ColorType
    {
        Red,
        Green,
        Blue,
    }
    public abstract class AbstractFactory
    {
        public abstract IShape GetShape(ShapeType shape);
        public abstract IColor GetColor(ColorType color);
    }
    public class ShapeFactory : AbstractFactory
    {
        public override IShape GetShape(ShapeType shape)
        {
            switch (shape)
            {
                case ShapeType.Rectangle:
                    return new Rectangle();
                case ShapeType.Square:
                    return new Square();
                case ShapeType.Circle:
                    return new Circle();
                default:
                    return null;
            }
        }
        public override IColor GetColor(ColorType color)
        {
            return null;
        }
    }
    public class ColorFactory : AbstractFactory
    {
        public override IShape GetShape(ShapeType shape)
        {
            return null;
        }
        public override IColor GetColor(ColorType color)
        {
            switch (color)
            {
                case ColorType.Red:
                    return new Red();
                case ColorType.Green:
                    return new Green();
                case ColorType.Blue:
                    return new Blue();
                default:
                    return null;
            }
        }
    }
    public enum ChoiceType
    {
        Shape,
        Color,
    }
    public class FactoryProducer
    {
        public static AbstractFactory GetFactory(ChoiceType choice)
        {
            switch (choice)
            {
                case ChoiceType.Shape:
                    return new ShapeFactory();
                case ChoiceType.Color:
                    return new ColorFactory();
                default:
                    return null;
            }
        }
    }
    class AbstractFactoryPatternDemo
    {
        static void Main(string[] args)
        {
            AbstractFactory shapeFactory = FactoryProducer.GetFactory(ChoiceType.Shape);
            IShape shape1 = shapeFactory.GetShape(ShapeType.Rectangle);
            shape1.Draw();
            IShape shape2 = shapeFactory.GetShape(ShapeType.Square);
            shape2.Draw();
            IShape shape3 = shapeFactory.GetShape(ShapeType.Circle);
            shape3.Draw();

            AbstractFactory colorFactory = FactoryProducer.GetFactory(ChoiceType.Color);
            IColor color1 = colorFactory.GetColor(ColorType.Red);
            color1.Fill();
            IColor color2 = colorFactory.GetColor(ColorType.Green);
            color2.Fill();
            IColor color3 = colorFactory.GetColor(ColorType.Blue);
            color3.Fill();
        }
    }
}
Rectangle
Square
Circle
Red
Green
Blue
请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值