写点什么

架构师训练营第 1 期 week3

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



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



为了方便起见,所有代码暂时都放到一个类里了。

import java.util.ArrayList;
import java.util.List;
public class Combination {
public abstract class Component{
String name;
Component(String name){
this.name = name;
}
abstract void print();
}
public class Picture extends Component{
Picture(String name){
super(name);
}
@Override
void print() {
System.out.println("print Picture("+this.name+")");
}
}
public class Button extends Component{
Button(String name){
super(name);
}
@Override
void print() {
System.out.println("print Button("+this.name+")");
}
}

public class Label extends Component{
Label(String name){
super(name);
}
@Override
void print() {
System.out.println("print Label("+this.name+")");
}
}
public class TextBox extends Component{
TextBox(String name){
super(name);
}
@Override
void print() {
System.out.println("print TextBox("+this.name+")");
}
}
public class PassWordBox extends Component{
PassWordBox(String name){
super(name);
}
@Override
void print() {
System.out.println("print PassWordBox("+this.name+")");
}
}
public class CheckBox extends Component{
CheckBox(String name){
super(name);
}
@Override
void print() {
System.out.println("print CheckBox("+this.name+")");
}
}
public class LinkLable extends Component{
LinkLable(String name){
super(name);
}
@Override
void print() {
System.out.println("print LinkLable("+this.name+")");
}
}

public class Window extends Component{
Window(String name){
super(name);
}
List<Component> list = new ArrayList<Component>();
public void addComponent(Component component){
this.list.add(component);
}
@Override
void print() {
System.out.println("print WinForm("+this.name+")");
for(Component c:list){
c.print();
}
}
}


public class Frame extends Component{
Frame(String name){
super(name);
}
List<Component> list = new ArrayList<Component>();
public void addComponent(Component component){
this.list.add(component);
}
@Override
void print() {
System.out.println("print Frame("+this.name+")");
for(Component c:list){
c.print();
}
}
}
public static void main(String[] args) {
Combination combination = new Combination();
Window cc = combination.new Window("WINDOW窗口");
cc.addComponent( combination.new Picture("LOGO图片"));
cc.addComponent( combination.new Button("登录"));
cc.addComponent( combination.new Button("注册"));
Frame frame = combination.new Frame("FRAME1");
frame.addComponent(combination.new Label("用户名"));
frame.addComponent(combination.new TextBox("文本框"));
frame.addComponent(combination.new Label("密码"));
frame.addComponent(combination.new PassWordBox("密码框"));
frame.addComponent(combination.new CheckBox("复选框"));
frame.addComponent(combination.new TextBox("记住用户名"));
frame.addComponent(combination.new LinkLable("忘记密码"));
cc.addComponent(frame);
cc.print();
}
}




用户头像

张建亮

关注

还未添加个人签名 2020.07.29 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第 1 期 week3