写点什么

架构师训练营第三周作业

用户头像
Geek_xq
关注
发布于: 2020 年 12 月 11 日
  1. 手写单例模式实现代码



  1. 用组合实现模式实现窗口打印

《主要是为了demo一下思想,就没有每个类放到单独的文件里了》

*********************************printWin.py***************************************

import abc

import yaml

import sys



class ElementBase(object):

metaclass = abc.ABCMeta

def init(self, label):

self.label = label

@abc.abstractmethod

def print(self):

"""print element"""

return



class Button(ElementBase):

def print(self):

print("Button({})".format(self.label))



class Picture(ElementBase):

def print(self):

print("Picture({})".format(self.label))



class TextBox(ElementBase):

def print(self):

print("TextBox({})".format(self.label))



class PasswordBox(ElementBase):

def print(self):

print("PasswordBox({})".format(self.label))



class LinkLabel(ElementBase):

def print(self):

print("LinkLabel({})".format(self.label))



class CheckBox(ElementBase):

def print(self):

print("CheckBox({})".format(self.label))



class Frame(ElementBase):

def init(self, label):

self.elements = []

super(Frame,self).init(label)

def print(self):

print("Frame({})".format(self.label))

for element in self.elements:

element.print()

def add_element(self, element):

self.elements.append(element)



class WinForm(ElementBase):

def init(self, label):

self.elements = []

super(WinForm,self).init(label)



def add_element(self, element):

self.elements.append(element)



def print(self):

print("WinForm")

for element in self.elements:

element.print()



def create_tree(node):

node_obj = getattr(obj,node['type'])(node['name'])

if 'elements' in node.keys():

for sub_node in node['elements']:

subnodeobj = createtree(subnode)

nodeobj.addelement(subnodeobj)

return node_obj



if name == "main":

obj = sys.modules[name]

with open("config.yaml","r") as f:

config = yaml.load(f, Loader=yaml.FullLoader)

root = create_tree(config)

root.print()

*****************************************************************************************

**********************config.yaml************************************************

name: winform

type: WinForm

elements:

- name: logo

type: Picture

- name: frame1

type: Frame

elements:

- name: User

type: TextBox

- name: Password

type: PasswordBox

- name: remember username

type: CheckBox

- name: forget password

type: LinkLabel

- name: login

type: Button

- name: register

type: Button

************************************************************************************

*********************output******************************************

[root@tmp]# python3 printWin.py

WinForm

Picture(logo)

Frame(frame1)

TextBox(User)

PasswordBox(Password)

CheckBox(remember username)

LinkLabel(forget password)

Button(login)

Button(register)



用户头像

Geek_xq

关注

还未添加个人签名 2020.10.15 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第三周作业