python pattern study notes

 1 class a:
 2     def __init__(self,arg):
 3         self.data = arg
 4 
 5     def reminder(self):
 6         print('{} is a number'.format(self.data))
 7         self.data = self.data**2
 8         return self.data
 9 class b:
10     def __init__(self,arg):
11         self.data = arg
12 
13     def reminder(self):
14         print('{} is a str'.format(self.data))
15         self.data = self.data+' nice to meet you'
16         return self.data
17 
18 def factory(arg):
19     connect = None
20     if isinstance(arg,int):
21         connect = a
22     elif isinstance(arg,str):
23         connect = b
24     else:
25         raise ValueError('sorry !')
26     return connect(arg)
27 
28 def main():
29     s = 5
30     #s = 'jim'
31     
32     modelfactory = factory(s)
33     result = modelfactory.reminder()
34     print(result)
35 
36 if __name__ == '__main__':
37     main()

The first chapter factory mode

First, why use the factory pattern (Factory Method)

1. Tracking instance of an object

2. create and use objects speak decoupling

3. Create objects when necessary, to improve performance

Second, the basis of usage patterns

1. Create a factory method (characteristic parameters):

(1) one or several dispensers:

The characteristic parameters used to determine points which factory

(2) return to the factory object (i.e. factory class)

2. Create two or more factory class

(1) has the same method name

 

 

 

Guess you like

Origin www.cnblogs.com/kaiseryin/p/11963072.html