第三节课后作业

用户头像
happy
关注
发布于: 2020 年 10 月 08 日

标题

  1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。

  2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出

解答

1:单例如下

2:

@Data
public abstract class Windows {
String name;
List<Windows> components;

public void add(Windows compenent){
if(null == components){
components = new ArrayList<>();
}
components.add(compenent);
}

public void setup(){
return ;
}

public void viewComponents(){
if(null != components){
components.forEach(x->{
System.out.println(x.getName());
if(null != x.getComponents()){
x.getComponents().forEach(y->{
System.out.println(y.getName());
});
}

});
}
}
}


@Data
public class Button extends Windows {

public Button(String name){
this.name = name;
}
}
@Data
public class CheckBox extends Windows{
public CheckBox (String name){
this.name = name;
}
}

@Data
public class Form extends Windows{

public Form(String name){
this.name = name;
this.setup();
}

@Override
public void setup(){

this.add(new Picture( "LOGO图片"));
this.add(new Button("登录"));
this.add(new Button("注册"));
this.add(new Frame("frame1"));

}

}
@Data
public class Frame extends Windows {

public Frame(String name){
this.name = name;
this.setup();
this.name = name;
}

@Override
public void setup(){
this.add(new Label( "用户名"));
this.add(new TextBox("文本框"));
this.add(new Label("密码"));
this.add(new PasswordBox("密码框"));
this.add(new CheckBox("复选框"));
this.add(new TextBox("记住用户名"));
this.add(new LinkLable("忘记密码"));
}
}
@Data
public class Label extends Windows{
public Label (String name){
this.name = name;
}

}
@Data
public class LinkLable extends Windows{
public LinkLable (String name){
this.name = name;
}
}
@Data
public class PasswordBox extends Windows{

public PasswordBox (String name){
this.name = name;
}
}
@Data
public class Picture extends Windows {
private String url;

public Picture (String name){
this.name = name;
}
}
@Data
public class TextBox extends Windows{

public TextBox (String name){
this.name = name;
}
}




用户头像

happy

关注

还未添加个人签名 2019.09.14 加入

还未添加个人简介

评论

发布
暂无评论
第三节课后作业