Design Mode learning-simple factory mode (python3) and design mode python3

Source: Internet
Author: User
Tags class operator

Design Mode learning-simple factory mode (python3) and design mode python3

Reference Links: In the Python3 environment, debugging has implemented the simple factory mode in the big talk design mode. by defining a separate factory class, you can instantiate a specific product.

For specific implementation, see the code:

 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # Date    : 2017-10-15 21:46:28 4 # Author  : John 5 # Version : V1.001 6 # Func    : 7  8  9 class Operator(object):10     """docstring for Operator"""11 12     def __init__(self, NumberA=0, NumberB=0):13         super(Operator, self).__init__()14         self.NumberA = NumberA15         self.NumberB = NumberB16 17     def GetResult(self):18         pass19 20 21 class AddOp(Operator):22     """docstring for AddOp"""23 24     def GetResult(self):25         return int(float(self.NumberA) + float(self.NumberB))26 27 28 class MinusOp(Operator):29     """docstring for MinusOp"""30 31     def GetResult(self):32         return int(float(self.NumberA) - float(self.NumberB))33 34 35 class MultiOp(Operator):36     """docstring for MultiOp"""37 38     def GetResult(self):39         return int(float(self.NumberA) * float(self.NumberB))40 41 42 class DivideOp(Operator):43     """docstring for DivideOp"""44 45     def GetResult(self):46         try:47             return float(float(self.NumberA) / float(self.NumberB) * 1.0)48         except ZeroDivisionError as e:49             print("DivideOp error, {0}".format(e))50 51 52 class OperatorFactory(object):53     """docstring for OperatorFactory"""54 55     def ChooseOperator(self, op):56         if op == '+':57             return AddOp()58         if op == '-':59             return MinusOp()60         if op == '*':61             return MultiOp()62         if op == '/':63             return DivideOp()64 65 66 if __name__ == '__main__':67     ch = ''68     while not ch == 'q':69         NumberA = input('Please input NumberA: ')70         op = input('Please input operator: ')71         NumberB = input('Please input NumberB: ')72 73         factory = OperatorFactory()74         opType = factory.ChooseOperator(op)75         opType.NumberA = NumberA76         opType.NumberB = NumberB77 78         print('The result is: {0}'.format(opType.GetResult()))79         print('\n#-- input q to exit any key to continue')80 81         try:82             ch = str(input())83         except Exception as e:84             print('Get input error: {0}'.format(e))85             print('Use default value to ch')86             ch = ''

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.