美文网首页
学习设计模式 Day1 2020-03-30

学习设计模式 Day1 2020-03-30

作者: Taraks | 来源:发表于2020-03-30 22:38 被阅读0次

工厂模式 Factory Pattern

简述: 由工厂类决定实例化哪种实例,延迟创建过程,主要解决接口选择的问题。

Step1: 实现一个interface ICar

using System.Xml.Serialization;

namespace LearnDesignPatterns.Patterns.FactoryPattern
{
    public interface ICar
    {
        public string GetName();
    }
}

Step2: 实现ICar接口的实体类

namespace LearnDesignPatterns.Patterns.FactoryPattern
{
    public class BMW420i : ICar
    {
        public string Name
        {
            get => "BMW 420i";
        }
        public string GetName()
        {
            return Name;
        }
    }
    
    public class TeslaModel3 : ICar
    {
        public string Name
        {
            get => "Tesla Model III";
        }
        public string GetName()
        {
            return Name;
        }
    }
}

Step3: 创建工厂

namespace LearnDesignPatterns.Patterns.FactoryPattern
{
    public class CarFactory
    {
        public ICar GetCar(string carType)
        {
            return carType switch
            {
                "BMW" => (ICar) new BMW420i(),
                "TESLA" => new TeslaModel3(),
                _ => null
            };
        }
    }
}

Step4: 执行&Log

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;

namespace LearnDesignPatterns.Patterns.FactoryPattern
{
    public class FactoryPatternExecutor : IExecutor
    {
        public void Execute()
        {
            var carFactory = new CarFactory();
            var cars = new List<ICar>()
            {
                carFactory.GetCar("BMW"),
                carFactory.GetCar("TESLA"),
                carFactory.GetCar("TOYOTA")
            };
            foreach (var s in cars) Log(s);
        }
        private static void Log(ICar car)
        {
            Console.WriteLine(car == null
                ? $"[Car Factory Info] This car product failed"
                : $"[Car Factory Info] This car is {car.GetName()}");
        }
    }
}

Output:

/usr/local/share/dotnet/dotnet /Users/garry/RiderProjects/LearnDesignPatterns/LearnDesignPatterns/bin/Debug/netcoreapp3.1/LearnDesignPatterns.dll
[Car Factory Info] This car is BMW 420i
[Car Factory Info] This car is Tesla Model III
[Car Factory Info] This car product failed

Process finished with exit code 0.

点击查看源代码

相关文章

  • 学习设计模式 Day1 2020-03-30

    工厂模式 Factory Pattern 简述: 由工厂类决定实例化哪种实例,延迟创建过程,主要解决接口选择的问题...

  • 装饰者模式——IO流运用

    推荐博客Java设计模式学习09Java设计模式学习09Java设计模式学习09 装饰者模式还是比较难懂的。。。。...

  • 设计模式之单例模式

    单例设计模式全解析 在学习设计模式时,单例设计模式应该是学习的第一个设计模式,单例设计模式也是“公认”最简单的设计...

  • 设计模式之前话一

    在进入具体的设计模式之前,我们有必要知道,设计模式是什么?为什么要学习设计模式?怎么学习设计模式?只有了解了这些问...

  • 设计模式:抽象工厂模式

    前言 来啦老铁! 笔者正在学习常见的设计模式,且将设计模式系列学习文章归入 “设计模式学习[https://www...

  • 设计模式:建造者模式

    前言 来啦老铁! 笔者正在学习常见的设计模式,且将设计模式系列学习文章归入 “设计模式学习[https://www...

  • 设计模式:原型模式

    前言 来啦老铁! 笔者正在学习常见的设计模式,且将设计模式系列学习文章归入 “设计模式学习[https://www...

  • 设计模式:空对象模式

    前言 来啦老铁! 笔者正在学习常见的设计模式,且将设计模式系列学习文章归入 “设计模式学习[https://www...

  • 设计模式:单例模式

    前言 来啦老铁! 笔者正在学习常见的设计模式,且将设计模式系列学习文章归入 “设计模式学习[https://www...

  • 学习Head First设计模式Day1

    Java设计模式之设计模式 策略模式:策略模式定义了算法簇,分别封装起来,让他们之间可以互相替换,此设计模式让算法...

网友评论

      本文标题:学习设计模式 Day1 2020-03-30

      本文链接:https://www.haomeiwen.com/subject/wfobuhtx.html