训练营 - 第三周 - 作业一

用户头像
行者
关注
发布于: 2020 年 10 月 03 日

手写单例模式代码

饿汉模式

懒汉模式

在写的过程中,发现对于synchronized volatile拼写不熟练。

用组合设计模式编写程序,输出指定内容

/**
* 组件
*/
public interface Component {
/**
* 打印
*/
abstract void print();
}



import java.util.List;
/**
* win form
*/
public class WinForm implements Component {
private String name;
private List<Component> items;
public WinForm(String name, List<Component> items) {
this.name = name;
this.items = items;
}
@Override
public void print() {
System.out.println("print WinForm(" + name + ")");
if (items != null) {
for (Component item : items) {
item.print();
}
}
}
}



import java.util.List;
/**
* button
*/
public class Frame implements Component {
private String name;
private List<Component> items;
public Frame(String name, List<Component> items) {
this.name = name;
this.items = items;
}
@Override
public void print() {
System.out.println("print Frame(" + name + ")");
if (items != null) {
for (Component item : items) {
item.print();
}
}
}
}



/**
* button
*/
public class Button implements Component {
private String name;
public Button(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print Button(" + name + ")");
}
}



/**
* check box
*/
public class CheckBox implements Component {
private String name;
public CheckBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print CheckBox(" + name + ")");
}
}



/**
* lable
*/
public class Lable implements Component {
private String name;
public Lable(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print Lable(" + name + ")");
}
}



/**
* link lable
*/
public class LinkLable implements Component {
private String name;
public LinkLable(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print LinkLable(" + name + ")");
}
}



/**
* password box
*/
public class PasswordBox implements Component {
private String name;
public PasswordBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print PasswordBox(" + name + ")");
}
}



/**
* picture
*/
public class Picture implements Component {
private String name;
public Picture(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print Picture(" + name + ")");
}
}



/**
* text box
*/
public class TextBox implements Component {
private String name;
public TextBox(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print TextBox(" + name + ")");
}
}



import java.util.Arrays;
/**
* main
*/
public class Main {
public static void main(String[] args) {
Frame frame = new Frame("FRAME1",
Arrays.asList(new Lable("用户名"),
new TextBox("文本框"),
new Lable("密码"),
new PasswordBox("密码框"),
new CheckBox("复选框"),
new TextBox("记住用户名"),
new LinkLable("忘记密码")));
new WinForm("WINDOW窗口",
Arrays.asList(new Picture("LOGO图片"),
new Button("登录"),
new Button("注册"), frame)).print();
}
}

通过组合模式,把组件组成了树型结构,易于扩展。

用户头像

行者

关注

还未添加个人签名 2018.03.09 加入

还未添加个人简介

评论

发布
暂无评论
训练营 - 第三周 - 作业一