通常建立一些管理物件
為了統一控管
不想被多個物件操作
我們可以利用 sealed class 的關鍵字
禁止其他類別繼承 sealed class
藉此操作物件內的資料
以下是 sealed class 的用法
sealed public class S3Mgr
{
....
}
只要繼承 S3Mgr 就會編譯錯誤
sealed public class S3Mgr
{
....
}
// A.cs 檔
partial class Program
{
public A _a = new A();
....
}
// B.cs 檔
partial class Program
{
public B _a = new B();
....
}
/// <summary>
/// 圖形抽像類
/// </summary>
public abstract class Graphics
{
public string Name { get; set; }
public Graphics(string name)
{
this.Name = name;
}
public abstract void Draw();
public abstract void Add(Graphics g);
public abstract void Remove(Graphics g);
}
/// <summary>
/// 簡單圖形類——線
/// </summary>
public class Line : Graphics
{
public Line(string name)
: base(name)
{ }
// 重寫父類抽像方法
public override void Draw()
{
Console.WriteLine("畫線:" + Name);
}
public override void Add(Graphics g)
{
throw new Exception("不能向簡單圖形Line添加其他圖形");
}
public override void Remove(Graphics g)
{
throw new Exception("不能向簡單圖形Line移除其他圖形");
}
}
/// <summary>
/// 簡單圖形類——圓
/// </summary>
public class Circle : Graphics
{
public Circle(string name)
: base(name)
{ }
// 重寫父類抽像方法
public override void Draw()
{
Console.WriteLine("畫圓:" + Name);
}
public override void Add(Graphics g)
{
throw new Exception("不能向簡單圖形Circle添加其他圖形");
}
public override void Remove(Graphics g)
{
throw new Exception("不能向簡單圖形Circle移除其他圖形");
}
}
/// <summary>
/// 複雜圖形,由一些簡單圖形組成,假設該複雜圖形由兩條線組成
/// </summary>
public class ComplexGraphics : Graphics
{
private List<Graphics> complexGraphicsList = new List<Graphics>();
public ComplexGraphics(string name)
: base(name)
{ }
/// <summary>
/// 複雜圖形的畫法
/// </summary>
public override void Draw()
{
foreach (Graphics g in complexGraphicsList)
{
g.Draw();
}
}
public override void Add(Graphics g)
{
complexGraphicsList.Add(g);
}
public override void Remove(Graphics g)
{
complexGraphicsList.Remove(g);
}
}
class MyClass
{
static void Main(string[] args)
{
ComplexGraphics complexGraphics = new ComplexGraphics("複雜圖形 - 兩條線段組成的複雜圖形");
complexGraphics.Add(new Line("線段A"));
complexGraphics.Add(new Line("線段C"));
// 顯示複雜圖形的畫法
Console.WriteLine("複雜圖形的繪製:");
Console.WriteLine("---------------------");
complexGraphics.Draw();
Console.WriteLine("複雜圖形繪製完成");
Console.WriteLine("---------------------");
}
}
public abstract class UnitFlowBase
{
protected UnitFlowBase()
{
Node1();
}
protected virtual void Node1()
{
}
protected virtual void Node2()
{
}
protected abstract bool Node3();
public void UnitTest()
{
Node2();
Console.WriteLine(Node3() ? "Assert Successful." : "Assert Fail.");
}
}
public class UnitCounter1 : UnitFlowBase
{
private int _classCounter = 0;
private int _methodCounter = 0;
protected override void Node1()
{
_classCounter++;
}
protected override void Node2()
{
_methodCounter++;
}
protected override bool Node3()
{
Console.WriteLine($"ClassCounter1 : {_classCounter}");
Console.WriteLine($"MethodCounter1: { _methodCounter}");
return true;
}
}
public class UnitCounter2 : UnitFlowBase
{
private int _classCounter = 0;
private int _methodCounter = 0;
protected override void Node1()
{
_classCounter += 2;
}
protected override void Node2()
{
_methodCounter += 2;
}
protected override bool Node3()
{
Console.WriteLine($"ClassCounter2 : {_classCounter}");
Console.WriteLine($"MethodCounter2: { _methodCounter}");
return true;
}
}
class Program
{
static void Main(string[] args)
{
UnitCounter1 unit1_ = new UnitCounter1();
UnitCounter2 unit2_ = new UnitCounter2();
unit1_.UnitTest();
unit1_.UnitTest();
unit2_.UnitTest();
unit2_.UnitTest();
}
}
public interface IDBConnection
{
void GetIDBConnection();
}
2.實作 MYSQL 與 MSSQL 資料庫連線方式
public class MSSQL : IDBConnection
{
public void GetDBConnection()
{
Console.WriteLine("MYSQL連線 ");
}
}
public class MYSQL:IDBConnection
{
public void GetDBConnection()
{
Console.WriteLine("MYSQL 連線");
}
}
3.實做工廠類別
public class ConnectionFactory
{
public static IDDConnection GetConnection(DBType type)
{
IDDConnection db_ = null;
switch (type)
{
case DBType.MySQL:
db_ = new MYSQL();
break;
case DBType.MSSQL:
db_ = new MSSQL();
break;
default:
Console.WriteLine("default type");
break;
}
return db_ ;
}
}
4.外部使用簡單工廠模式
class Program
{
static void Main(string[] args)
{
IDBConnection Connection_ = ConnectionFactory.GetConnection(DBType.MySQL);
Connection.GetDBConnection();
Console.ReadKey();
}
}
using System;
using System.Text.RegularExpressions;
namespace BinaryStringToInteger
{
class Program
{
static void Main(string[] args)
{
BinaryStringToInteger("1000000000000000000");
Console.ReadKey();
}
static readonly Regex Binary = new Regex("^[01]{1,32}$", RegexOptions.Compiled);
static void BinaryStringToInteger(string s)
{
if (Binary.IsMatch(s))
{
Console.WriteLine(Convert.ToInt32(s, 2));
}
else
{
Console.WriteLine("invalid: " + s);
}
}
}
}
///
/// 給人繼承成為獨體
///
/// 要繼承的類別
public class Singleton where T : class, new()
{
protected Singleton()
{
Debug.Assert(null == _instance);
}
private static readonly T _instance = new T();
public static T Instance
{
get
{
return _instance;
}
}
}
var john = new User("John", "Doe");
string EmailTo(User u) => Domain(FullName(u));
var email = EmailTo(john);
Console.WriteLine(email);
// jodo@gmail.com (姓 2 碼 + 名 2 碼 + gmail.com)
namespace ConsoleApp
{
public static class Email
{
public static string FullName(this User u) => ShortName(u.FirstName) + CutName(u.LastName);
public static string Domain(this string localPart) => $"{localPart}@gmail.com";
private static string ShortName(string s) => s.Substring(0, 2).ToLower();
}
}
namespace ConsoleApp
{
internal static class Program
{
private static void Main()
{
var email =
new User("John", "Doe")
.FullName()
.Domain();
Console.WriteLine(email);
}
}
}
Enumerable.Range(1, 3)
.Select(x => x * 2)
.ToList()
.ForEach(x => Console.WriteLine(x.ToString()));
namespace ConsoleApp
{
public static class Extensions
{
internal static void ForEach<t>(this IEnumerable<t> source, Action<t> action)
{
foreach (T element in source)
action(element);
}
}
}
Enumerable
.Range(1, 3)
.Select(x => x * 2)
.ForEach(x => Console.WriteLine(x.ToString()));
用了 Extension Method 後這樣就能拿掉 ToList()
public class FileReader
{
public string Read(string parameter)
{
string result = string.Empty;
//實作硬碟讀取
return result;
}
}
public interface IReadData
{
string GetJsonData(string parameter);
}
///
/// 從網路上讀取要的資料
///
public class WebReader : IReadData
{
public string GetJsonData(string parameter)
{
string result = string.Empty;
//實作網路讀取
return result;
}
}
public class FileAdapter : IReadData
{
public string GetJsonData(string parameter)
{
var reader = new FileReader();
return reader.Read(parameter);
}
}
參考文章: 覺得 Google 的 Blogger 不太順手?透過 HTML 的 iframe 移花接木 HackMD