Protection Proxy (C#)
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2020/10/02 16:40 UTC 版)
「Proxy パターン」の記事における「Protection Proxy (C#)」の解説
以下のC#の例では、RealClient はアカウント番号を格納する。正しいパスワードを知っているユーザーだけが、このアカウント番号にアクセスできる。RealClient はパスワードを知っている ProtectionProxy で守られている。ユーザーがアカウント番号を得たい場合、まずプロキシがユーザーに対して認証を求め、ユーザーが正しいパスワードを入力したときだけプロキシが RealClient を呼び出してアカウント番号をユーザーに通知する。 この例では、正しいパスワードは thePassword である。 using System;namespace ConsoleApplicationTest.FundamentalPatterns.ProtectionProxyPattern{ public interface IClient { string GetAccountNo(); } public class RealClient : IClient { private string accountNo = "12345"; public RealClient() { Console.WriteLine("RealClient: Initialized"); } public string GetAccountNo() { Console.WriteLine("RealClient's AccountNo: " + accountNo); return accountNo; } } public class ProtectionProxy : IClient { private string password; //秘密のパスワード RealClient client; public ProtectionProxy(string pwd) { Console.WriteLine("ProtectionProxy: Initialized"); password = pwd; client = new RealClient(); } // ユーザーを認証し、アカウント番号を返す public String GetAccountNo() { Console.Write("Password: "); string tmpPwd = Console.ReadLine(); if (tmpPwd == password) { return client.GetAccountNo(); } else { Console.WriteLine("ProtectionProxy: Illegal password!"); return ""; } } } class ProtectionProxyExample { [STAThread] public static void Main(string[] args) { IClient client = new ProtectionProxy("thePassword"); Console.WriteLine(); Console.WriteLine("main received: " + client.GetAccountNo()); Console.WriteLine("\nPress any key to continue . . ."); Console.Read(); } }}
※この「Protection Proxy (C#)」の解説は、「Proxy パターン」の解説の一部です。
「Protection Proxy (C#)」を含む「Proxy パターン」の記事については、「Proxy パターン」の概要を参照ください。
- Protection Proxyのページへのリンク