我是靠谱客的博主 傻傻小笼包,最近开发中收集的这篇文章主要介绍Proxy 为其他对象提供一个代理(surrogate)或者占位符(placeholder),从而完成对其他对象的访问。...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

ContractedBlock.gif ExpandedBlockStart.gif 自定义代理类
  1None.gifusing System;
  2None.gifusing System.Data;
  3None.gif
  4None.gifnamespace DataLayer
  5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  7InBlock.gif    /// This class strictly forwards every call to a subject object. To
  8InBlock.gif    /// intercept any specific call, create a subclass.
  9ExpandedSubBlockEnd.gif    /// </summary>

 10InBlock.gif    public class DataReaderProxy : IDataReader
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12InBlock.gif        private IDataReader _subject;
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 14InBlock.gif        /// Create a proxy for the given subject.
 15InBlock.gif        /// </summary>
 16ExpandedSubBlockEnd.gif        /// <param name="subject">The real reader to proxy for</param>

 17InBlock.gif        public DataReaderProxy(IDataReader subject) 
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            _subject = subject;
 20ExpandedSubBlockEnd.gif        }

 21InBlock.gif        // properties for IDataRecord
 22InBlock.gif        public virtual int FieldCount
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24InBlock.gif            get
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 26InBlock.gif                return _subject.FieldCount;
 27ExpandedSubBlockEnd.gif            }

 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif        public virtual object this [string name]
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31InBlock.gif            get
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 33InBlock.gif                return _subject[name];
 34ExpandedSubBlockEnd.gif            }

 35ExpandedSubBlockEnd.gif        }

 36InBlock.gif        public virtual object this [int index]
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 38InBlock.gif            get
 39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 40InBlock.gif                return _subject[index];
 41ExpandedSubBlockEnd.gif            }

 42ExpandedSubBlockEnd.gif        }

 43InBlock.gif        // properties for IDataReader
 44InBlock.gif        public virtual int Depth
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            get
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 48InBlock.gif                return _subject.Depth;
 49ExpandedSubBlockEnd.gif            }

 50ExpandedSubBlockEnd.gif        }

 51InBlock.gif        public virtual bool IsClosed
 52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 53InBlock.gif            get
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 55InBlock.gif                return _subject.IsClosed;
 56ExpandedSubBlockEnd.gif            }

 57ExpandedSubBlockEnd.gif        }

 58InBlock.gif        // methods for IDataRecord
 59InBlock.gif        public virtual bool GetBoolean(int i) 
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 61InBlock.gif            return _subject.GetBoolean(i);
 62ExpandedSubBlockEnd.gif        }

 63InBlock.gif        public virtual byte GetByte(int i) 
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 65InBlock.gif            return _subject.GetByte(i);
 66ExpandedSubBlockEnd.gif        }

 67InBlock.gif        public virtual long GetBytes(
 68InBlock.gif            int i,
 69InBlock.gif            long fieldoffset,
 70InBlock.gif            byte[] buffer,
 71InBlock.gif            int bufferoffset,
 72InBlock.gif            int length
 73InBlock.gif            ) 
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            return _subject.GetBytes(i, fieldoffset, buffer, bufferoffset, length);
 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif        public virtual char GetChar(int i) 
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 79InBlock.gif            return _subject.GetChar(i);
 80ExpandedSubBlockEnd.gif        }

 81InBlock.gif        public virtual long GetChars(
 82InBlock.gif            int i,
 83InBlock.gif            long fieldoffset,
 84InBlock.gif            char[] buffer,
 85InBlock.gif            int bufferoffset,
 86InBlock.gif            int length
 87InBlock.gif            ) 
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            return _subject.GetChars(i, fieldoffset, buffer, bufferoffset, length);
 90ExpandedSubBlockEnd.gif        }

 91InBlock.gif        public virtual IDataReader GetData(int i) 
 92ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 93InBlock.gif            return _subject.GetData(i);
 94ExpandedSubBlockEnd.gif        }

 95InBlock.gif        public virtual string GetDataTypeName(int i) 
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 97InBlock.gif            return _subject.GetDataTypeName(i);
 98ExpandedSubBlockEnd.gif        }

 99InBlock.gif        public virtual DateTime GetDateTime(int i) 
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
101InBlock.gif            return _subject.GetDateTime(i);
102ExpandedSubBlockEnd.gif        }

103InBlock.gif        public virtual decimal GetDecimal(int i) 
104ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
105InBlock.gif            return _subject.GetDecimal(i);
106ExpandedSubBlockEnd.gif        }

107InBlock.gif        public virtual double GetDouble(int i) 
108ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
109InBlock.gif            return _subject.GetDouble(i);
110ExpandedSubBlockEnd.gif        }

111InBlock.gif        public virtual Type GetFieldType(int i) 
112ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
113InBlock.gif            return _subject.GetFieldType(i);
114ExpandedSubBlockEnd.gif        }

115InBlock.gif        public virtual float GetFloat(int i) 
116ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
117InBlock.gif            return _subject.GetFloat(i);
118ExpandedSubBlockEnd.gif        }

119InBlock.gif        public virtual Guid GetGuid(int i) 
120ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
121InBlock.gif            return _subject.GetGuid(i);
122ExpandedSubBlockEnd.gif        }

123InBlock.gif        public virtual short GetInt16(int i) 
124ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
125InBlock.gif            return _subject.GetInt16(i);
126ExpandedSubBlockEnd.gif        }

127InBlock.gif        public virtual int GetInt32(int i) 
128ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
129InBlock.gif            return _subject.GetInt32(i);
130ExpandedSubBlockEnd.gif        }

131InBlock.gif        public virtual long GetInt64(int i) 
132ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
133InBlock.gif            return _subject.GetInt64(i);
134ExpandedSubBlockEnd.gif        }

135InBlock.gif        public virtual string GetName(int i) 
136ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
137InBlock.gif            return _subject.GetName(i);
138ExpandedSubBlockEnd.gif        }

139InBlock.gif        public virtual int GetOrdinal(string name) 
140ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
141InBlock.gif            return _subject.GetOrdinal(name);
142ExpandedSubBlockEnd.gif        }

143InBlock.gif        public virtual string GetString(int i) 
144ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
145InBlock.gif            return _subject.GetString(i);
146ExpandedSubBlockEnd.gif        }

147InBlock.gif        public virtual object GetValue(int i) 
148ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
149InBlock.gif            return _subject.GetValue(i);
150ExpandedSubBlockEnd.gif        }

151InBlock.gif        public virtual int GetValues(object [] values) 
152ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
153InBlock.gif            return _subject.GetValues(values);
154ExpandedSubBlockEnd.gif        }

155InBlock.gif        public virtual bool IsDBNull(int i)
156ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
157InBlock.gif            return _subject.IsDBNull(i);
158ExpandedSubBlockEnd.gif        }

159InBlock.gif        // methods for IDataReader
160InBlock.gif        public virtual int RecordsAffected
161ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
162InBlock.gif            get
163ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
164InBlock.gif                return _subject.RecordsAffected;
165ExpandedSubBlockEnd.gif            }

166ExpandedSubBlockEnd.gif        }

167InBlock.gif        public virtual void Close()
168ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
169InBlock.gif            _subject.Close();
170ExpandedSubBlockEnd.gif        }

171InBlock.gif        public virtual DataTable GetSchemaTable()
172ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
173InBlock.gif            return _subject.GetSchemaTable();
174ExpandedSubBlockEnd.gif        }

175InBlock.gif        public virtual bool NextResult()
176ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
177InBlock.gif            return _subject.NextResult();
178ExpandedSubBlockEnd.gif        }

179InBlock.gif        public virtual bool Read()
180ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
181InBlock.gif            return _subject.Read();
182ExpandedSubBlockEnd.gif        }

183InBlock.gif        // methods for IDisposable
184InBlock.gif        public virtual void Dispose()
185ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
186InBlock.gif            _subject.Dispose();
187ExpandedSubBlockEnd.gif        }

188ExpandedSubBlockEnd.gif    }

189ExpandedBlockEnd.gif}
 
ContractedBlock.gif ExpandedBlockStart.gif 重写的特别代理
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing DataLayer;
 4None.gif
 5ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
 6InBlock.gif/// Show that we know get our hooks into access to a data reader
 7ExpandedBlockEnd.gif/// </summary>

 8None.gifpublic class LimitingReader : DataReaderProxy
 9ExpandedBlockStart.gifContractedBlock.gifdot.gif{
10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
11InBlock.gif    /// Just here to capture the subject
12InBlock.gif    /// </summary>
13ExpandedSubBlockEnd.gif    /// <param name="subject">the reader we are a proxy for</param>

14InBlock.gif    public LimitingReader(IDataReader subject) : base (subject)
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16ExpandedSubBlockEnd.gif    }

17ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
18InBlock.gif    /// Show that we can intercept requests for apogee information.
19ExpandedSubBlockEnd.gif    /// </summary>

20InBlock.gif    public override object this [string name]
21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
22InBlock.gif        get
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            if (String.Compare(name, "apogee"true== 0// same 
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                return 0;
27ExpandedSubBlockEnd.gif            }

28InBlock.gif            else 
29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
30InBlock.gif                return base [name];
31ExpandedSubBlockEnd.gif            }

32ExpandedSubBlockEnd.gif        }

33ExpandedSubBlockEnd.gif    }

34ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif DataService类
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Data.OleDb;
 4None.gifusing System.Reflection;
 5None.gifusing System.IO;
 6None.gif
 7None.gifnamespace Gof.Test.Proxy
 8ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 9InBlock.gif    public delegate object BorrowReader(IDataReader reader);
10InBlock.gif    public class DataService
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif        public DataService()
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
14InBlock.gif        public static object GetDataReader(string sel,BorrowReader borrow)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            try
17ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
18InBlock.gif                using(OleDbConnection con = CreateConnection())
19ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
20InBlock.gif                    con.Open();
21InBlock.gif                    OleDbCommand com = new OleDbCommand();
22InBlock.gif                    com.Connection = con;
23InBlock.gif                    com.CommandText = sel;
24InBlock.gif                    OleDbDataReader reader = com.ExecuteReader();
25InBlock.gif                    return borrow(reader);
26ExpandedSubBlockEnd.gif                }

27ExpandedSubBlockEnd.gif            }

28InBlock.gif            catch(Exception ex)
29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
30InBlock.gif                throw ex;//不处理了
31ExpandedSubBlockEnd.gif            }

32ExpandedSubBlockEnd.gif        }

33InBlock.gif        public static OleDbConnection CreateConnection()
34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
35InBlock.gif            string fileName = GetFileName("db","oozinoz.mdb");
36InBlock.gif            OleDbConnection con = new OleDbConnection();
37InBlock.gif            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileName;
38InBlock.gif            return con;
39ExpandedSubBlockEnd.gif        }

40InBlock.gif        public static String GetFileName(String dirName, String fileName)
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            String path;
43InBlock.gif            // Can we find the file using the OOZINOZ environment variable?
44InBlock.gif            String oozinozBase = Environment.GetEnvironmentVariable("OOZINOZ");
45InBlock.gif            if (oozinozBase != null
46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
47InBlock.gif                path = Path.Combine(Path.Combine(oozinozBase, dirName), fileName);
48InBlock.gif                if (File.Exists(path))
49ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
50InBlock.gif                    return path;
51ExpandedSubBlockEnd.gif                }

52ExpandedSubBlockEnd.gif            }

53InBlock.gif            // How 'bout relative to where the bin files are?
54InBlock.gif            Assembly a = Assembly.GetAssembly(typeof(DataService));
55InBlock.gif            DirectoryInfo thisDir = Directory.GetParent(a.Location);
56InBlock.gif            DirectoryInfo parentDir = Directory.GetParent(thisDir.FullName);
57InBlock.gif            path = Path.Combine(
58InBlock.gif                parentDir.FullName, 
59InBlock.gif                dirName + Path.DirectorySeparatorChar + fileName);
60InBlock.gif            if (File.Exists(path))
61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
62InBlock.gif                return path;
63ExpandedSubBlockEnd.gif            }

64InBlock.gif            // Ok, how 'bout in the top-level directory?
65InBlock.gif            path = Path.Combine(Path.Combine(@"ConsoleApplication2", dirName), fileName);
66InBlock.gif            if (File.Exists(path))
67ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
68InBlock.gif                return path;
69ExpandedSubBlockEnd.gif            }

70InBlock.gif            // dang
71InBlock.gif            throw new Exception("FileFinder.GetFileName() cannot find " + fileName + " in directory " + dirName);
72InBlock.gif
73ExpandedSubBlockEnd.gif        }

74ExpandedSubBlockEnd.gif    }

75ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 客户代码
 1None.gif    string sel = "SELECT * FROM ROCKET";
 2None.gif            Gof.Test.Proxy.DataService.GetDataReader(sel, new Gof.Test.Proxy.BorrowReader(GetNames));
 3None.gif
 4None.gif    private static Object GetNames(System.Data.IDataReader reader)
 5ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 6InBlock.gif            System.Data.IDataReader proxy = new LimitingReader(reader);
 7InBlock.gif            while (proxy.Read()) 
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 9InBlock.gif                Console.Write("{0,10} ", proxy["Name"]);
10InBlock.gif                Console.Write("{0,7:C} ", proxy["price"]);
11InBlock.gif                Console.Write("{0,5}", proxy["apogee"]);
12InBlock.gif                Console.WriteLine();
13ExpandedSubBlockEnd.gif            }

14InBlock.gif            Console.ReadLine();
15InBlock.gif            return null;
16ExpandedBlockEnd.gif        }

最后

以上就是傻傻小笼包为你收集整理的Proxy 为其他对象提供一个代理(surrogate)或者占位符(placeholder),从而完成对其他对象的访问。...的全部内容,希望文章能够帮你解决Proxy 为其他对象提供一个代理(surrogate)或者占位符(placeholder),从而完成对其他对象的访问。...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部