第三章作业

发布于: 2020 年 06 月 24 日
  1. 手写一个单例模式

  1. 组合模式打印表单

工程目录结构

src -

com.composite

Client.java

Component.java

LeafComponent.java

RootComponent.java

SecondaryComponent.java

各个类的源码

package com.composite;

public class Client {

public static void main(String[] args) {

// TODO Auto-generated method stub

//创建根

Component root = new RootComponent("window form(window 窗口)", "");

//创建根下的叶子和非叶子

Component picLeaf = new LeafComponent("Picture (LOGO图片)", "");

Component logButtonLeaf = new LeafComponent("Button (登录)", "");

Component signButtonLeaf = new LeafComponent("Button (注册)", "");

Component frameComponent = new SecondaryComponent("Frame(Frame1)", "");

//创建非叶子下的叶子

Component labelUserLeaf = new LeafComponent("Label (用户名)", "");

Component textBoxLeaf = new LeafComponent("TextBox (文本框)", "");

Component labelPassLeaf = new LeafComponent("Label (密码)", "");

Component passwordLeaf = new LeafComponent("PassWordBox (密码框)", "");

Component checkBoxLeaf = new LeafComponent("CheckBox (复选框)", "");

Component textBoxLeaf2 = new LeafComponent("TextBox (记住用户名)", "");

Component linklabelLeaf = new LeafComponent("linkLabel (忘记密码)", "");

frameComponent.add(labelUserLeaf);

frameComponent.add(textBoxLeaf);

frameComponent.add(labelPassLeaf);

frameComponent.add(passwordLeaf);

frameComponent.add(checkBoxLeaf);

frameComponent.add(textBoxLeaf2);

frameComponent.add(linklabelLeaf);

root.add(picLeaf);

root.add(logButtonLeaf);

root.add(signButtonLeaf);

root.add(frameComponent);

root.print();

}

}

package com.composite;

public abstract class Component {

private String name; // 名字

private String des; // 说明

protected void add(Component Component) {

//默认实现

throw new UnsupportedOperationException();

}

protected void remove(Component Component) {

//默认实现

throw new UnsupportedOperationException();

}

//构造器

public Component(String name, String des) {

super();

this.name = name;

this.des = des;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDes() {

return des;

}

public void setDes(String des) {

this.des = des;

}

//方法print, 做成抽象的, 子类都需要实现

protected abstract void print();

}

package com.composite;

public class LeafComponent extends Component {

//没有集合

public LeafComponent(String name, String des) {

super(name, des);

// TODO Auto-generated constructor stub

}

//add , remove 就不用写了,因为他是叶子节点

@Override

public String getName() {

// TODO Auto-generated method stub

return super.getName();

}

@Override

public String getDes() {

// TODO Auto-generated method stub

return super.getDes();

}

@Override

protected void print() {

// TODO Auto-generated method stub

System.out.println(getName());

}

}

package com.composite;

import java.util.ArrayList;

import java.util.List;

//RootComponent管理SecondaryComponent

public class RootComponent extends Component {

List<Component> components = new ArrayList<Component>();

// 构造器

public RootComponent(String name, String des) {

super(name, des);

// TODO Auto-generated constructor stub

}

// 重写add

@Override

protected void add(Component component) {

// TODO Auto-generated method stub

components.add(component);

}

// 重写remove

@Override

protected void remove(Component component) {

// TODO Auto-generated method stub

components.remove(component);

}

@Override

public String getName() {

// TODO Auto-generated method stub

return super.getName();

}

@Override

public String getDes() {

// TODO Auto-generated method stub

return super.getDes();

}

// print方法,就是输出University 包含的学院

@Override

protected void print() {

// TODO Auto-generated method stub

System.out.println("--------------" + getName() + "--------------");

//遍历 organizationComponents

for (Component component : components) {

component.print();

}

}

}

package com.composite;

import java.util.ArrayList;

import java.util.List;

public class SecondaryComponent extends Component {

//List 中 存放的LeafComponent

List<Component> components = new ArrayList<Component>();

// 构造器

public SecondaryComponent(String name, String des) {

super(name, des);

// TODO Auto-generated constructor stub

}

// 重写add

@Override

protected void add(Component component) {

// TODO Auto-generated method stub

components.add(component);

}

// 重写remove

@Override

protected void remove(Component component) {

// TODO Auto-generated method stub

components.remove(component);

}

@Override

public String getName() {

// TODO Auto-generated method stub

return super.getName();

}

@Override

public String getDes() {

// TODO Auto-generated method stub

return super.getDes();

}

// print方法,就是输出SecondaryComponent 包含的LeafComponent

@Override

protected void print() {

// TODO Auto-generated method stub

System.out.println("--------------" + getName() + "--------------");

//遍历components

for (Component component : components) {

component.print();

}

}

}

用户头像

武鹏

关注

还未添加个人签名 2020.04.23 加入

还未添加个人简介

评论

发布
暂无评论
第三章作业