C ++ explains design patterns

  Seeing Daniel's incisive comments on the side dishes in the big talk design mode, I also wrote a calculator program, and followed the comments all the way to improve. It was still not fun. I applied the class template and exception capture mechanism to rewrite the program.
  
  This article cannot be considered dry goods. The proper analogy is dessert before a meal. It tastes a little bit bitter and interesting. It is always too small to be full.
  
   #include <stdlib.h>
  
  #include <stdio.h>
  
  #include <iostream>
  
  #include <Windows.h>
  
  #include <exception>
  
  using namespace std;
  
  template <typename T>
  
  class myOperation
  
  {
  
  public:
  
  virtual void SetNumberA (const T & val) {NumberA = val;}
  
  virtual void SetNumberB (const T & val) {NumberB = val;}
  
  virtual T GetNumberA () {return NumberA;}
  
  virtual T GetNumberB () {return NumberB;}
  
  myOperation (const T & valA, const T & valB) {NumberA = valA; NumberB = valB;}
  
  myOperation () {NumberA = 0; NumberB = 0;}
  
  myOperation(const myOperation& other){NumberA = other.NumverA; NumverB = other.NumberB;}
  
  ~myOperation() {}
  
  virtual T Operation() = 0;
  
  private:
  
  T NumberA;
  
  T NumberB;
  
  };
  
  template <typename T>
  
  class myPlusOperation : public myOperation<T>
  
  {
  
  public:
  
  T Operation();
  
  myPlusOperation(){}
  
  myPlusOperation(const T& valA, const T& valB):myOperation(valA, valB){}
  
  myPlusOperation(const myPlusOperation& other){myOperation(other);}
  
  ~myPlusOperation(){}
  
  };
  
  template <typename T>
  
  class myMinusOperation : public myOperation<T>
  
  {
  
  public:
  
  T Operation();
  
  myMinusOperation() {}
  
  myMinusOperation(const T& valA, const T& valB):myOperation(valA, valB){}
  
  myMinusOperation(const myMinusOperation& other){myOperation(other);}
  
  ~myMinusOperation(){}
  
  };
  
  template <typename T>
  
  class myMultipleOperation : public myOperation<T>
  
  {
  
  public:
  
  T Operation();
  
  myMultipleOperation() {}
  
  myMultipleOperation(const T& valA, const T& valB):myOperation(valA, valB){}
  
  myMultipleOperation(const myMultipleOperation& other){myOperation(other);}
  
  ~myMultipleOperation(){}
  
  };
  
  template <typename T>
  
  class myDivideOperation : public myOperation<T>
  
  {
  
  public:
  
  T Operation();
  
  myDivideOperation() {}
  
  myDivideOperation(const T& valA, const T& valB):myOperation(valA, valB){}
  
  myDivideOperation(const myDivideOperation& other){myOperation(other);}
  
  ~myDivideOperation(){}
  
  };
  
  template <typename T>
  
  T myPlusOperation<T>::Operation()
  
  {
  
  return GetNumberA()+GetNumberB();
  
  };
  
  template <typename T>
  
  T myMinusOperation<T>::Operation()
  
  {
  
  return GetNumberA()-GetNumberB();
  
  };
  
  template <typename T>
  
  T myMultipleOperation<T>::Operation()
  
  {
  
  return GetNumberA()*GetNumberB();
  
  };
  
  template <typename T>
  
  T myDivideOperation <T> :: Operation ()
  
  {
  
  if (GetNumberB () == 0)
  
  throw exception ("Divisor cannot be 0"); // Throw object, catch reference can capture
  
  return GetNumberA () / GetNumberB () ;
  
  };
  
  template <typename T>
  
  class myOperationFactory
  
  {
  
  public:
  
  static myOperation <T> * CreatemyOperation (char operate); // The factory only provides static methods to get class instances.
  
  };
  
  template <typename T>
  
  myOperation <T> * myOperationFactory <T> :: CreatemyOperation (char operate)
  
  {
  
  myOperation <T> * oper = NULL; // The returned pointer has an instantiation type, specify <T>
  
  switch (operate )
  
  {
  
  case '+':
  
  oper = new myPlusOperation <T>; // new objects need to specify the instantiation type <T>
  
  break;
  

  
  oper = new myMinusOperation<T>;
  
  break;
  
  case ‘*‘:
  
  oper = new myMultipleOperation<T>;
  
  break;
  
  case ‘/‘:
  
  oper = new myDivideOperation<T>;
  
  break;
  
  default:
  
  throw new exception("Operation isn‘t correct"); //new出来的异常,catch对应的指针可以捕捉到
  
  break;
  
  }
  
  return oper;
  
  };
  
  void foo44()
  
  {
  
  try{
  
  while(1)
  
  {
  
  int NumberA = 0, NumberB = 0, Result = 0;
  
  char Oper = ‘\0‘;
  
  char Enter = ‘\0‘;
  
  printf("please input NumberA\n");
  
  scanf("%d", &NumberA);
  
  printf("please input NumberB\n");
  
  scanf("%d", &NumberB);
  
  printf ("please input operator \ n");
  
  scanf ("% c% c", & Enter, & Oper); // scanf gets the next matching input from the current input stream, and the carriage return and line feed spaces are regarded as valid characters
  
  myOperation <int> * Operation = myOperationFactory <int> :: CreatemyOperation (Oper);
  
  Operation-> SetNumberA (NumberA);
  
  Operation-> SetNumberB (NumberB);
  
  Result = Operation-> Operation ();
  
  printf ("Result is% d \ n ", Result);
  
  delete Operation;
  
  }
  
  / * myPlusOperation <int> Operation (1, 10);
  
  int iResult = Operation.Operation ();
  
  printf (" iResult is% d \ n ", iResult); * /
  
  }
  
  / / After the exception is caught, the code of the try block is skipped, and the program continues to process the subsequent code
  
  catch (exception * ex)
  
  {
  
  cout << ex-> what () << endl;
  
  }
  
  catch (exception& ex)
  
  {
  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  
  The exception can be understood as the program can not complete some work, we can continue to try to complete other work.
  
  Simple factory model
  
  Simple factory can better support the same base class derived from several derived class scenarios, such as the addition, subtraction, multiplication and division of this article, the base class provides operands and operation interfaces, derived classes with different responsibilities rewrite the operation function, put The implementation of differentiated functions is handed over to the derived class. This design method is convenient for adding and modifying functions. Each function modification only needs to modify a class, and the new function only needs to add a new class and add branches to the class factory. As far as the user is concerned, the instantiation of the difference content is isolated. When the user uses it, he only needs to ask "Hey, I want to add an operation, and you return me an object pointer that can operate the addition."
  
  The above paragraph describes the three elements of a simple factory: the
  
  role of the factory, which is called by the outside world and creates a product object, and usually provides a static method to obtain the product object.
  
  The abstract product role, the parent class of all concrete products, contains a common interface common to all instances.
  
  There are usually multiple specific product roles, specific targets created by the factory, and executors of detailed work.

Guess you like

Origin www.cnblogs.com/zhenhua1618/p/12729752.html