• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C#设计模式——工厂方法模式Factory Method Pattern

武飞扬头像
待续,,
帮助1

工厂方法模式


工厂方法模式->创建型设计模式

定义:定义一个创建对象的接口,但由子类决定需要实例化那一个类。(工厂模式是我们常用的实例化对象模式了,是用工厂方法代替new 操作的一种模式,简单工厂的特点就是“简单粗暴”,通过一个含参的工厂方法,我们可以实例化任何产品类。)

代码

  1.  
    namespace ConsoleApp1
  2.  
    {
  3.  
    public class Program
  4.  
    {
  5.  
    static void Main(String[] args)
  6.  
    {
  7.  
    Console.WriteLine("选择形状绘制:");
  8.  
    shape sh1 = ShapeFactory.GetShape(ShapeType.Square);
  9.  
    sh1.draw();
  10.  
    shape sh2 = ShapeFactory.GetShape(ShapeType.Circle);
  11.  
    sh2.draw();
  12.  
    shape sh3 = ShapeFactory.GetShape(ShapeType.Rectangle);
  13.  
    sh3.draw();
  14.  
    Console.Read();
  15.  
    }
  16.  
    }
  17.  
    /// <summary>
  18.  
    /// 形状 接口
  19.  
    /// </summary>
  20.  
    public interface shape
  21.  
    {
  22.  
    void draw();
  23.  
    }
  24.  
    /// <summary>
  25.  
    /// 矩形
  26.  
    /// </summary>
  27.  
    public class Rectangle : shape
  28.  
    {
  29.  
    public void draw()
  30.  
    {
  31.  
    Console.WriteLine("矩形实现形状的接口方法draw()。");
  32.  
    }
  33.  
    }
  34.  
    /// <summary>
  35.  
    /// 正方形
  36.  
    /// </summary>
  37.  
    public class Square : shape
  38.  
    {
  39.  
    public void draw()
  40.  
    {
  41.  
    Console.WriteLine("正方形实现形状的接口方法draw()。");
  42.  
    }
  43.  
    }/// <summary>
  44.  
    /// 圆形
  45.  
    /// </summary>
  46.  
    public class Circle : shape
  47.  
    {
  48.  
    public void draw()
  49.  
    {
  50.  
    Console.WriteLine("圆形实现形状的接口方法draw()。");
  51.  
    }
  52.  
    }
  53.  
    /// <summary>
  54.  
    /// 枚举 形状类型
  55.  
    /// </summary>
  56.  
    public enum ShapeType
  57.  
    {
  58.  
    Rectangle,
  59.  
    Square,
  60.  
    Circle
  61.  
    }
  62.  
    /// <summary>
  63.  
    /// 形状工厂
  64.  
    /// </summary>
  65.  
    public class ShapeFactory
  66.  
    {
  67.  
    /// <summary>
  68.  
    /// 通过参数决定创建的形状类型
  69.  
    /// </summary>
  70.  
    /// <param name="type"></param>
  71.  
    /// <returns></returns>
  72.  
    public static shape GetShape(ShapeType type)
  73.  
    {
  74.  
    switch (type)
  75.  
    {
  76.  
    case ShapeType.Rectangle:
  77.  
    return new Rectangle();
  78.  
    case ShapeType.Square:
  79.  
    return new Square();
  80.  
    case ShapeType.Circle:
  81.  
    return new Circle();
  82.  
    default:
  83.  
    return new Square();
  84.  
    }
  85.  
    }
  86.  
    }
  87.  
    }
学新通

结果

学新通

特点:工厂方法模式中,具体的工厂只是生产具体的某一个产品。而抽象工厂模式的具体的工厂可以生产族产品(例如奔驰轿车、奔驰SUV、奔驰MPV)。

区别:抽象工厂模式与工厂方法模式最大的区别在于,工厂方法模式针对的是一种产品的继承结构,而抽象工厂模式需要面对多个产品的继承结构。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghegic
系列文章
更多 icon
同类精品
更多 icon
继续加载