写点什么

架构师训练营第三课作业

用户头像
曾祥斌
关注
发布于: 2020 年 06 月 24 日

1.手写单例模式



2.组合模式打印window,代码如下



class WinForm父类根节点

public abstract class WinForm {
protected String name;
public WinForm(String name)
{
this.name = name;
}
public abstract void addChild(WinForm w);
public abstract void removeChild(WinForm w);
public abstract void print();
}



class Frame树枝节点用来处理树枝逻辑



import java.util.ArrayList;
import java.util.List;
public class Frame extends WinForm{
private List<WinForm> children = new ArrayList<WinForm>();
public Frame(String name)
{
super(name);
}
@Override
public void addChild(WinForm w) {
children.add(w);
}
@Override
public void removeChild(WinForm w) {
children.remove(w);
}
@Override
public void print(){
System.out.println("print " + name);
for (WinForm f : children) {
f.print();
}
}
}



class Contral叶子节点用来处理叶子逻辑



public class Contral extends WinForm {
public Contral(String name)
{
super(name);
}
@Override
public void addChild(WinForm w) {
}
@Override
public void removeChild(WinForm w) {
}
@Override
public void print(){
System.out.println("print " + name);
}
}



class Play用于调用main函数打印树结构

public class Play {
public static void main(String[] args)
{
WinForm winForm = new Frame("WinFrom(WINDOW窗口)");
winForm.addChild(new Contral("picture(LOGO图片)"));
winForm.addChild(new Contral("button(登录)"));
winForm.addChild(new Contral("button(注册)"));
WinForm frame = new Frame("Frame(FRAME1)");
winForm.addChild(frame);
frame.addChild(new Contral("Label(用户名)"));
frame.addChild(new Contral("TextBox(文本框)"));
frame.addChild(new Contral("Label(密码)"));
frame.addChild(new Contral("PasswordBox(密码框)"));
frame.addChild(new Contral("CheckBox(复选框)"));
frame.addChild(new Contral("TextBox(记住用户名)"));
frame.addChild(new Contral("LinkLable(忘记密码)"));
winForm.print();
}
}



用户头像

曾祥斌

关注

还未添加个人签名 2020.02.19 加入

还未添加个人简介

评论

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