Emiliatan

Simple Factory

Feb 3rd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace operationFactory
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Operation oper;
  14.             oper = Factory.createOperation("+");
  15.             oper.NumberA = 100;
  16.             oper.NumberB = 100;
  17.             double result = oper.getResult();
  18.             Console.WriteLine(oper.NumberA.ToString() + " + " + oper.NumberB.ToString() + " = " + result.ToString());
  19.             Console.Read();
  20.         }
  21.     }
  22.  
  23.  
  24.  
  25.     public class Operation
  26.     {
  27.         private double _numA;
  28.         private double _numB;
  29.  
  30.         public double NumberA
  31.         {
  32.             get { return _numA; }
  33.             set { _numA = value; }
  34.         }
  35.         public double NumberB
  36.         {
  37.             get { return _numB; }
  38.             set { _numB = value; }
  39.         }
  40.         public virtual double getResult()
  41.         {
  42.             return 0;
  43.         }
  44.     }
  45.     class OperationAdd : Operation
  46.     {
  47.         public override double getResult()
  48.         {
  49.             double result = 0;
  50.             result = NumberA + NumberB;
  51.             return result;
  52.         }
  53.     }
  54.  
  55.     class OperationSub : Operation
  56.     {
  57.         public override double getResult()
  58.         {
  59.             double result = 0;
  60.             result = NumberA - NumberB;
  61.             return result;
  62.         }
  63.     }
  64.  
  65.     class OperationMul : Operation
  66.     {
  67.         public override double getResult()
  68.         {
  69.             double result = 0;
  70.             result = NumberA * NumberB;
  71.             return result;
  72.         }
  73.     }
  74.  
  75.     class OperationDiv : Operation
  76.     {
  77.         public override double getResult()
  78.         {
  79.             double result = 0;
  80.             if (NumberB == 0)
  81.                 throw new Exception("Error!");
  82.             result = NumberA / NumberB;
  83.             return result;
  84.         }
  85.     }
  86.  
  87.     public class Factory
  88.     {
  89.         public static Operation createOperation(string operate)
  90.         {
  91.             Operation oper = null;
  92.             switch (operate)
  93.             {
  94.                 case "+":
  95.                     oper = new OperationAdd();
  96.                     break;
  97.                 case "-":
  98.                     oper = new OperationSub();
  99.                     break;
  100.                 case "*":
  101.                     oper = new OperationMul();
  102.                     break;
  103.                 case "/":
  104.                     oper = new OperationDiv();
  105.                     break;
  106.                 default:
  107.                     throw new Exception("Error!");
  108.             }
  109.             return oper;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment