Home > Backend Development > Python Tutorial > Introduction to the simple factory pattern of python3

Introduction to the simple factory pattern of python3

黄舟
Release: 2017-10-17 10:23:40
Original
2090 people have browsed it

This article mainly introduces the simple factory pattern of python3 design pattern in detail, which has certain reference value. Interested friends can refer to it

In the Python3 environment, debugging is implemented " The simple factory pattern in "Dahua Design Pattern" completes the instantiation of specific products by defining a separate factory class. Reference link

See the code for specific implementation:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Date : 2017-10-15 21:46:28
# Author : John
# Version : V1.001
# Func :


class Operator(object):
 """docstring for Operator"""

 def __init__(self, NumberA=0, NumberB=0):
 super(Operator, self).__init__()
 self.NumberA = NumberA
 self.NumberB = NumberB

 def GetResult(self):
 pass


class AddOp(Operator):
 """docstring for AddOp"""

 def GetResult(self):
 return int(float(self.NumberA) + float(self.NumberB))


class MinusOp(Operator):
 """docstring for MinusOp"""

 def GetResult(self):
 return int(float(self.NumberA) - float(self.NumberB))


class MultiOp(Operator):
 """docstring for MultiOp"""

 def GetResult(self):
 return int(float(self.NumberA) * float(self.NumberB))


class pideOp(Operator):
 """docstring for pideOp"""

 def GetResult(self):
 try:
  return float(float(self.NumberA) / float(self.NumberB) * 1.0)
 except ZeropisionError as e:
  print("pideOp error, {0}".format(e))


class OperatorFactory(object):
 """docstring for OperatorFactory"""

 def ChooseOperator(self, op):
 if op == '+':
  return AddOp()
 if op == '-':
  return MinusOp()
 if op == '*':
  return MultiOp()
 if op == '/':
  return pideOp()


if __name__ == '__main__':
 ch = ''
 while not ch == 'q':
 NumberA = input('Please input NumberA: ')
 op = input('Please input operator: ')
 NumberB = input('Please input NumberB: ')

 factory = OperatorFactory()
 opType = factory.ChooseOperator(op)
 opType.NumberA = NumberA
 opType.NumberB = NumberB

 print('The result is: {0}'.format(opType.GetResult()))
 print('\n#-- input q to exit any key to continue')

 try:
  ch = str(input())
 except Exception as e:
  print('Get input error: {0}'.format(e))
  print('Use default value to ch')
  ch = ''
Copy after login

The above is the detailed content of Introduction to the simple factory pattern of python3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template