C#面向对象17 23种设计模式

本文介绍了一种常用的设计模式——简单工厂模式,并通过.NET框架下的具体示例代码展示了如何使用该模式来实现对象的创建。简单工厂模式允许客户端通过提供一个字符串参数来决定创建哪个具体的子类实例。

1.简单工厂模式

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

namespace 简单工厂
{
    class Program
    {
        static void Main(string[] args)
        {
            string tt= Console.ReadLine();
            Notabook tb = Getbook(tt);
            tb.Say();
            Console.ReadKey();
        }

        /// <summary>
        /// 简单工厂核心,根据输入 创建对象赋值给父类~
        /// </summary>
        /// <param name="style"></param>
        /// <returns></returns>
        public static Notabook Getbook(string style)
        {
            Notabook tb = null;
            switch(style)
            {
                case "lenvon":
                    tb=new lenvon();
                    break;
                case "IBM":
                    tb = new IBM();
                    break;
                case "DELL":
                    tb = new DELL();
                    break;
            }
            return tb;
        }
    }

    public abstract class Notabook
    {
        public abstract void Say();
    }

    public class lenvon: Notabook
    {
        public override void Say()
        {
            Console.WriteLine("this is lenvon!");
        }
    }

    public class IBM:Notabook
    {
        public override void Say()
        {
            Console.WriteLine("this is IBM!");
        }
    }

    public class DELL:Notabook
    {
        public override void Say()
        {
            Console.WriteLine("this is DELL!");
        }
    }
}

 

转载于:https://www.cnblogs.com/youguess/p/8662170.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值