• 设计模式总结


    设计模式总结

    1.单例模式

    单例模式的特点:
    a:一个单例只有一个实例
    b:单例必须自己创建一个唯一的实例
    c:单例必须给其他对象提供这个实例

    单例的应用
    a:每台计算机都有若干个打印机,但只能有一个pr 避免两个打印作业同时输出打印机
    b:一个具有主动编号的表可以多个用户使用,一个数据库中只有一个地方分配下一个主键编号,否则会出现主键重复

    public class Singleton
    {
    private static Singleton _instance = null;
    private Singleton(){}
    public static Singleton CreateInstance()
    {
    if(_instance == null)
    {
    _instance = new Singleton();
    }
    return _instance;
    }
    }

    2.简单工厂 静态工厂方法 不属于23中设计模式
    1.简而言之,就是将父类创建对象的职责转移给工厂来制造。
    2.基本过程
    1。场景 人去旅游并且搭乘交通工具
    人的类 并且创建一个旅游的方法
    创建一个交通工具的接口,自行车,车,分别继承接口
    创建一个工厂
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Configuration;

    namespace ConsoleApplication2
    {
    public class Factory
    {
    public static IRunable CreatVehicle()
    {
    string vehicleType = ConfigurationManager.AppSettings["VehicleType"];
    IRunable vehicle = null;
    switch (vehicleType)
    {
    case "Car": vehicle = new Car(); break;
    case "Bike": vehicle = new Bike(); break;
    case "Plane": vehicle = new Plane(); break;
    }
    return vehicle;
    }
    }


    private IRunable vehicle=Factory.CreatVehicle();并且将值传给vehicle

    using System;
    using System.Collections.Generic; //人类
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication2
    {
    public class Person
    {
    private IRunable vehicle=Factory.CreatVehicle();
    public void Travel()
    {
    Console.WriteLine("人在旅行....");
    vehicle.Run();
    }
    }
    }
    }


    2.抽象工厂模式
    1.符合开闭原则
    2.、当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
    3.抽象工厂隔离了具体类的生产
    4.
    多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。
    一个抽象工厂类,可以派生出多个具体工厂类。
    每个具体工厂类可以创建多个具体产品类的实例。
    缺点:
    增加新产品结构复杂,需要修改抽象工厂和所有工厂类


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Configuration;
    using System.Reflection;
    namespace ConsoleApplication2
    {
    public abstract class AbstractFactory
    {
    public abstract IRunable CreatVehicle(); //具体的组分类
    public abstract IShotable CreatWeapon();
    public static AbstractFactory ChooseFactory()
    {
    string factoryName = ConfigurationManager.AppSettings["FactoryName"];
    //基于反射技术根据类名动态创建该类的对象
    return Assembly.GetExecutingAssembly().CreateInstance("ConsoleApplication2." + factoryName) as AbstractFactory;

    }
    }
    }


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

    namespace ConsoleApplication2
    {
    public class Person
    {
    private AbstractFactory factory = AbstractFactory.ChooseFactory(); //调用抽象工厂
    public void Travel()
    {
    IRunable vehicle = factory.CreatVehicle();
    IShotable weapon = factory.CreatWeapon();
    Console.WriteLine("人在旅行....");
    vehicle.Run();
    weapon.Shot();
    }
    }
    }

  • 相关阅读:
    二十七、正则表达式补充
    二十六、python中json学习
    二十五、python中pickle序列学习(仅python语言中有)
    MongoDB系列
    产品经理思考
    摩拜数据产品
    龙珠直播之swot
    ahp层次分析法软件
    用户画像之门店用户类型的体系
    汽车后市场SWOT分析
  • 原文地址:https://www.cnblogs.com/liyiyong/p/5125754.html
Copyright © 2020-2023  润新知