What do you call a more awesome object? is actually a higher level object--managing objects
What is a Management object:
An object used to manage other objects that can tie everything together
The role of the management object:
Delegating work to other methods, simply delegating other methods to work in a well-defined order
All right! A bit of dizzy feeling! I thought of an example, such as a system patching.
Operating systems patching typically takes three steps:
1. Download Patches
2. Installation Patches
3. Restart the system
So, managing objects just make sure that these 3 steps happen sequentially!!
The following code is just a simulation of the event, see the following code:
class host: def __init__ (SELF, HOSTNAME, IP): self.hostname = hostname self.ip = ip def patch_repair (self): #管理者方法 (patch fix Order) self.download_patch () #下载 self.install_patch () #安装 self.reboot_system () #重启 def download_patch (self): #补丁下载 print ("{} Start downloading the patch ". Format (self.hostname)) def install_ Patch (self): #补丁安装 print ("{} start&nbsP;install patch ". Format (Self.hostname)) def reboot_system (self): #重启系统 print ("{} start reboot system ". Format (self.hostname)) if __name__ == ' __main__ ': h = host ("pc001", "192.168.89.67") h.patch_repair ()
There seems to be nothing wrong! So what are the benefits of doing this?
1, enhance readability, each step of the code is independent,
2, enhanced scalability, if the subclass just download the patch method, then you can directly inherit and overwrite it, rewrite it
3, enhance the isolation, an external class can create an instance of this class, you can directly use one of the methods, rather than Patch_repair
python--Create a more awesome object