架构师训练营 第三周 设计模式作业

发布于: 2020 年 06 月 24 日

作业一

请在草稿纸上手写一个单例模式的实现代码。

图 手写单例模式代码图

作业二

请用组合设计模式编写程序,打印输出图1的窗口,窗口组件的树结构如图2所示,打印输出示例参考图3.

图1 窗口图例

图2 窗口组件树结构

图3 程序输出示例

源代码

UML类图

图4 组合模式UML类图

代码结构

图5 组合模式示例项目结构

源代码

package com.xianyanyang.designmode.composite;
/**
* Windows组件接口
*/
public interface WindowsComponent {
/**
* 打印组件内容
*/
void draw();
}

package com.xianyanyang.designmode.composite;
/**
* Windows组合组件接口
*/
public interface WindowsComponsiteComponent extends WindowsComponent {
/**
* 添加子组件
*
* @param child 子组件
* @return Windows组合组件
*/
void addComponent(WindowsComponent child);
}

package com.xianyanyang.designmode.composite.folder;
import com.xianyanyang.designmode.composite.WindowsComponent;
import com.xianyanyang.designmode.composite.WindowsComponsiteComponent;
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class LoginFrameComponent implements WindowsComponsiteComponent {
private Collection<WindowsComponent> children = Collections.emptyList();
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print Frame(FRAME1)");
if (CollectionUtils.isEmpty(children)) {
return;
}
children.forEach(child -> child.draw());
}
/**
* 添加子组件
*
* @param child 子组件
* @return Windows组件
*/
@Override
public void addComponent(WindowsComponent child) {
if (CollectionUtils.isEmpty(children)) {
children = new ArrayList<>();
}
children.add(child);
}
}

package com.xianyanyang.designmode.composite.folder;
import com.xianyanyang.designmode.composite.WindowsComponent;
import com.xianyanyang.designmode.composite.WindowsComponsiteComponent;
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class WinFormComponent implements WindowsComponsiteComponent {
private Collection<WindowsComponent> children = Collections.emptyList();
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print WinForm(WINDOW 窗口)");
if (CollectionUtils.isEmpty(children)) {
return;
}
children.forEach(child -> child.draw());
}
/**
* 添加子组件
*
* @param child 子组件
* @return Windows组件
*/
@Override
public void addComponent(WindowsComponent child) {
if (CollectionUtils.isEmpty(children)) {
children = new ArrayList<>();
}
children.add(child);
}
}

package com.xianyanyang.designmode.composite.leaf;
import com.xianyanyang.designmode.composite.WindowsComponent;
public class LoginButtonComponent implements WindowsComponent {
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print Button(登录)");
}
}

package com.xianyanyang.designmode.composite.leaf;
import com.xianyanyang.designmode.composite.WindowsComponent;
public class LoginNameLabelComponent implements WindowsComponent {
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print Label(用户名)");
}
}

package com.xianyanyang.designmode.composite.leaf;
import com.xianyanyang.designmode.composite.WindowsComponent;
public class LoginNameRememberCheckBoxComponent implements WindowsComponent {
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print CheckBox(复选框)");
}
}

package com.xianyanyang.designmode.composite.leaf;
import com.xianyanyang.designmode.composite.WindowsComponent;
public class LoginNameRememberTextBoxComponent implements WindowsComponent {
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print TextBox(记住用户名)");
}
}

package com.xianyanyang.designmode.composite.leaf;
import com.xianyanyang.designmode.composite.WindowsComponent;
public class LoginNameTextBoxComponent implements WindowsComponent {
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print TextBox(文本框)");
}
}

package com.xianyanyang.designmode.composite.leaf;
import com.xianyanyang.designmode.composite.WindowsComponent;
public class LoginPasswordBoxComponent implements WindowsComponent {
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print PasswordBox(密码框)");
}
}

package com.xianyanyang.designmode.composite.leaf;
import com.xianyanyang.designmode.composite.WindowsComponent;
/**
* 忘记密码控件
*/
public class LoginPasswordForgotLinkLabelComponent implements WindowsComponent {
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print LinkLabel(忘记密码)");
}
}

package com.xianyanyang.designmode.composite.leaf;
import com.xianyanyang.designmode.composite.WindowsComponent;
public class LoginPasswordLabelComponent implements WindowsComponent {
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print Label(密码)");
}
}

package com.xianyanyang.designmode.composite.leaf;
import com.xianyanyang.designmode.composite.WindowsComponent;
public class LogoComponent implements WindowsComponent {
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print Picture(LOGO 图片)");
}
}

package com.xianyanyang.designmode.composite.leaf;
import com.xianyanyang.designmode.composite.WindowsComponent;
public class RegistryButtonComponent implements WindowsComponent {
/**
* 打印组件内容
*/
@Override
public void draw() {
System.out.println("print Button(注册)");
}
}

单元测试用例

package com.xianyanyang.designmode.composite.folder;
import com.xianyanyang.designmode.composite.WindowsComponsiteComponent;
import com.xianyanyang.designmode.composite.leaf.*;
import junit.framework.TestCase;
public class WinFormComponentTest extends TestCase {
private WinFormComponent winFormComponent;
@Override
public void setUp() {
winFormComponent = new WinFormComponent();
}
public void testCompositeWindowsForm() {
WindowsComponsiteComponent frame = new LoginFrameComponent();
frame.addComponent(new LoginNameLabelComponent());
frame.addComponent(new LoginNameTextBoxComponent());
frame.addComponent(new LoginPasswordLabelComponent());
frame.addComponent(new LoginPasswordBoxComponent());
frame.addComponent(new LoginNameRememberCheckBoxComponent());
frame.addComponent(new LoginNameRememberTextBoxComponent());
frame.addComponent(new LoginPasswordForgotLinkLabelComponent());
this.winFormComponent.addComponent(new LogoComponent());
this.winFormComponent.addComponent(new LoginButtonComponent());
this.winFormComponent.addComponent(new RegistryButtonComponent());
this.winFormComponent.addComponent(frame);
this.winFormComponent.draw();
}
}

运行结果

"D:\Program Files\Java\jdk-10\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.5\lib\idea_rt.jar=56864:D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.5\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.5\plugins\junit\lib\junit-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.5\plugins\junit\lib\junit5-rt.jar;E:\book\architect-design\target\test-classes;E:\book\architect-design\target\classes;F:\repository\org\apache\commons\commons-collections4\4.4\commons-collections4-4.4.jar;F:\repository\junit\junit\4.13\junit-4.13.jar;F:\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit3 com.xianyanyang.designmode.composite.folder.WinFormComponentTest,testCompositeWindowsForm
print WinForm(WINDOW 窗口)
print Picture(LOGO 图片)
print Button(登录)
print Button(注册)
print Frame(FRAME1)
print Label(用户名)
print TextBox(文本框)
print Label(密码)
print PasswordBox(密码框)
print CheckBox(复选框)
print TextBox(记住用户名)
print LinkLabel(忘记密码)
Process finished with exit code 0

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

且听且吟

关注

没有绝世高手 2018.06.30 加入

还未添加个人简介

评论

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