架构师训练营第 3 周作业
发布于: 2020 年 06 月 24 日
1、手写一个单例模式的实现代码
单例模式的常用的实现方式有几种:一是通过类加载时就实例化一个对象;二是通过延迟加载的方式,需要用才实例;三是二的变形,采用双重检测的方法;四是采用内部类的方法等
2、请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
public abstract class Component { protected String content; public Component(String content) { this.content = content; } public abstract void print();}public abstract class FrameComponent extends Component{ protected List<Component> listComponent = new ArrayList<>(); public FrameComponent(String content) { super(content); } public void printAll() { print(); for(Component component: listComponent) { if(component instanceof FrameComponent){ ((FrameComponent)component).printAll(); }else { component.print(); } } } public void addComponent(Component component) { listComponent.add(component); }}public class Button extends Component { public Button(String content) { super(content); } public void print(){ System.out.println("print Button(" + content +")"); }}public class CheckBox extends Component { public CheckBox(String content) { super(content); } public void print(){ System.out.println("print CheckBox(" + content +")"); }}public class Frame extends FrameComponent { public Frame(String content){ super(content); } public void print() { System.out.println("print Frame(" + content +")"); }}public class Lable extends Component { public Lable(String content) { super(content); } public void print(){ System.out.println("print Lable(" + content +")"); }}public class LinkLable extends Component { public LinkLable(String content) { super(content); } public void print(){ System.out.println("print LinkLable(" + content +")"); }}public class PassswordBox extends Component { public PassswordBox(String content) { super(content); } public void print(){ System.out.println("print PasswordBox(" + content +")"); }}public class Picture extends Component { public Picture(String content) { super(content); } public void print(){ System.out.println("print Picture(" + content +")"); }}public class TextBox extends Component { public TextBox(String content) { super(content); } public void print(){ System.out.println("print TextBox(" + content +")"); }}public class WinForm extends FrameComponent { public WinForm(String content){ super(content); } public void print() { System.out.println("print WinForm(" + content +")"); }}public class Demo { public static void main(String[] args) { WinForm winForm = new WinForm("WINDOW窗口"); Picture picture = new Picture("LOGO图片"); Button buttonLogin = new Button("登录"); Button buttonReg = new Button("注册"); Frame frame = new Frame("FRAME1"); winForm.addComponent(picture); winForm.addComponent(buttonLogin); winForm.addComponent(buttonReg); winForm.addComponent(frame); Lable lableUser = new Lable("用户名"); Lable lablePw = new Lable("密码"); TextBox textBox = new TextBox("文本框"); TextBox textBoxUser = new TextBox("记住用户名"); PassswordBox passswordBox = new PassswordBox("密码框"); CheckBox checkBox = new CheckBox("复选框"); LinkLable linkLable = new LinkLable("忘记密码"); frame.addComponent(lableUser); frame.addComponent(textBox); frame.addComponent(lablePw); frame.addComponent(passswordBox); frame.addComponent(checkBox); frame.addComponent(textBoxUser); frame.addComponent(linkLable); winForm.printAll(); }}
划线
评论
复制
发布于: 2020 年 06 月 24 日 阅读数: 29
子豪sirius
关注
还未添加个人签名 2018.05.03 加入
还未添加个人简介
评论