架构师训练营第三章作业
单例模式
保证一个类仅有一个实例,并提供一个访问它的全局访问点
组合模式(Composite Pattern)将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户可以使用一致的方法操作单个对象和组合对象。
类图
Component.java
------------------------------------------------------------------------------------------------------------
public abstract class Component {
public String name ;
public Component(String name) {
this.name = name;
}
public abstract void print();
public void add(Component c)
{
throw new UnsupportedOperationException("不支持");
}
public void remove(Component c)
{
throw new UnsupportedOperationException("不支持");
}
}
------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
/**
* @author Administrator
* @version 1.0
* @created 23-6 月-2020 17:32:59
*/
public class ContainerComponent extends Component {
private ArrayList<Component> children;
private String name;
public ContainerComponent(String name){
super(name);
this.name=name ;
this.children = new ArrayList<Component>();
}
/**
*
* @param c
*/
public void add(Component c){
this.children.add(c);
}
public void print(){
System.out.print("+") ;
System.out.println(this.getClass().getName()+":"+this.name);
if(this.children != null){
System.out.println("**");
for (Component cc : children) {
cc.print() ;
}
}
}
/**
*
* @param c
*/
public void remove(Component c){
this.children.remove(c) ;
}
/**
*
* @param i
*/
public Component getChild(int i){
return this.children.get(i);
}
public ArrayList<Component> getChildren() {
return this.children;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
------------------------------------------------------------------------------------------------------------
/**
* @author Administrator
* @version 1.0
* @created 23-6 月-2020 17:32:57
*/
public class LeafComponent extends Component {
private String name;
public LeafComponent(String name){
super(name);
this.name=name ;
}
public void print(){
System.out.print(" ") ;
System.out.println(this.getClass().getName()+":"+this.name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
------------------------------------------------------------------------------------------------------------
/**
* @author Administrator
* @version 1.0
* @created 23-6 月-2020 17:33:01
*/
public class WinForm extends ContainerComponent {
public WinForm(String name) {
super(name);
// TODO Auto-generated constructor stub
}
}
Frame 略
------------------------------------------------------------------------------------------------------------
/**
* @author Administrator
* @version 1.0
* @created 23-6 月-2020 17:33:10
*/
public class Lable extends LeafComponent {
public Lable(String name) {
super(name);
// TODO Auto-generated constructor stub
}
}
Button 等 略
------------------------------------------------------------------------------------------------------------
public class ClientTest {
public static void main(String[]args){
Component root = new WinForm("windows 窗口");
Component frame = new Frame("frame1");
Component pic = new Picture("logo 图片");
Component btnLogin = new Button("登录") ;
Component btnRegist = new Button("注册") ;
Component lableUsername = new Lable("用户名") ;
Component lablePassword = new Lable("密码") ;
Component textBox = new TextBox("文本框") ;
Component pwdBox = new PasswordBox("密码框") ;
Component checkBox = new CheckBox("复选框") ;
Component textBox2 = new Lable("记住用户名密码") ;
Component linkLable = new LinkLable("忘记密码") ;
root.add(pic);
root.add(btnLogin);
root.add(btnRegist) ;
frame.add(lableUsername);
frame.add(lablePassword);
frame.add(textBox);
frame.add(textBox2);
frame.add(checkBox);
frame.add(pwdBox);
frame.add(linkLable);
root.add(frame);
root.print();
}
}
------------------------------------------------------------------------------------------------------------
评论