当前位置:  开发笔记 > 编程语言 > 正文

桥梁或工厂和如何

如何解决《桥梁或工厂和如何》经验,为你挑选了1个好方法。

我正在努力学习模式,而且我有一份为模式而尖叫的工作,我只知道它,但我无法理解.我知道过滤器类型可以抽象并可能被桥接.我不是在寻找代码重写的建议.我不是在找人做我的工作.我想知道如何将模式应用于此示例.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;

namespace CopyTool
{
    class CopyJob
    {
        public enum FilterType
        { TextFilter, RegExFilter, NoFilter }
        public FilterType JobFilterType { get; set; }

        private string _jobName;
        public string JobName { get { return _jobName; } set { _jobName = value; } }

        private int currentIndex;
        public int CurrentIndex { get { return currentIndex; } }

        private DataSet ds;

        public int MaxJobs { get { return ds.Tables["Job"].Rows.Count; } }

        private string _filter;
        public string Filter { get { return _filter; } set { _filter = value; } }

        private string _fromFolder;
        public string FromFolder
        {
            get { return _fromFolder; }
            set
            {
                if (Directory.Exists(value))
                { _fromFolder = value; }
                else
                { throw new DirectoryNotFoundException(String.Format("Folder not found: {0}", value)); }
            }
        }

        private List _toFolders;
        public List ToFolders { get { return _toFolders; } }

        public CopyJob()
        {
            Initialize();
        }

        private void Initialize()
        {
            if (ds == null)
            { ds = new DataSet(); }
            ds.ReadXml(Properties.Settings.Default.ConfigLocation);
            LoadValues(0);
        }

        public void Execute()
        {
            ExecuteJob(FromFolder, _toFolders, Filter, JobFilterType);
        }

        public void ExecuteAll()
        {
            string OrigPath;
            List DestPaths;
            string FilterText;
            FilterType FilterWay;
            foreach (DataRow rw in ds.Tables["Job"].Rows)
            {
                OrigPath = rw["FromFolder"].ToString();
                FilterText = rw["FilterText"].ToString();
                switch (rw["FilterType"].ToString())
                {
                    case "TextFilter":
                        FilterWay = FilterType.TextFilter;
                        break;
                    case "RegExFilter":
                        FilterWay = FilterType.RegExFilter;
                        break;
                    default:
                        FilterWay = FilterType.NoFilter;
                        break;
                }
                DestPaths = new List();
                foreach (DataRow crw in rw.GetChildRows("Job_ToFolder"))
                {
                    DestPaths.Add(crw["FolderPath"].ToString());
                }
                ExecuteJob(OrigPath, DestPaths, FilterText, FilterWay);
            }
        }

        private void ExecuteJob(string OrigPath, List DestPaths, string FilterText, FilterType FilterWay)
        {
            FileInfo[] files;
            switch (FilterWay)
            {
                case FilterType.RegExFilter:
                    files = GetFilesByRegEx(new Regex(FilterText), OrigPath);
                    break;
                case FilterType.TextFilter:
                    files = GetFilesByFilter(FilterText, OrigPath);
                    break;
                default:
                    files = new DirectoryInfo(OrigPath).GetFiles();
                    break;
            }
            foreach (string fld in DestPaths)
            {
                CopyFiles(files, fld);
            }
        }

        public void MoveToJob(int RecordNumber)
        {
            Save();
            LoadValues(RecordNumber - 1);
        }

        public void AddToFolder(string folderPath)
        {
            if (Directory.Exists(folderPath))
            { _toFolders.Add(folderPath); }
            else
            { throw new DirectoryNotFoundException(String.Format("Folder not found: {0}", folderPath)); }
        }

        public void DeleteToFolder(int index)
        {
            _toFolders.RemoveAt(index);
        }

        public void Save()
        {
            DataRow rw = ds.Tables["Job"].Rows[currentIndex];
            rw["JobName"] = _jobName;
            rw["FromFolder"] = _fromFolder;
            rw["FilterText"] = _filter;
            switch (JobFilterType)
            {
                case FilterType.RegExFilter:
                    rw["FilterType"] = "RegExFilter";
                    break;
                case FilterType.TextFilter:
                    rw["FilterType"] = "TextFilter";
                    break;
                default:
                    rw["FilterType"] = "NoFilter";
                    break;
            }
            DataRow[] ToFolderRows = ds.Tables["Job"].Rows[currentIndex].GetChildRows("Job_ToFolder");
            for (int i = 0; i <= ToFolderRows.GetUpperBound(0); i++)
            {
                ToFolderRows[i].Delete();
            }
            foreach (string fld in _toFolders)
            {
                DataRow ToFolderRow = ds.Tables["ToFolder"].NewRow();
                ToFolderRow["JobId"] = ds.Tables["Job"].Rows[currentIndex]["JobId"];
                ToFolderRow["Job_Id"] = ds.Tables["Job"].Rows[currentIndex]["Job_Id"];
                ToFolderRow["FolderPath"] = fld;
                ds.Tables["ToFolder"].Rows.Add(ToFolderRow);
            }
        }

        public void Delete()
        {
            ds.Tables["Job"].Rows.RemoveAt(currentIndex);
            LoadValues(currentIndex++);
        }

        public void MoveNext()
        {
            Save();
            currentIndex++;
            LoadValues(currentIndex);
        }

        public void MovePrevious()
        {
            Save();
            currentIndex--;
            LoadValues(currentIndex);
        }

        public void MoveFirst()
        {
            Save();
            LoadValues(0);
        }

        public void MoveLast()
        {
            Save();
            LoadValues(ds.Tables["Job"].Rows.Count - 1);
        }

        public void CreateNew()
        {
            Save();
            int MaxJobId = 0;
            Int32.TryParse(ds.Tables["Job"].Compute("Max(JobId)", "").ToString(), out MaxJobId);
            DataRow rw = ds.Tables["Job"].NewRow();
            rw["JobId"] = MaxJobId + 1;
            ds.Tables["Job"].Rows.Add(rw);
            LoadValues(ds.Tables["Job"].Rows.IndexOf(rw));
        }

        public void Commit()
        {
            Save();
            ds.WriteXml(Properties.Settings.Default.ConfigLocation);
        }

        private void LoadValues(int index)
        {
            if (index > ds.Tables["Job"].Rows.Count - 1)
            { currentIndex = ds.Tables["Job"].Rows.Count - 1; }
            else if (index < 0)
            { currentIndex = 0; }
            else
            { currentIndex = index; }
            DataRow rw = ds.Tables["Job"].Rows[currentIndex];
            _jobName = rw["JobName"].ToString();
            _fromFolder = rw["FromFolder"].ToString();
            _filter = rw["FilterText"].ToString();
            switch (rw["FilterType"].ToString())
            {
                case "TextFilter":
                    JobFilterType = FilterType.TextFilter;
                    break;
                case "RegExFilter":
                    JobFilterType = FilterType.RegExFilter;
                    break;
                default:
                    JobFilterType = FilterType.NoFilter;
                    break;
            }
            if (_toFolders == null)
                _toFolders = new List();
            _toFolders.Clear();
            foreach (DataRow crw in rw.GetChildRows("Job_ToFolder"))
            {
                AddToFolder(crw["FolderPath"].ToString());
            }
        }

        private static FileInfo[] GetFilesByRegEx(Regex rgx, string locPath)
        {
            DirectoryInfo d = new DirectoryInfo(locPath);
            FileInfo[] fullFileList = d.GetFiles();
            List filteredList = new List();
            foreach (FileInfo fi in fullFileList)
            {
                if (rgx.IsMatch(fi.Name))
                {
                    filteredList.Add(fi);
                }
            }
            return filteredList.ToArray();
        }

        private static FileInfo[] GetFilesByFilter(string filter, string locPath)
        {
            DirectoryInfo d = new DirectoryInfo(locPath);
            FileInfo[] fi = d.GetFiles(filter);
            return fi;
        }

        private void CopyFiles(FileInfo[] files, string destPath)
        {
            foreach (FileInfo fi in files)
            {
                bool success = false;
                int i = 0;
                string copyToName = fi.Name;
                string copyToExt = fi.Extension;
                string copyToNameWithoutExt = Path.GetFileNameWithoutExtension(fi.FullName);
                while (!success && i < 100)
                {
                    i++;
                    try
                    {
                        if (File.Exists(Path.Combine(destPath, copyToName)))
                            throw new CopyFileExistsException();
                        File.Copy(fi.FullName, Path.Combine(destPath, copyToName));
                        success = true;
                    }
                    catch (CopyFileExistsException ex)
                    {
                        copyToName = String.Format("{0} ({1}){2}", copyToNameWithoutExt, i, copyToExt);
                    }
                }
            }
        }
    }
    public class CopyFileExistsException : Exception
    {
        public string Message;
    }

}

msergeant.. 6

此代码也"尖叫",可以分解为更小的更专业的对象.

您的CopyJob对象似乎更像是作业列表的管理者.我可能会将此名称更改为CopyJobManager或其他东西.然后,您可以将CopyJob作为不同过滤器类型的基类.例如,公共代码Execute()将在基类中定义,并且自定义行为(例如,Filtering)将在派生类中处理.你会有一个TextFilterCopyJob,一个RegExFilterCopyJob和一个NoFilterCopyJob.

工厂模式可以发挥作用的是你正在构建一个CopyJobs列表.您可以拥有一个CopyJobFactory对象,该对象从您的数据集中获取一行并返回CopyJob的正确子版本.然后,CopyJobManager将在CopyJobs列表上执行其操作,而不是数据集行列表.



1> msergeant..:

此代码也"尖叫",可以分解为更小的更专业的对象.

您的CopyJob对象似乎更像是作业列表的管理者.我可能会将此名称更改为CopyJobManager或其他东西.然后,您可以将CopyJob作为不同过滤器类型的基类.例如,公共代码Execute()将在基类中定义,并且自定义行为(例如,Filtering)将在派生类中处理.你会有一个TextFilterCopyJob,一个RegExFilterCopyJob和一个NoFilterCopyJob.

工厂模式可以发挥作用的是你正在构建一个CopyJobs列表.您可以拥有一个CopyJobFactory对象,该对象从您的数据集中获取一行并返回CopyJob的正确子版本.然后,CopyJobManager将在CopyJobs列表上执行其操作,而不是数据集行列表.

推荐阅读
无名有名我无名_593
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有