写点什么

架构师训练营第三周作业

用户头像
CATTY
关注
发布于: 2020 年 06 月 21 日
架构师训练营第三周作业

1. 手写单例模式



  1. 组合模式编写程序

IComponent接口:

public interface IComponent {
/**
* 打印
*/
void print();
}



BaseComponent类:

public class BaseComponent implements IComponent {
private String name;
public BaseComponent(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + getClass().getSimpleName() + "(" + name + ")");
}
}



ComponentGroup类:

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class ComponentGroup extends BaseComponent {
private List<IComponent> components = new ArrayList<>();
public ComponentGroup(String name) {
this(name, null);
}
public ComponentGroup(String name, Collection<IComponent> comps) {
super(name);
if (comps != null) {
components.addAll(comps);
}
}
public void addComponent(IComponent comp) {
if (comp != null) {
components.add(comp);
}
}
@Override
public final void print() {
super.print();
components.stream().forEach(IComponent::print);
}
}



Button类:

/**
* 按钮
*/
public class Button extends BaseComponent {
public Button(String name) {
super(name);
}
}



CheckBox类:

/**
* 复选框
*/
public class CheckBox extends BaseComponent {
public CheckBox(String name) {
super(name);
}
}



Frame类:

/**
* 框架
*/
public class Frame extends ComponentGroup {
public Frame(String name) {
super(name);
}
}



Label类:

/**
* 标签
*/
public class Label extends BaseComponent {
public Label(String name) {
super(name);
}
}



LinkLabel类:

/**
* 带链接的标签
*/
public class LinkLabel extends BaseComponent {
public LinkLabel(String name) {
super(name);
}
}



PasswordBox类:

/**
* 密码框
*/
public class PasswordBox extends BaseComponent {
public PasswordBox(String name) {
super(name);
}
}



Picture类:

/**
* 图片
*/
public class Picture extends BaseComponent {
public Picture(String name) {
super(name);
}
}



TextBox类:

/**
* 文本框
*/
public class TextBox extends BaseComponent {
public TextBox(String name) {
super(name);
}
}



WinForm类:

/**
* 窗口
*/
public class WinForm extends ComponentGroup {
public WinForm(String name) {
super(name);
}
}



MainClient主客户端类:

public class MainClient {
public static void main(String[] args) {
WinForm window = new WinForm("WINDOW窗口");
window.addComponent(new Picture("LOGO图片"));
window.addComponent(new Button("登录"));
window.addComponent(new Button("注册"));
Frame frame = new Frame("FRAME1");
window.addComponent(frame);
frame.addComponent(new Label("用户名"));
frame.addComponent(new TextBox("文本框"));
frame.addComponent(new Label("密码"));
frame.addComponent(new PasswordBox("密码框"));
frame.addComponent(new CheckBox("复选框"));
frame.addComponent(new Label("记住用户名"));
frame.addComponent(new LinkLabel("忘记密码"));
window.print();//打印
}
}



用户头像

CATTY

关注

还未添加个人签名 2019.12.29 加入

还未添加个人简介

评论

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