第三周 架构方法学习总结
本周内容比较细致,通过老师的讲解对这常用的设计模式又了进一层的认识。之前的了解相对散碎,通过老师讲解很多知识点一下被激化成链。
一、本章节受益的主要内容是面向对象的设计模式,内容包括:
定义;
分类;
工厂模式、单例模式、适配器模式、策略模式、组合模式、装饰模式;
应用举例:JUnit、Spring 中的应用;
案例分析:Intel 大数据SQL引擎&Panthera设计模式
二、作业:
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。


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

package com.geekbang
public interface Windowform {
public void print();
}
package com.geekbang
import java.util.ArrayList;
import java.util.List;
public class Frame implements Windowform {
private List<Windowform> childComponents = new ArrayList<Windowform>();
private String name;
public Frame(String name) {
this.name = name;
}
public void Add(Windowform w) {
childComponents.add(w);
}
public void Remove(Windowform w) {
childComponents.remove(w);
}
@Override
public void print(){
System.out.println(this.name);
}
}
package com.geekbang
public class Widget implements Windowform {
private String name;
public Widget(String name) {
this.name = name;
}
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println(name);
}
}
package com.geekbang
public class CompositePattern {
public static void main(String[] args) {
Windowform windowform = new Frame("WINDOW窗口");
Windowform frame2 = new Frame("FRAME1");
Windowform picture = new Widget("LOGO图片");
Windowform buttonLogin = new Widget("登录");
Windowform buttonRegister = new Widget("注册");
((Frame) windowform).Add(frame2);
((Frame) windowform).Add(picture);
((Frame) windowform).Add(buttonLogin);
((Frame) windowform).Add(buttonRegister);
windowform.print();
picture.print();
buttonLogin.print();
buttonRegister.print();
frame2.print();
Windowform lableUsernaem = new Widget("用户名");
Windowform textBoxText = new Widget("文本框");
Windowform lablePassword = new Widget("密码");
Windowform passwordBox = new Widget("密码框");
Windowform CheckBox = new Widget("复选框");
Windowform textBoxUsername = new Widget("记住用户名");
Windowform LinkLable = new Widget("忘记密码");
((Frame) frame2).Add(lableUsernaem);
((Frame) frame2).Add(textBoxText);
((Frame) frame2).Add(lablePassword);
((Frame) frame2).Add(passwordBox);
((Frame) frame2).Add(CheckBox);
((Frame) frame2).Add(textBoxUsername);
((Frame) frame2).Add(LinkLable);
lableUsernaem.print();
textBoxText.print();
lablePassword.print();
passwordBox.print();
CheckBox.print();
textBoxUsername.print();
LinkLable.print();
}
}
评论