首页 > 其他分享 >CSharp: Bridge Patterns

CSharp: Bridge Patterns

时间:2022-09-21 22:47:23浏览次数:62  
标签:Bridge Product int void private Patterns CSharp new public

 

   /// <summary>
    /// Summary description for VisList.
    /// 20220918
    /// geovindu,Geovin Du,涂聚文
    /// Bridge  Patterns桥接
    /// </summary>
    public interface VisList
    {
        //add a line to the display
        void addLine(Product p);
        //remove a line from the display
        void removeLine(int num);
    }

   /// <summary>
    /// Summary description for Bridger.
    /// 20220918
    /// geovindu,Geovin Du,涂聚文
    /// Bridge  Patterns桥接
    /// </summary>
    //Bridge interface to display list classes
    public interface Bridger
    {
        void addData(ArrayList col);

    }

  

 /// <summary>
    /// A simple file handlng class
    /// 20220918
    /// geovindu,Geovin Du,涂聚文
    /// Bridge  Patterns桥接
    /// </summary>
    public class csFile
    {
        private string fileName;
        StreamReader ts;
        StreamWriter ws;
        private bool opened, writeOpened;
        /// <summary>
        /// 
        /// </summary>
        public csFile()
        {
            init();
        }
        /// <summary>
        /// 
        /// </summary>
        private void init()
        {
            opened = false;
            writeOpened = false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="file_name"></param>
        public csFile(string file_name)
        {
            fileName = file_name;
            init();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="file_name"></param>
        /// <returns></returns>
        public bool OpenForRead(string file_name)
        {
            fileName = file_name;
            try
            {
                ts = new StreamReader(fileName);
                opened = true;
            }
            catch (FileNotFoundException)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool OpenForRead()
        {
            return OpenForRead(fileName);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string readLine()
        {
            return ts.ReadLine();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        public void writeLine(string s)
        {
            ws.WriteLine(s);
        }
        /// <summary>
        /// 
        /// </summary>
        public void close()
        {
            if (opened)
                ts.Close();
            if (writeOpened)
                ws.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool OpenForWrite()
        {
            return OpenForWrite(fileName);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="file_name"></param>
        /// <returns></returns>
        public bool OpenForWrite(string file_name)
        {
            try
            {
                ws = new StreamWriter(file_name);
                fileName = file_name;
                writeOpened = true;
                return true;
            }
            catch (FileNotFoundException)
            {
                return false;
            }
        }
    }

  

    /// <summary>
    /// Summary description for Product.
    /// 20220918
    /// geovindu,Geovin Du,涂聚文
    /// Bridge  Patterns桥接
    /// </summary>
    public class Product : IComparable
    {
        private string quantity;
        private string name;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="line"></param>
        public Product(string line)
        {
            int i = line.IndexOf("--");
            name = line.Substring(0, i).Trim();
            quantity = line.Substring(i + 2).Trim();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string getQuantity()
        {
            return quantity;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string getName()
        {
            return name;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public int CompareTo(object p)
        {
            Product prod = (Product)p;
            return name.CompareTo(prod.getName());
        }
    }

/// <summary>
    /// Summary description for ProductList.
    /// 20220918
    /// geovindu,Geovin Du,涂聚文
    /// Bridge  Patterns桥接
    /// </summary>
    //A VisList class for the ListBox
    public class ProductList : VisList
    {
        private ListBox list;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lst"></param>
        public ProductList(ListBox lst)
        {
            list = lst;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        public void addLine(Product p)
        {
            list.Items.Add(p.getName());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="num"></param>
        public void removeLine(int num)
        {
            if (num >= 0 && num < list.Items.Count)
            {
                list.Items.Remove(num);
            }
        }
    }

  

 /// <summary>
    /// Summary description for SortBridge.
    /// 20220918
    /// geovindu,Geovin Du,涂聚文
    /// Bridge Patterns桥接
    /// </summary>
    public class SortBridge : ListBridge
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="v"></param>
        public SortBridge(VisList v)
            : base(v)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ar"></param>
        public override void addData(ArrayList ar)
        {
            int max = ar.Count;
            Product[] prod = new Product[max];
            for (int i = 0; i < max; i++)
            {
                prod[i] = (Product)ar[i];
            }
            for (int i = 0; i < max; i++)
            {
                for (int j = i; j < max; j++)
                {
                    if (prod[i].CompareTo(prod[j]) > 0)
                    {
                        Product pt = prod[i];
                        prod[i] = prod[j];
                        prod[j] = pt;
                    }
                }
            }
            for (int i = 0; i < max; i++)
            {
                vis.addLine(prod[i]);
            }
        }
    }

 /// <summary>
    /// Summary description for ListBridge.
    /// 20220918
    /// geovindu,Geovin Du,涂聚文
    /// Bridge  Patterns桥接
    /// </summary>
    public class ListBridge : Bridger
    {
        protected VisList vis;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="v"></param>
        public ListBridge(VisList v)
        {
            vis = v;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ar"></param>
        public virtual void addData(ArrayList ar)
        {
            for (int i = 0; i < ar.Count; i++)
            {
                Product p = (Product)ar[i];
                vis.addLine(p);
            }
        }
    }

  

 /// <summary>
    /// Summary description for GridList.
    /// 20220918
    /// geovindu,Geovin Du,涂聚文
    /// Bridge  Patterns桥接
    /// </summary>
    public class GridList : VisList
    {
        private DataGridView grid;
        private DataTable dtable;
        private GridAdapter gAdapter;
        //-----
        public GridList(DataGridView grd)
        {
            grid = grd;
            dtable = new DataTable("Products");
            DataColumn column = new DataColumn("ProdName");
            dtable.Columns.Add(column);
            column = new DataColumn("Qty");
            dtable.Columns.Add(column);
            grid.DataSource = dtable;
            gAdapter = new GridAdapter(grid);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        public void addLine(Product p)
        {
            gAdapter.Add(p);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="num"></param>
        public void removeLine(int num)
        {

        }
    }


    /// <summary>
    /// Summary description for GridAdapter.
    /// 20220918
    /// geovindu,Geovin Du,涂聚文
    /// Bridge  Patterns桥接
    /// </summary>
    public class GridAdapter
    {

       // private DataGridView DataGrid
        private DataGridView grid;
        private DataTable dTable;
        private int row;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="grd"></param>
        public GridAdapter(DataGridView grd)
        {
            grid = grd;
            dTable = (DataTable)grid.DataSource;
            grid.MouseDown += new System.Windows.Forms.MouseEventHandler(Grid_Click);
            row = -1;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Grid_Click(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            DataGridView.HitTestInfo hti = grid.HitTest(e.X, e.Y);
            if (hti.Type == DataGridViewHitTestType.Cell)    //HitTestType.Cell
            {
                row = hti.RowIndex;   // hti.Row;
            }
        }

        //private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        //{
        //    // If the user right-clicks a cell, store it for use by the shortcut menu.
        //    if (e.Button == MouseButtons.Right)
        //    {
        //        DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
        //        if (hit.Type == DataGridViewHitTestType.Cell)
        //        {
        //            clickedCell =
        //                dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
        //        }
        //    }
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        public void Add(Product p)
        {
            DataRow row = dTable.NewRow();
            row[0] = p.getName();
            row[1] = p.getQuantity();
            dTable.Rows.Add(row);
            dTable.AcceptChanges();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int SelectedIndex()
        {
            return row;
        }
        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            int count = dTable.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                dTable.Rows[i].Delete();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void clearSelection() { }
    }

  

窗体调用:

 /// <summary>
    /// 20220918
    /// geovindu,Geovin Du,涂聚文
    /// Bridge  Patterns桥接
    /// </summary>
    public partial class BridgePatternsForm : Form
    {

        private ArrayList products;
        private VisList prodList, gridList;
        /// <summary>
        /// 
        /// </summary>
        private void init()
        {
            products = new ArrayList();
            readFile(products);	//read in the data file
            //create the product list 
            prodList = new ProductList(lsProd);
            //Bridge to product VisList
            Bridger lbr = new SortBridge(prodList);
            //put the data into the product list
            lbr.addData(products);
            //create the grid VisList
            gridList = new GridList(grdProd);
            //Bridge to the grid list
            Bridger gbr = new SortBridge(gridList);
            //put the data into the grid display
            gbr.addData(products);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="products"></param>
        private void readFile(ArrayList products)
        {
            csFile fl = new csFile("products.txt");
            fl.OpenForRead();
            string s = fl.readLine();
            while (s != null)
            {
                Product p = new Product(s);
                products.Add(p);
                s = fl.readLine();
            }
            fl.close();
        }
        /// <summary>
        /// 
        /// </summary>
        public BridgePatternsForm()
        {
            InitializeComponent();
            init();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BridgePatternsForm_Load(object sender, EventArgs e)
        {

        }
    }

 输出:

 

标签:Bridge,Product,int,void,private,Patterns,CSharp,new,public
From: https://www.cnblogs.com/geovindu/p/16717429.html

相关文章

  • Java: Immutable Patterns
     /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*不变模式ImmutablePatterns*历史版本:JDK14.02*2022-09-12创建者geovindu*2022-09-1......
  • java: Bridge Patterns
     /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*桥梁模式BridgePatterns*历史版本:JDK14.02*2022-09-12创建者geovindu*2022-09-12......
  • CSharp: Adapter Patterns
     ///<summary>///Asimplefilehandlngclass///geovindu,GeovinDu,涂聚文///适配器模式(AdapterPatterns)///</summary>publicclass......
  • java: Facade Patterns
     /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*门面模式FacadePatterns*历史版本:JDK14.02*2022-09-12创建者geovindu*2022-09-12......
  • java: Flyweight Patterns
     /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*享元模式FlyweightPatterns*历史版本:JDK14.02*2022-09-12创建者geovindu*2022-09-1......
  • Csharp: Singleton Patterns
     ///<summary>///SummarydescriptionforSpooler.///geovindu,GeovinDu,涂聚文///单例模式(SingletonPatterns)///</summary>pu......
  • Csharp: Prototype Patterns
     ///<summary>///SummarydescriptionforSwimmer///geovindu,GeovinDu,涂聚文///原型模式(PrototypePatterns)///</summary>public......
  • dependency-injection-principles-practices-and-patterns 阅读记录
    最近断断续续看了一下dependency-injection-principles-practices-and-patterns这本书,随便记录了一下思维导图,结合目前的经验还是感觉到收获不少。......
  • Csharp: Builder Patterns
     ///<summary>///SummarydescriptionforEquities.///geovindu,GeovinDu,涂聚文///生成器(建造)模式(BuilderPatterns)///</summary>......
  • 用 CSharpCompilation 进行动态编译
    项目里需要用到动态编译。网上一大片的介绍C#动态编译的,是CodeDomProvider,这个东西确实好用。但是说好了支持.net6.0 的,但运行时却说不支持当前平台。骗子!网上找的......