单例模式和组合模式
发布于: 2020 年 06 月 24 日
单例的实现
懒汉式会按需去加载,但锁的粒度比较大
用双重校验锁时,静态属性需定义成volatile,避免初始化对象时指令重排序导致未初始化完成就返回了实例引用,在使用实例时产生异常。
内部类可以安全的实现懒加载,而且避免了使用锁
2.用组合设计模式编写模式,打印输出图1的窗口,窗口组件的树结构如图2所示,打印输出实例参考图3
UML类图
/** * * @Description: 界面组件 * * */public interface Component { public void print(); public String getName(); default String getSimpleClassName() { return this.getClass().getSimpleName(); }}
/** * * @Description: 容器,可添加额外组件 * * */public interface Container extends Component{ public void addComponent(Component... components); }
/** * * @Description: 组件的抽象实现 * */public abstract class AbstractComponent implements Component{ private final String name; public AbstractComponent(String name) { this.name = name; } @Override public void print() { System.out.println(String.format("print %s(%s)", this.getInfo(), this.getName())); } @Override public String getName() { return name; } protected abstract String getInfo();}
/** * * @Description: 容器的抽象实现 * */public abstract class AbstractContainer implements Container { private final String name; private List<Component> childComponent = new ArrayList<>(); public AbstractContainer(String name) { this.name = name; } @Override public void addComponent(Component... components) { childComponent.addAll(Lists.newArrayList(components)); } @Override public void print() { System.out.println(String.format("print %s(%s)", this.getMyPrint(), this.getName())); childComponent.stream().forEach(component -> component.print()); } protected abstract String getMyPrint(); public String getName() { return name; } }
/** * * @Description: 按钮 CheckBox/Label/LinkLabel/TextBox/PasswordBox * */public class Button extends AbstractComponent{ public Button(String name) { super(name); } @Override protected String getInfo() { return getSimpleClassName(); }}
/** * * @Description: FRAME * */public class Frame extends AbstractContainer { public Frame(String name) { super(name); } @Override public String getMyPrint() { return this.getSimpleClassName(); }}
/** * * @Description: LOGO图片 * */public class Picture extends AbstractContainer { public Picture(String name) { super(name); } @Override public String getMyPrint() { return this.getSimpleClassName(); }}
/** * * @Description: windowss * */public class WinForm extends AbstractContainer { public WinForm(String name) { super(name); } @Override public String getMyPrint() { return this.getSimpleClassName(); } public static void main(String[] args) { WinForm winForm = new WinForm("WINDOW窗口"); Picture picture = new Picture("LOGO图片"); Button loginButton = new Button("登录"); Button registerButton = new Button("注册"); Frame frame1 = new Frame("FRAME1"); Label userNameLabel = new Label("用户名"); TextBox textBox = new TextBox("文本框"); Label passwdLabel = new Label("密码"); PasswordBox passwordBox = new PasswordBox("密码框"); TextBox rememberMeTextBox = new TextBox("记住用户名"); LinkLabel linkLable = new LinkLabel("忘记密码"); frame1.addComponent(userNameLabel, textBox, passwdLabel, passwordBox, rememberMeTextBox, linkLable); winForm.addComponent(picture, loginButton, registerButton, frame1); winForm.print(); } }
结果:print WinForm(WINDOW窗口)print Picture(LOGO图片)print Button(登录)print Button(注册)print Frame(FRAME1)print Label(用户名)print TextBox(文本框)print Label(密码)print PasswordBox(密码框)print TextBox(记住用户名)print LinkLabel(忘记密码)
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 48
allen
关注
还未添加个人签名 2019.02.26 加入
还未添加个人简介
评论