• 《Cracking the Coding Interview》——第8章:面向对象设计——题目2


    2014-04-23 17:45

    题目:假设有个呼叫中心,有接线员、经理、主管三种角色。如果接线员无法处理呼叫,就上传给经理;如果仍无法处理,则上传给主管。请用代码描述这一过程。

    解法:第一眼觉得这题肯定是在考察设计模式,很像exception的throw过程。对于我这种对设计模式一窍不通的人,这题还无法很好解答。待稍后专门学习设计模式之后,再回来好好琢磨一遍。

    代码:

      1 // 8.2 Design a call center system to handle calls, the order will be respondent->manager->director, find the first employee to handle a call.
      2 #include <iostream>
      3 #include <vector>
      4 using namespace std;
      5 
      6 class People {
      7 public:
      8     People() {};
      9     virtual void handle() = 0;
     10     virtual ~People() {};
     11 };
     12 
     13 class Respondent: public People {
     14 public:
     15     Respondent() {};
     16     void handle() {
     17         cout << "Respondent is handling the call." << endl;
     18     };
     19     ~Respondent() {};
     20 };
     21 
     22 class Manager: public People {
     23 public:
     24     Manager() {};
     25     void handle() {
     26         cout << "Manager is handling the call." << endl;
     27     };
     28     ~Manager() {};
     29 };
     30 
     31 class Director: public People {
     32 public:
     33     Director() {};
     34     void handle() {
     35         cout << "Director is handling the call." << endl;
     36     };
     37     ~Director() {};
     38 };
     39 
     40 class CallCenter {
     41 public:
     42     CallCenter(int num_respondent = 0, int num_manager = 0, int num_director = 0) {
     43         respondents.resize(num_respondent);
     44         managers.resize(num_manager);
     45         directors.resize(num_director);
     46         respondents_available.resize(num_respondent);
     47         managers_available.resize(num_manager);
     48         directors_available.resize(num_director);
     49         fill(respondents_available.begin(), respondents_available.end(), true);
     50         fill(managers_available.begin(), managers_available.end(), true);
     51         fill(directors_available.begin(), directors_available.end(), true);
     52     }
     53     
     54     void handle() {
     55         size_t i;
     56         
     57         for (i = 0; i < respondents.size(); ++i) {
     58             if (respondents_available[i]) {
     59                 break;
     60             }
     61         }
     62         if (i < respondents.size()) {
     63             respondents_available[i] = false;
     64             respondents[i].handle();
     65             respondents_available[i] = true;
     66             return;
     67         }
     68 
     69         for (i = 0; i < managers.size(); ++i) {
     70             if (managers_available[i]) {
     71                 break;
     72             }
     73         }
     74         if (i < managers.size()) {
     75             managers_available[i] = false;
     76             managers[i].handle();
     77             managers_available[i] = true;
     78             return;
     79         }
     80         
     81         for (i = 0; i < directors.size(); ++i) {
     82             if (directors_available[i]) {
     83                 break;
     84             }
     85         }
     86         if (i < directors.size()) {
     87             directors_available[i] = false;
     88             directors[i].handle();
     89             directors_available[i] = true;
     90             return;
     91         }
     92     }
     93 private:
     94     vector<Respondent> respondents;
     95     vector<Manager> managers;
     96     vector<Director> directors;
     97     vector<bool> respondents_available;
     98     vector<bool> managers_available;
     99     vector<bool> directors_available;
    100 };
    101 
    102 int main()
    103 {
    104     CallCenter *p_call_center = nullptr;
    105     int r, m, d;
    106     
    107     while (cin >> r >> m >> d) {
    108         p_call_center = new CallCenter(r, m, d);
    109         p_call_center->handle();
    110         delete p_call_center;
    111         p_call_center = nullptr;
    112     }
    113     
    114     return 0;
    115 }
  • 相关阅读:
    PowerDesigner快捷键
    Android 的开源电话/通讯/IM聊天项目全集
    Android ContentProvider完整案例
    Android中观察者模式的升入理解
    Android中Socket大文件断点上传
    Storm概念学习系列之Tuple元组(数据载体)
    开始使用storm
    Storm概念学习系列之storm的功能和三大应用
    Storm概念学习系列之storm的特性
    Storm概念学习系列之storm核心组件
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3683530.html
Copyright © 2020-2023  润新知