BGProject 項目類型,組合多個BGDiargam視圖對象
BGDiagram視圖類型,組合多個BGElement元素對象,存在多個派生元素類型
View層在使用數據時,可封裝視圖數據類型組合Model數據類型,以記錄其他UI相關數據
Compile方法由各個派生類自行實現編譯邏輯
上層編譯邏輯的實現,簡單使用多態,如下
public bool Compile(BGProject project, out String errorMessage)
{
TextCompiler textCompiler = new TextCompiler(project);
ImageCompiler imageCompiler = new ImageCompiler(project);
XxxCompiler xxxCompiler = new XxxCompiler(project);
foreach (CompilerBase compiler in
new CompilerBase[] {textCompiler, imageCompiler, XxxCompiler})
{
compiler.Compile();
if (!compiler.Validate(out errorMessage))
{
return false;
}
}
// ...
}
protected static void TravelElements<T>(BGProject project, BGElementType elementType, Action<T> action)
where T : BGElement
{
foreach (BGDiagram diagram in project.Diagrams)
{
foreach (T element in
diagram.Elements.Where(e => e.ElementType == elementType))
{
action(element);
}
}
}
處理邏輯通過action參數傳遞進來
TextCompiler中編譯文字元素時,調用上述方法,通過lambda表達式生成匿名方法完成處理邏輯
TravelElements<BGTextElement>(Project, BGElementType.Text,
element =>
{
//.... 針對目標元素做相應處理
});
處理該特定問題,這裡使用委托的方式,泛型化使其易於使用,你也可以使用TemplateMethod模式
序列化工具類
項目中涉及數據序列化到本地文件,直接使用如下工具類型
public static class SerializeUtility
{
public static void BinarySave<T>(String filePath, T obj)
{
using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
}
}
public static T BinaryLoad<T>(String filePath)
{
return BinaryLoad<T>(filePath, null);
}
public static T BinaryLoad<T>(String filePath, SerializationBinder serializationBinder)
{
if (!File.Exists(filePath))
{
return default(T);
}
using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
{
IFormatter formatter = new BinaryFormatter();
if (serializationBinder != null)
{
formatter.Binder = serializationBinder;
}
return (T)formatter.Deserialize(stream);
}
}
}
int與byte數組轉換工具類
涉及到傳輸數據至下位機,考慮數據存儲格式,編寫如下工具類,支持littleEnding與bigEnding
// Created by Ant 2014-4-30
public static class BytesConverterUtility
{
public static byte[] GetBytes(int value, int length, bool isLittleEndian = true)
{
if (value < 0)
{
throw new ArgumentException("value不能為負數");
}
if (length > 4)
{
throw new ArgumentException("length不能>4");
}
var rawBytes = BitConverter.GetBytes(value);
if (rawBytes.Length < length)
{
throw new ApplicationException(
String.Format("BitConverter.GetBytes返回的字符數{0}小於目標字符數{1}", rawBytes.Length, length));
}
var bytes = new byte[length];
if (BitConverter.IsLittleEndian != isLittleEndian)
{
Array.Reverse(rawBytes);
Array.Copy(rawBytes, rawBytes.Length - length,
bytes, 0, length);
}
else
{
Array.Copy(rawBytes, bytes, length);
}
return bytes;
}
public static int ToInt(byte[] bytes, int offset, int length, bool isLittleEndian = true)
{
if (length == 1)
{
return bytes[offset];
}
var tempBytes = new byte[length];
Array.Copy(bytes, offset, tempBytes, 0, length);
if (!isLittleEndian)
{
Array.Reverse(tempBytes);
}
switch (length)
{
case 2:
// 特殊處理,轉換為無符號int類型,返回時自動轉換為Int32
return BitConverter.ToUInt16(tempBytes, 0);
case 4:
// 注意,這裡將數據轉換為有符號int類型
return BitConverter.ToInt32(tempBytes, 0);
default:
throw new ArgumentException("length 長度非標准值");
}
}
}
工具類型的方便之處在於其獨立性,幾乎無外部依賴,不需要考慮對其進行初始化,拿來就可以直接使用