第三周设计模式作业

发布于: 2020 年 06 月 24 日

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

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

代码如下:

WinForm.java

package com.example.demo;
public abstract class WinForm {
private String name;
public WinForm(String name) {
this.name = name;
}
public WinForm(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected abstract void add(WinForm winForm);
protected abstract void romove(WinForm winForm);
protected abstract void display(int depth);
}

Frame.java

package com.example.demo;
import java.util.ArrayList;
import java.util.List;
public class Frame extends WinForm {
private List<WinForm> cList;
public Frame(){
cList = new ArrayList<WinForm>();
}
public Frame(String name){
super(name);
cList = new ArrayList<WinForm>();
}
@Override
protected void add(WinForm winForm) {
// TODO Auto-generated method stub
cList.add(winForm);
}
@Override
protected void romove(WinForm winForm) {
// TODO Auto-generated method stub
cList.remove(winForm);
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
for (WinForm c : cList) {
c.display(depth + 2);
}
}
}

Button.java

package com.example.demo;
public class Button extends WinForm {
public Button(String name){
super(name);
}
@Override
protected void add(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void romove(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
}
}

CheckBox.java

package com.example.demo;
public class CheckBox extends WinForm {
public CheckBox(String name){
super(name);
}
@Override
protected void add(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void romove(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
}
}

Lable.java

package com.example.demo;
public class Lable extends WinForm {
public Lable(String name){
super(name);
}
@Override
protected void add(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void romove(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
}
}

LinkLable.java

package com.example.demo;
public class LinkLable extends WinForm {
public LinkLable(String name){
super(name);
}
@Override
protected void add(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void romove(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
}
}

PasswordBox.java

package com.example.demo;
public class PasswordBox extends WinForm {
public PasswordBox(String name){
super(name);
}
@Override
protected void add(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void romove(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
}
}

Picture.java

package com.example.demo;
public class Picture extends WinForm {
public Picture(String name){
super(name);
}
@Override
protected void add(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void romove(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
}
}

TextBox.java

package com.example.demo;
public class TextBox extends WinForm {
public TextBox(String name){
super(name);
}
@Override
protected void add(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void romove(WinForm winForm) {
// TODO Auto-generated method stub
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
}
}

main代码

public static void main(String[] args) {
WinForm root = new Frame();
root.setName("WINDOW窗口");
root.add(new Picture("Logo图片"));
root.add(new Button("登录"));
root.add(new Button("注册"));
WinForm iFrame = new Frame("FRAME1");
iFrame.add(new Lable("用户名"));
iFrame.add(new TextBox("文本框"));
iFrame.add(new Lable("密码"));
iFrame.add(new PasswordBox("密码框"));
iFrame.add(new CheckBox("复选框"));
iFrame.add(new TextBox("记住用户名"));
iFrame.add(new LinkLable("忘记密码"));
root.add(iFrame);
root.display(0);
}

运行结果:

WINDOW窗口
--Logo图片
--登录
--注册
--FRAME1
----用户名
----文本框
----密码
----密码框
----复选框
----记住用户名
----忘记密码

作业二:根据当周学习情况,完成一篇学习总结

用户头像

关注

还未添加个人签名 2018.04.25 加入

还未添加个人简介

评论

发布
暂无评论
第三周设计模式作业