写点什么

week3. 课后作业

发布于: 2020 年 06 月 21 日

1.手写单例模式

饱汉式、懒汉式、双重加锁、静态内部类方式实现单例模式





2.组合设计模式打印窗口组件的树结构

package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:06
*/
public interface IComponent {
void printName();
}



package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:07
*/
public class Component implements IComponent{
protected String name;
public Component(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println("print Component(" + this.name + ")");
}
}



package com.demo.frame;
import java.util.ArrayList;
import java.util.List;
/**
* @author niki-lauda
* @create 2020-06-24 23:50
*/
public class ComplexComponent extends Component{
private List<IComponent> components = new ArrayList<>();
public ComplexComponent(String name) {
super(name);
}
public void addChild(IComponent component) {
components.add(component);
}
@Override
public void printName() {
for (IComponent component : components) {
component.printName();
}
}
}



package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:11
*/
public class ButtonComponent extends Component{
public ButtonComponent(String name) {
super(name);
}
@Override
public void printName() {
System.out.println("print Button(" + name + ")");
}
}



package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:11
*/
public class CheckBoxComponent extends Component{
public CheckBoxComponent(String name) {
super(name);
}
@Override
public void printName() {
System.out.println("print CheckBox(" + name + ")");
}
}



package com.demo.frame;
import java.util.ArrayList;
import java.util.List;
/**
* @author niki-lauda
* @create 2020-06-21 18:11
*/
public class FrameComponent extends ComplexComponent{
public FrameComponent(String name) {
super(name);
}
@Override
public void printName() {
System.out.println("print Frame(" + this.name + ")");
super.printName();
}
}



package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:11
*/
public class LabelComponent extends Component{
public LabelComponent(String name) {
super(name);
}
@Override
public void printName() {
System.out.println("print Label(" + name + ")");
}
}



package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:11
*/
public class LinkLableComponent extends Component{
public LinkLableComponent(String name) {
super(name);
}
@Override
public void printName() {
System.out.println("print LinkLable(" + name + ")");
}
}



package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:11
*/
public class PasswordBoxComponent extends Component{
public PasswordBoxComponent(String name) {
super(name);
}
@Override
public void printName() {
System.out.println("print PasswordBox(" + name + ")");
}
}



package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:11
*/
public class PictureComponent extends Component{
public PictureComponent(String name) {
super(name);
}
@Override
public void printName() {
System.out.println("print Picture(" + name + ")");
}
}



package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:11
*/
public class TextBoxComponent extends Component{
public TextBoxComponent(String name) {
super(name);
}
@Override
public void printName() {
System.out.println("print TextBox(" + name + ")");
}
}



package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:11
*/
public class WinFormComponent extends ComplexComponent{
public WinFormComponent(String name) {
super(name);
}
@Override
public void printName() {
System.out.println("print WinForm(" + name + ")");
super.printName();
}
}



package com.demo.frame;
/**
* @author niki-lauda
* @create 2020-06-21 18:24
*/
public class TestFrame {
public static void main(String[] args) {
WinFormComponent winFormComponent = new WinFormComponent("WINDOWS窗口");
PictureComponent pictureComponent = new PictureComponent("LOGO图片");
winFormComponent.addChild(pictureComponent);
ButtonComponent loginButtonComponent = new ButtonComponent("登录");
winFormComponent.addChild(loginButtonComponent);
ButtonComponent registerButtonComponent = new ButtonComponent("注册");
winFormComponent.addChild(registerButtonComponent);
FrameComponent frameComponent = new FrameComponent("FRAME1");
winFormComponent.addChild(frameComponent);
LabelComponent labelComponent = new LabelComponent("用户名");
frameComponent.addChild(labelComponent);
TextBoxComponent textBoxComponent = new TextBoxComponent("文本框");
frameComponent.addChild(textBoxComponent);
LabelComponent passwordLabel = new LabelComponent("密码");
frameComponent.addChild(passwordLabel);
PasswordBoxComponent passwordBoxComponent = new PasswordBoxComponent("密码框");
frameComponent.addChild(passwordBoxComponent);
CheckBoxComponent checkBoxComponent = new CheckBoxComponent("复选框");
frameComponent.addChild(checkBoxComponent);
TextBoxComponent rememberMeComponent = new TextBoxComponent("记住用户名");
frameComponent.addChild(rememberMeComponent);
LinkLableComponent linkLableComponent = new LinkLableComponent("忘记密码");
frameComponent.addChild(linkLableComponent);
winFormComponent.printName();
}
}

打印结果

print WinForm(WINDOWS窗口)
print Picture(LOGO图片)
print Button(登录)
print Button(注册)
print Frame(FRAME1)
print Label(用户名)
print TextBox(文本框)
print Label(密码)
print PasswordBox(密码框)
print CheckBox(复选框)
print TextBox(记住用户名)
print LinkLable(忘记密码)



发布于: 2020 年 06 月 21 日阅读数: 58
用户头像

做一个真诚、坦诚的行人,追求自由。 2018.07.30 加入

还未添加个人简介

评论

发布
暂无评论
week3.课后作业