• 工厂模式,单例模式


    工厂模式:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
    设计模式:工厂模式,单例模式
    《大话设计模式》
    
    计算器
    class JiSuanQi
    {
    private int a;
    
    public int A
    {
    get { return a; }
    set { a = value; }
    }
    private int b;
    
    public int B
    {
    get { return b; }
    set { b = value; }
    }
    
    //加法
    public int Jia()
    {
    return a + b;
    }
    
    //减法
    public int Jian()
    {
    return a - b;
    }
    
    //乘法
    public int Cheng()
    {
    return a * b;
    }
    
    }
    
    改进计算器
    public class JiSuan
    {
    private int a;
    
    public int A
    {
    get { return a; }
    set { a = value; }
    }
    private int b;
    
    public int B
    {
    get { return b; }
    set { b = value; }
    }
    
    
    public virtual int YunSuan()
    {
    return 0;
    }
    
    }
    
    //加法类
    public class Jia:JiSuan
    {
    public override int YunSuan()
    {
    return base.A + base.B;
    }
    }
    
    //减法类
    public class Jian : JiSuan
    {
    public override int YunSuan()
    {
    return base.A - base.B;
    }
    }
    
    
    //工厂类
    public class GongChang
    {
    public static JiSuan DuiXiang(string s)
    {
    switch(s)
    {
    case "+":
    return new Jia();
    break;
    case "-":
    return new Jian();
    break;
    case "*":
    return new Cheng();
    break;
    default:
    return new Jia();
    break;
    
    }
    }
    }



    单例模式:



    控制一个类只能实例化一个对象
    
    class Test
    {
     public string name;
    }
    
    数据访问类
    class DBDA
    {
    public string host;
    public string database;
    
    静态成员,用来存储该类的对象
    public static DBDA db = null;
    
    让该类不能被实例化
    private DBDA()
    {
    }
    
    提供一个造对象的方法,控制只能造一个对象
    public static DBDA DuiXiang()
    {
    if (db == null)
    {
    db = new DBDA();
    }
    
    return db;
    }
    }
    
    定义委托
    public delegate void SuiBian(string s);
    
    class Program
    {
    static void Main(string[] args)
    {
    Test t1 = new Test();
    Test t2 = new Test();
    
    DBDA db = DBDA.DuiXiang();
    db.host = "localhost";
    DBDA db1 = DBDA.DuiXiang();
    
    
    委托
    把方法参数化
    SuiBian s = China;
    
    s += America; //+=是绑定方法 -=去掉一个方法
    
    事件
    事件就是一种特殊的委托
    
    
    调用语音方法
    Speak(s);
    
     
    
    Console.WriteLine();
    Console.ReadLine();
    
    
    面向对象三大特性
    设计模式
    }
    
    语音功能的方法
    static void Speak(SuiBian yu)
    {
    yu("张三");
    
    if (n == 0)
    {
     America();
    }
    else if (n == 1)
    {
     China();
    }
    else if (n == 2)
    {
    HanYu();
    }
    
    }
    
    语音包
    public static void America(string s)
    {
    Console.WriteLine(s+"hello");
    }
    static void China(string s)
    {
    Console.WriteLine(s+"你好");
    }
    static void HanYu(string s)
    {
    Console.WriteLine(s+"bjdkaj");
    }
    
    
    }
    }
  • 相关阅读:
    python实战之爬取喜玛拉雅专辑信息
    python工具之exccel模板生成报表
    python模拟登录博客园(附:问题求教)
    maven 三个基本插件 clean dependency compiler
    oracle 安装注意
    mybatis generate 自动生成 entity dao 和 xml 文件
    mybatis 打印sql 语句
    mybatis 关联查询 association
    oracle 多级菜单查询 。start with connect by prior
    mybatis 控制台打印sql
  • 原文地址:https://www.cnblogs.com/zzzy0828/p/5794316.html
Copyright © 2020-2023  润新知