博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
proxy-spacebook.cs
阅读量:6957 次
发布时间:2019-06-27

本文共 2497 字,大约阅读时间需要 8 分钟。

  using System;
  using System.Collections.Generic;
  // Proxy Pattern Example              Judith Bishop  Aug 2007
  // Sets up a SpaceBook page with registration and authentication
  class SpaceBookSystem {    
    // The Subject
    private class SpaceBook {
      static SortedList <string,SpaceBook> community =
            new SortedList <string,SpaceBook> (100);
      string pages;
      string name;
      string gap = "\n\t\t\t\t";
      static public bool IsUnique (string name) {
         return community.ContainsKey(name);
      }
      internal SpaceBook (string n) {
         name = n;
         community [n] = this;
      }
      internal void Add(string s) {
         pages += gap+s;
         Console.Write(gap+"======== "+name+"'s SpaceBook =========");
         Console.Write(pages);
         Console.WriteLine(gap+"===================================");
      }
      internal void Add(string friend, string message) {
         community[friend].Add(message);
      }
      internal void Poke (string who, string friend) {
         community[who].pages += gap + friend + " poked you";
      }
    }
    // The Proxy
    public class MySpaceBook {
      // Combination of a virtual and authentication proxy
      SpaceBook mySpaceBook;
      string password;
      string name;
      bool loggedIn = false;
      void Register () {
         Console.WriteLine("Let's register you for SpaceBook");
         do {
            Console.WriteLine("All SpaceBook names must be unique");
            Console.Write("Type in a user name: ");
            name = Console.ReadLine();
         } while (SpaceBook.IsUnique(name));
         Console.Write("Type in a password: ");
         password = Console.ReadLine();
         Console.WriteLine("Thanks for registering with SpaceBook");
      }
      bool Authenticate () {
         Console.Write("Welcome "+name+". Please type in your password: ");
         string supplied = Console.ReadLine();
         if (supplied==password) {
            loggedIn = true;
            Console.WriteLine("Logged into SpaceBook");
            if (mySpaceBook == null)
              mySpaceBook = new SpaceBook(name);
            return true;
         }
         Console.WriteLine("Incorrect password");
         return false;
      }
      public void Add(string message) {
         Check();
         if (loggedIn) mySpaceBook.Add(message);
      }
      public void Add(string friend, string message) {
         Check();
         if (loggedIn)
            mySpaceBook.Add(friend, name + " said: "+message);
      }
      public void Poke(string who) {
         Check();
         if (loggedIn)
              mySpaceBook.Poke(who,name);
      }
      void Check() {
        if (!loggedIn)
          if (password==null)
            Register();
          if (mySpaceBook == null)
            Authenticate();
        }
      }
    }
  // The Client
  class ProxyPattern : SpaceBookSystem {
    static void Main () {
      MySpaceBook me = new MySpaceBook();
      me.Add("Hello world");
      me.Add("Today I worked 18 hours");
      MySpaceBook tom = new MySpaceBook();
      tom.Poke("Judith");
      tom.Add("Judith","Poor you");
      tom.Add("Off to see the Lion King tonight");
    }
  }

转载地址:http://zjmil.baihongyu.com/

你可能感兴趣的文章
从原理分析Kotlin的延迟初始化: lateinit var和by lazy
查看>>
【翻译】Postmortem-debugging-Go-services-with-Delve
查看>>
Picasso-源码解析(一)
查看>>
MongoDB数据库基础操作
查看>>
再谈前后端API签名安全?
查看>>
css:transform处理兼容
查看>>
ThreadPoolExecutor源码分析
查看>>
双向数据绑定实现原理
查看>>
【重要】统一管理碎片代码,跟混乱代码说再见
查看>>
公司来了个“奇葩”的程序员
查看>>
JS中的箭头函数与this
查看>>
回顾·机器学习/深度学习工程实战
查看>>
单点登录(SSO)简介
查看>>
鸿篇巨制 —— LevelDB 的整体架构
查看>>
【Python3网络爬虫开发实战】3.1.4-分析Robots协议
查看>>
Excel通用类工具(二)
查看>>
iOS核心动画高级技术(十一) 基于定时器的动画
查看>>
源码阅读:AFNetworking(十五)——UIRefreshControl+AFNetworking
查看>>
从GCD到NSOperation之NSOperation
查看>>
Kubernetes API server工作原理
查看>>