zoukankan      html  css  js  c++  java
  • 简单工厂模式+泛型

    简单工厂模式 UML:

     

    1.我们首先定义一个工厂模式的核心逻辑类:ProductFactoryClass,

    2.然后抽象产品类:ProductClass1,ProductClass2

    3.接口:IProduct

     没啥好说的 直接贴代码吧:

    1.外部调用时:

    1             SimpleFactory<Product1> product = new SimpleFactory<Product1>();
    2             product.CreateShow().Product(20,200);

    2.入口:

     1    /// <summary>
     2     /// 简单工厂 的实现层 
     3     /// </summary>
     4     public class SimpleFactory<T> where T : new()
     5     {
     6         private static T _instance;     
     7         public  T CreateShow()
     8         {
     9             if (_instance == null)     
    10             {
    11                 _instance = new T();   
    12             }
    13             return _instance;
    14         }
    15     }

    3.抽象商品类:

     1     public class Product1 : IProduct
     2     {
     3         public void Product(int count, double price)
     4         {
     5             Console.WriteLine("Total:{0}", count * price);
     6             Console.ReadKey();
     7         }
     8     }
     9     public class Product2: IProduct
    10     {
    11         public void Product(int count, double price)
    12         {
    13             Console.WriteLine("Total:{0}", count * price*2);
    14             Console.ReadKey();
    15         }
    16     }

    4.接口:

    1     public interface IProduct
    2     {
    3         public void Product(int days, double price);
    4     }
  • 相关阅读:
    C++类中的封装-9
    递归神经网络
    卷积神经网络
    Tensorflow人工智能神经网络学习
    右键弹出菜单 并动态新增菜单子项
    sql 列转行
    sql 删除表中某字段的重复数据
    疑难问题解决
    jquery ashx交互 返回list 循环json输入信息
    jquery 控件赋值
  • 原文地址:https://www.cnblogs.com/LY-NET/p/11876782.html
Copyright © 2011-2022 走看看