Week 3
单例模式
在单例模式中,一个class只能生成 唯一的一个对象!!
public class SMemoryCache
{
private static SMemoryCache cache;private Dictionary<string, string> datas;//key: uid value : 12388// pwd : 93898493//一定要初始化 dictionaryprivate SMemoryCache(){
datas = new Dictionary<string, string>();}//存值public void SetValue(string key, string value){
datas[key] = value;}//取值public string GetValue(string key){
if (datas.ContainsKey(key)){
return datas[key];}return "";}//构造单例public static SMemoryCache GetInstance(){
if (cache == null){
cache = new SMemoryCache();}return cache;}
}
注意:以上实现的并不支持多线程
- 也可以在c#利用静态构造函数 实现单例
private static SMemoryCache cache;private Dictionary<string, string> datas; //key: uid value : 12388 // pwd : 93898493 static SMemoryCache() { cache = new SMemoryCache(); }private SMemoryCache() { datas = new Dictionary<string, string>(); }
调用方式:
//HomeController 下:
public ActionResult SetValue(string key,string value)
{
var cache = SMemoryCache.GetInstance();cache.SetValue(key, value);return Content("true");
}public ActionResult GetValue(string key)
{
var cache = SMemoryCache.GetInstance();string value = cache.GetValue(key);return Content(value);
}
tips:这里的GetInstance()永远只会返回同一个实例
简单工厂、工厂模式、抽象工厂
工厂模式主要是负责 对象的创建
这样的原因有很多,绝大多数原因就是对上层的使用者隔离对象创建的过程;或者是对象创建的过程复杂,使用者不容易掌握;或者是对象创建要满足某种条件**,这些条件是业务的需求也好,是系统约束也好,没有必要让上层使用者掌握**,增加别人开发的难度。
- 简单工厂:通过TestBank.CreateBank生成不同的Bank
- 缺点:开闭规则不符合 每次要修改if else
public class TestBank
{
public static ISBank CreateBank(string bankName){
if (bankName == "ncwu"){
return new NcwuBank(1223);}else if (bankName == "abc"){
return new ABCBank("ddfda");}return default;}
}/// <summary>
/// 1定义了 银行的抽象
/// </summary>
public abstract class ISBank
{
private double maxAmount;private double balence;protected double MaxAmount {
get => maxAmount; set => maxAmount = value; }protected double Balence {
get => balence; set => balence = value; }//public double Balence { get => balence; }/// <summary>/// 方法 存钱/// </summary>/// <param name="amount"></param>public abstract void PayIn(double amount);
}/// <summary>
/// 1.1 实现 ncwu的银行
/// </summary>
public class NcwuBank : ISBank
{
public NcwuBank(double maxAmount){
this.MaxAmount = maxAmount;Console.WriteLine("create ncwu!");}public override void PayIn(double amount){
Balence += amount;}
}/// <summary>
/// 实现了 abc的银行
/// </summary>
public class ABCBank : ISBank
{
private string masterName;public ABCBank(string masterName){
this.masterName = masterName;Console.WriteLine("create abc!");}public override void PayIn(double amount){
Balence += amount * 2;}
}
调用:
var ncwuBank = TestBank.CreateBank("ncwu");
var abcBank = TestBank.CreateBank("abc");
- 变为工厂模式
/// <summary>
/// 2抽象 一个工厂 --》生产银行
/// </summary>
public interface IFactoryBank
{
ISBank CreateBank();
}/// <summary>
/// 2.1ncwu 工厂 --》 ncwu的银行
/// </summary>
public class NcwuFacotry : IFactoryBank
{
public ISBank CreateBank(){
return new NcwuBank(398498);}
}/// <summary>
/// 2.2 abc的工厂 ---》 abc的银行
/// </summary>
public class AbcFactory : IFactoryBank
{
public ISBank CreateBank(){
return new ABCBank("Dave");}
}
调用:
IFactoryBank factoryBank = new NcwuFacotry();
var bank = factoryBank.CreateBank();
- 抽象工厂:让一个Factory不单纯能生产一个Product,还能生产其他的Product
- 在抽象工厂模式中,每一个具体工厂都提供了多个工厂方法用于产生多种不同类型的对象
class TFactory
{
public abstract class Button{
public abstract void Show();}public abstract class Checkbox{
public abstract void Show();}public class WinBotton : Button{
public override void Show(){
Console.WriteLine("win btn click");}}public class WinCheckbox : Checkbox{
public override void Show(){
Console.WriteLine("win checkbox click");}}public class MacBotton : Button{
public override void Show(){
Console.WriteLine("Mac btn click");}}public class MacCheckbox : Checkbox{
public override void Show(){
Console.WriteLine("Mac checkbox click");}}public interface IGuiFactory{
Button CreateBotton();Checkbox CreateCheckbox();}public class WinFactory : IGuiFactory{
public Button CreateBotton(){
return new WinBotton();}public Checkbox CreateCheckbox(){
return new WinCheckbox();}}public class MacFactory : IGuiFactory{
public Button CreateBotton(){
return new MacBotton();}public Checkbox CreateCheckbox(){
return new MacCheckbox();}}
}
调用:
IGuiFactory guiFactory = new WinFactory();
var button = guiFactory.CreateBotton();
button.Show();
var checkedBox = guiFactory.CreateCheckbox();
checkedBox.Show();
补充泛型 Nullable(显式、隐式)
出现原因:数据库中值类型 与 C# 中值类型 的值的不一致
**一个空 ** 、 一个不空
class SNullable<T>
{
private bool hasValue= false;private T value;public SNullable(){
this.hasValue = false;}public SNullable(T value){
this.hasValue = true;this.value = value;}public T Value{
get{
if (!hasValue){
throw new InvalidOperationException("empty!");}return value;}}public bool HasValue{
get{
return hasValue;}}public override string ToString(){
return Value.ToString();}/// <summary>/// 隐式转换 nullable = a;/// </summary>/// <param name="value"></param>public static implicit operator SNullable<T>(T value){
return new SNullable<T>(value);}/// <summary>/// 显示转换 a = (T)nullable/// </summary>/// <param name="sNullable"></param>public static explicit operator T(SNullable<T> sNullable){
return sNullable.Value;}}
调用:
-
直接不赋值,给null,调用会return Exception
SNullable<int> sNullable1 = new SNullable<int>(); Console.WriteLine(sNullable1.HasValue); _ = sNullable1.Value;
-
显式、隐式 转换
int a = 1; double c = a;SNullable<int> sNullable =a; int b; b = (int)sNullable;Console.WriteLine(sNullable.ToString());
c# 中 Nullabe的简写:
?运算符:
Int? a; == Nullable<int> a
https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/nullable.cs