本文共 13887 字,大约阅读时间需要 46 分钟。
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Console.Write("Please Input Some Words:"); string inputData = Console.ReadLine(); Console.WriteLine(inputData); Console.Read(); } } } |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication10 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(textBox1.Text); } } } |
using System; using System.Collections.Generic; using System.Text; namespace DISimple { class Program { static void Main(string[] args) { InputAccept accept = new InputAccept(new PromptDataProcessor()); accept.Execute(); Console.ReadLine(); } } public class InputAccept { private IDataProcessor _dataProcessor; public void Execute() { Console.Write("Please Input some words:"); string input = Console.ReadLine(); input = _dataProcessor.ProcessData(input); Console.WriteLine(input); } public InputAccept(IDataProcessor dataProcessor) { _dataProcessor = dataProcessor; } } public interface IDataProcessor { string ProcessData(string input); } public class DummyDataProcessor : IDataProcessor { #region IDataProcessor Members public string ProcessData(string input) { return input; } #endregion } public class PromptDataProcessor : IDataProcessor { #region IDataProcessor Members public string ProcessData(string input) { return "your input is: " + input; } #endregion } } |
InputAccept accept = new InputAccept(new PromptDataProcessor()); |
//Factory InputAccept accept = new InputAccept(DataProcessorFactory.Create()); //Builder InputAccept accept = new InputAccept(DataProcessorBulder.Build()); |
//declare public class DataProcessorFactory:IFactory .......... //Builder public class DataProcessorBuilder:IBuilder ........... .................... //initialize //Factory GenericFactory.RegisterTypeFactory(typeof(IDataProcessor),typeof(DataProcessorFactory)); //Builder GenericFactory.RegisterTypeBuilder(typeof(IDataProcessor),typeof(DataProcessorBuilder)); ................ //Factory InputAccept accept = new InputAccept(GenericFactory.Create(typeof(IDataProcessor)); //Builder InputAccept accept = new InputAccept(GenericBuilder.Build(typeof(IDataProcessor)); |
PS: 這一小節進度超前許多,接下來將回歸 Dependency Injection 的三種模式,請注意!接下來幾小節的討論是依據三種模式的精神,所以例子以簡單易懂為主,不考慮本文所提及的完整架構。 |
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { InputAccept accept = new InputAccept(); accept.Inject(new DummyDataProcessor()); accept.Execute(); Console.Read(); } } public class InputAccept { private IDataProcessor _dataProcessor; public void Inject(IDataProcessor dataProcessor) { _dataProcessor = dataProcessor; } public void Execute() { Console.Write("Please Input some words:"); string input = Console.ReadLine(); input = _dataProcessor.ProcessData(input); Console.WriteLine(input); } } public interface IDataProcessor { string ProcessData(string input); } public class DummyDataProcessor : IDataProcessor { #region IDataProcessor Members public string ProcessData(string input) { return input; } #endregion } public class PromptDataProcessor : IDataProcessor { #region IDataProcessor Members public string ProcessData(string input) { return "your input is: " + input; } #endregion } } |
public class InputAccept implements Serviceable { private IDataProcessor m_dataProcessor; public void service(ServiceManager sm) throws ServiceException { m_dataProcessor = (IDataProcessor) sm.lookup("DataProcessor"); } public void Execute() { ........ string input = m_dataProcessor.ProcessData(input); ........ } } |
PS: 在正確的Interface Injection定義中,組裝InputAccept與IDataProcessor的是容器,在本例中,我並未使用容器,而是提取其行為。 |
public static class Container { private static Dictionary<Type, object> _stores = null; private static Dictionary<Type, object> Stores { get { if (_stores == null) _stores = new Dictionary<Type, object>(); return _stores; } } private static Dictionary<string,object> CreateConstructorParameter(Type targetType) { Dictionary<string, object> paramArray = new Dictionary<string, object>(); ConstructorInfo[] cis = targetType.GetConstructors(); if (cis.Length > 1) throw new Exception( "target object has more then one constructor,container can't peek one for you." ); foreach (ParameterInfo pi in cis[0].GetParameters()) { if (Stores.ContainsKey(pi.ParameterType)) paramArray.Add(pi.Name, GetInstance(pi.ParameterType)); } return paramArray; } public static object GetInstance(Type t) { if (Stores.ContainsKey(t)) { ConstructorInfo[] cis = t.GetConstructors(); if (cis.Length != 0) { Dictionary<string, object> paramArray = CreateConstructorParameter(t); List<object> cArray = new List<object>(); foreach (ParameterInfo pi in cis[0].GetParameters()) { if (paramArray.ContainsKey(pi.Name)) cArray.Add(paramArray[pi.Name]); else cArray.Add(null); } return cis[0].Invoke(cArray.ToArray()); } else if (Stores[t] != null) return Stores[t]; else return Activator.CreateInstance(t, false); } return Activator.CreateInstance(t, false); } public static void RegisterImplement(Type t, object impl) { if (Stores.ContainsKey(t)) Stores[t] = impl; else Stores.Add(t, impl); } public static void RegisterImplement(Type t) { if (!Stores.ContainsKey(t)) Stores.Add(t, null); } } |
class Program { static void Main(string[] args) { Container.RegisterImplement(typeof(InputAccept)); Container.RegisterImplement(typeof(IDataProcessor), new PromptDataProcessor()); InputAccept accept = (InputAccept)Container.GetInstance(typeof(InputAccept)); accept.Execute(); Console.Read(); } } public class InputAccept { private IDataProcessor _dataProcessor; public void Execute() { Console.Write("Please Input some words:"); string input = Console.ReadLine(); input = _dataProcessor.ProcessData(input); Console.WriteLine(input); } public InputAccept(IDataProcessor dataProcessor) { _dataProcessor = dataProcessor; } } public interface IDataProcessor { string ProcessData(string input); } public class DummyDataProcessor : IDataProcessor { #region IDataProcessor Members public string ProcessData(string input) { return input; } #endregion } public class PromptDataProcessor : IDataProcessor { #region IDataProcessor Members public string ProcessData(string input) { return "your input is: " + input; } #endregion } |
public static class Container { private static Dictionary<Type, object> _stores = null; private static Dictionary<Type, object> Stores { get { if (_stores == null) _stores = new Dictionary<Type, object>(); return _stores; } } public static object GetInstance(Type t) { if (Stores.ContainsKey(t)) { if (Stores[t] == null) { object target = Activator.CreateInstance(t, false); foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(target)) { if (Stores.ContainsKey(pd.PropertyType)) pd.SetValue(target, GetInstance(pd.PropertyType)); } return target; } else return Stores[t]; } return Activator.CreateInstance(t, false); } public static void RegisterImplement(Type t, object impl) { if (Stores.ContainsKey(t)) Stores[t] = impl; else Stores.Add(t, impl); } public static void RegisterImplement(Type t) { if (!Stores.ContainsKey(t)) Stores.Add(t, null); } } |
class Program { static void Main(string[] args) { Container.RegisterImplement(typeof(InputAccept)); Container.RegisterImplement(typeof(IDataProcessor), new PromptDataProcessor()); InputAccept accept = (InputAccept)Container.GetInstance(typeof(InputAccept)); accept.Execute(); Console.Read(); } } public class InputAccept { private IDataProcessor _dataProcessor; public IDataProcessor DataProcessor { get { return _dataProcessor; } set { _dataProcessor = value; } } public void Execute() { Console.Write("Please Input some words:"); string input = Console.ReadLine(); input = _dataProcessor.ProcessData(input); Console.WriteLine(input); } } |
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1282139