zoukankan      html  css  js  c++  java
  • 工厂模式,单例模式

    工厂模式:

    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");
    }
    
    
    }
    }
  • 相关阅读:
    Word pair Hu
    [bzoj1601] 灌水
    小木棍
    雇佣计划
    [Luogu1282] 多米诺骨牌
    [Luogu1216] 数字三角形
    [Luogu1734] 最大约数和
    [NOIp2008] 传纸条
    [Luogu1325] 雷达安装
    nginx
  • 原文地址:https://www.cnblogs.com/zzzy0828/p/5794316.html
Copyright © 2011-2022 走看看