-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleFactory.py
More file actions
50 lines (38 loc) · 1000 Bytes
/
SimpleFactory.py
File metadata and controls
50 lines (38 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# @Time : 2022/08/16 19:41:50
# @Author : Gary
# @Email : None
class Animal():
# 抽象产品类
name = ""
weight = 0.0
type = "Animal"
def get_weight(self):
return self.weight
def set_weight(self, weight):
self.price = weight
def get_name(self):
return self.name
class dog(Animal):
# 具体产品类
def __init__(self):
self.name = "dog"
self.weight = 40.0
class cat(Animal):
# 具体产品类
def __init__(self):
self.name = "cat"
self.weight = 5.0
class animalFactory():
# 简单工厂类
def create(self, animal):
if animal == 'dog':
animalIns = dog()
elif animal == 'cat':
animalIns = cat()
return animalIns
if __name__ == "__main__":
animal_factory = animalFactory()
dog = animal_factory.create("dog")
print(dog.get_name(), dog.get_weight()) # dog 40.0