前几天阅读一框架文档,里面有一段这样的描述 “从对象工厂中………” ,促使写下本文。尽管一些模式简单和简单,但是常用、有用。
结合最近一个项目场景回顾一下里面应用到的一些模式 Singleton、Factory、Strategy。
项目需求/场景:通过增加辅助工具使用脚本程序对特定应用程序进行“自动化测试”,内容包括:点击按钮、选择菜单、读取控件内容等。
原始实现:脚本程序
目标程序简化图:
使用辅助工具前 :
使用辅助工具后:
这里只对 辅助工具 进行分析设计,其它略过。
1、图1 目标程序有以下主要特点:
2、辅助工具对外提供统一调用
3、辅助工具可以被重复调用,但不支持并发操作
基于以上分析:
using System; using System.Threading; using DesignPatternDemo.Operator; namespace DesignPatternDemo { public class AuxiliaryToolSingleton { public static Semaphore OperatorSemaphore = new Semaphore(1, 1); private static readonly object OperatorLock = new object(); public static AuxiliaryToolSingleton Instance = new AuxiliaryToolSingleton(); private AuxiliaryToolSingleton() { RegistorOperator(OperatorFactory.Instance); } public void CallOperator(string operatorName, params string[] operatorParams) { //OperatorSemaphore.WaitOne(); lock (OperatorLock) { Console.WriteLine($"Call method CallOperator :{operatorName} .Current Thread:{Thread.CurrentThread.ManagedThreadId}"); BaseOperator concreteOperator = OperatorFactory.Instance.GetOperator(operatorName); concreteOperator.InitializationParameters(operatorParams); concreteOperator.Execute(); } //OperatorSemaphore.Release(); } public static void RegistorOperator(OperatorFactory factory) { factory.Register(nameof(AOperator), new AOperator()); factory.Register(nameof(BOperator), new BOperator()); factory.Register(nameof(COperator), new COperator()); factory.Register(nameof(DOperator), new DOperator()); factory.Register(nameof(EOperator), new EOperator()); } } } AuxiliaryToolSingleton复制代码
using System; using System.Threading; namespace DesignPatternDemo.Operator { public class BaseOperator { public string Name { get; set; } public string Description { get; set; } public void Execute() { //ToDo Thread.Sleep(new Random().Next(0, 5) * 1000); Console.WriteLine($"Execute concrete operator:{GetType().Name} .Current Thread:{Thread.CurrentThread.ManagedThreadId}"); ConcreteOperate($"{GetType().Name}"); } public void InitializationParameters(params string[] operatorParams) { //ToDo Console.WriteLine($"Initialization Parameters :{GetType().Name}"); } private void ConcreteOperate(string mark) { // ToDo Console.WriteLine($"The concrete operation :{mark} was performed successfully ./r/n"); } public virtual void ClickButtonByMark(string mark) { // ToDo ConcreteOperate(mark); } public virtual void ClickPopupMenuByMark(string mark) { // ToDo ConcreteOperate(mark); } public virtual void SelectDropdownBoxByIndex(int dropBoxIndex) { // ToDo ConcreteOperate($"{dropBoxIndex}"); } } } BaseOperator复制代码
using System; namespace DesignPatternDemo.Operator { public class AOperator : BaseOperator, ISpecialOperateA { public void SetContent(string content) { //ToDo Console.WriteLine($"Filled the content:{content} successfully"); } public string GetContent() { //ToDo return $"{new Random().Next()}{Guid.NewGuid()}"; } } } namespace DesignPatternDemo.Operator { public interface ISpecialOperateA { void SetContent(string content); string GetContent(); } } AOperator复制代码
namespace DesignPatternDemo.Operator { public class BOperator : BaseOperator { } } namespace DesignPatternDemo.Operator { public class COperator : BaseOperator { } } namespace DesignPatternDemo.Operator { public class DOperator : BaseOperator { } } Concrete Operator复制代码
using System; namespace DesignPatternDemo.Operator { public class EOperator : BaseOperator, ISpecialOperateE { public void ClickTreeviewByMark(string mark) { //ToDo Console.WriteLine($"{mark}: execution succeed"); } } } namespace DesignPatternDemo.Operator { public interface ISpecialOperateE { void ClickTreeviewByMark(string mark); } } EOperator复制代码
using System.Collections.Generic; namespace DesignPatternDemo { public class Factory<TF, TV> where TF : new() { protected Factory() { KeyValues = new Dictionary<string, TV>(); } public static TF Instance { get; set; } = new TF(); private Dictionary<string, TV> KeyValues { get; } public TV GetItem(string key) { KeyValues.TryGetValue(key, out TV find); return find; } public void Register(string key, TV t) { UnRegister(key); KeyValues.Add(key, t); } public void UnRegister(string key) { if (KeyValues.ContainsKey(key)) KeyValues.Remove(key); } } } Factory复制代码
using DesignPatternDemo.Operator; namespace DesignPatternDemo { public class OperatorFactory : Factory<OperatorFactory, BaseOperator> { public BaseOperator GetOperator(string operatorName) { return GetItem(operatorName); } } } OperatorFactory复制代码
using System; using System.Collections.Generic; using System.Threading.Tasks; using DesignPatternDemo.Operator; namespace DesignPatternDemo { internal class Program { private static void Main(string[] args) { Console.WriteLine("Hello World!"); List<string> concreteOperators = GetConcreteOperators(); Parallel.ForEach(concreteOperators, current => { CallOperator(current); }); foreach (string operatorName in concreteOperators) { Task concreteTask = new Task(() => { CallOperator(operatorName); }); concreteTask.Start(); } Console.ReadKey(); } private static List<string> GetConcreteOperators() { List<string> concreteOperators = new List<string> { nameof(AOperator), nameof(BOperator), nameof(COperator), nameof(DOperator), nameof(EOperator) }; return concreteOperators; } private static void CallOperator(string operatorName, params string[] operatorParams) { AuxiliaryToolSingleton auxiliaryTool = AuxiliaryToolSingleton.Instance; auxiliaryTool.CallOperator(operatorName, operatorParams); } } } Program复制代码
六、说明、小结
文源网络,仅供学习之用,如有侵权,联系删除。
我将优质的技术文章和经验总结都汇集在了我的公众号【Java圈子】里。
为方便大家学习,我整理了一套学习资料,涵盖Java虚拟机、spring框架、Java线程、数据结构、设计模式等等,免费提供给热爱Java的同学! 更有学习交流群,多交流问题才能更快进步~