-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
100 lines (83 loc) · 2.64 KB
/
main.py
File metadata and controls
100 lines (83 loc) · 2.64 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Date : 2020/8/19
# @Author : Bruce Liu /Lin Luo
# @Mail : 15869300264@163.com
import sys
from structural.bridge.example import ProgramAbstraction, Implementor, mac_system_location, linux_system_location, \
windows_system_location
class MacImp(Implementor):
"""
模拟一个mac的implementor
"""
def get_location(self) -> (float, float):
"""
mac系统中获取坐标信息
假设mac系统返回坐标的api为mac_sysytem_location
返回的坐标为lon和lat,类型为字符串
:return:
"""
lon, lat = mac_system_location()
return float(lon), float(lat)
class LinuxImp(Implementor):
"""
模拟一个Linux系统的implementor
"""
def get_location(self) -> (float, float):
"""
linux系统中获取坐标的信息
假设linux系统返回坐标的api为linux_system_location
返回的坐标为一个字符串
:return:
"""
lon, lat = linux_system_location().split(',')
return float(lon), float(lat)
class WindowsImp(Implementor):
"""
模拟一个Windows系统的Implementor
"""
def get_location(self) -> (float, float):
"""
windows系统中获取坐标的信息
假设windoes系统返回坐标的api为windows_system_location
返回的坐标为一个字符串
:return:
"""
return windows_system_location()
class Program(ProgramAbstraction):
def __init__(self):
super(Program, self).__init__()
self._get_imp()
def _get_imp(self) -> Implementor:
"""
:return:
"""
if not self._imp:
platform = sys.platform
# 判断当前操作系统
if platform == 'darwin':
# mac
print('system: mac')
self._imp = MacImp()
elif platform == 'win32':
# windows
print('system: windows')
self._imp = WindowsImp()
elif platform == 'linux':
# linux
print('system: linux')
self._imp = LinuxImp()
else:
# 其他
pass
return self._imp
def upload(self):
lon, lat = self._get_imp().get_location()
# 执行上传操作
print(f'get location info lon:{lon}, lat:{lat}')
if __name__ == '__main__':
# 生成一个程序,然后执行, 后去当前系统,并生成对应imp
obj = Program()
print('upload info')
# 调用imp对象,获取系统的坐标信息,并上传
obj.upload()