写点什么

第三周作业

用户头像
Griffenliu
关注
发布于: 2020 年 11 月 08 日
  1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。



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



public interface Component {
void draw();
}
public class Container implements Component {
private List<Component> components;
public Container() {
components = new ArrayList<Component>();
}
public void add(Component comp){
this.components.add(comp);
}
public void draw(){
for(comp in components){
comp.draw();
}
}
public class Picture implements Componenet {
private String name;
public Picture(String name){
this.name = name;
}
public void draw() {
System.out.println("print Picture("+ this.name +")");
}
}
public class Button implements Componenet {
private String label;
public Button(String label){
this.label = label;
}
public void draw() {
System.out.println("print Button(" + this.label + ")");
}
}
public class Label implements Componenet {
private String label;
public Label(String label){
this.label = label;
}
public void draw() {
System.out.println("print Label(" + this.label + ")");
}
}
public class CheckBox implements Component {
private String label;
public CheckBox(String label){
this.label = label;
}
public void draw() {
System.out.println("print CheckBox(" + this.label + ")");
}
}
public class TextBox implements Component {
private String label;
public TextBox(String label){
this.label = label;
}
public void draw() {
System.out.println("print TextBox(" + this.label + ")");
}
}
public class LinkLabel implements Component {
private String label;
public LinkLabel(String label){
this.label = label;
}
public void draw() {
System.out.println("print LinkLabel(" + this.label + ")");
}
}
public class PasswordBox implements Component {
private String label;
public PasswordBox(String label){
this.label = label;
}
public void draw() {
System.out.println("print PasswordBox(" + this.label + ")");
}
}
public class Frame extends Container {
private String title;
public Frame(String title){
this.title = title;
}
public void draw(){
System.out.println("print Frame(" + this.title + ")");
super.draw();
}
}
public class WinForm extends Component {
private String title;
public WinForm(String title){
this.title = title;
}
public void draw(){
System.out.println("print WinForm(" + this.title + ")");
super.draw();
}
}
public class Main {
public static void main() {
WinForm w = new WinForm("WINDOW窗口");
w.add(new Picture("LOGO图片"));
w.add(new Button("登录"));
w.add(new Button("注册"));
Frame frame = new Frame("FRAME1");
frame.add(new Label("用户名"));
frame.add(new TextBox("文本框"));
frame.add(new Label("密码"));
frame.add(new PasswordBox("密码框"));
frame.add(new CheckBox("复选框"));
frame.add(new TextBox("记住用户名"));
frame.add(new LinkLabel("忘记密码"));
w.add(frame);
w.draw();
}
}



用户头像

Griffenliu

关注

还未添加个人签名 2020.07.05 加入

还未添加个人简介

评论

发布
暂无评论
第三周作业