写点什么

组合模式程序案例

用户头像
天天向上
关注
发布于: 2020 年 10 月 04 日

1、定义组件统一接口

组件的核心是定义输出逻辑

public interface Component {
/**
* 所有组件都需要组件打印的接口
*/
void print();
}

2、定义一个组件基类

基类定义了组件的基本元素及输出逻辑

/**
* 定义组件的基本元素及输出流程
*/
public abstract class BaseComponent implements Component {
private String name;
public BaseComponent(String name) {
this.name = name;
}
@Override
public void print() {
StringBuilder s = new StringBuilder();
s.append("print ").append(getType()).append("(").append(name).append(")");
System.out.println(s);
}
public abstract String getType();
}



3、基本组件

3.1、Picture组件

public class Picture extends BaseComponent {
public Picture(String name) {
super(name);
}
@Override
public String getType() {
return "Picture";
}
}



3.2、Button组件

public class Button extends BaseComponent {
public Button(String name) {
super(name);
}
@Override
public String getType() {
return "Button";
}
}

3.3、Label组件

public class Label extends BaseComponent {
public Label(String name) {
super(name);
}
@Override
public String getType() {
return "Label";
}
}



3.4、TextBox组件

public class TextBox extends BaseComponent {
public TextBox(String name) {
super(name);
}
@Override
public String getType() {
return "TextBox";
}
}



3.5、PasswordBox组件

public class PasswordBox extends BaseComponent {
public PasswordBox(String name) {
super(name);
}
@Override
public String getType() {
return "PasswordBox";
}
}



3.6、CheckBox组件

public class CheckBox extends BaseComponent {
public CheckBox(String name) {
super(name);
}
@Override
public String getType() {
return "CheckBox";
}
}



3.7、LinkLabel组件

public class LinkLabel extends BaseComponent{
public LinkLabel(String name) {
super(name);
}
@Override
public String getType() {
return "LinkLabel";
}
}



4、容器组件

容器组件除了具备组件的基本元素之外,还包含子组件,子组件通过组合的方式实现。

4.1、容器组件基类

定义子组件的输出方法

public abstract class Container extends BaseComponent {
/**
* 容器中的子组件
*/
private List<Component> subComponents;
public Container(String name, List<Component> subComponents) {
super(name);
this.subComponents = subComponents;
}
@Override
public void print() {
super.print();
if (subComponents == null || subComponents.isEmpty()) {
return;
}
for (int i = 0; i < subComponents.size(); i++) {
subComponents.get(i).print();
}
}
}

4.2、WinForm组件

import java.util.List;
public class WinForm extends Container {
public WinForm(String name, List<Component> subComponents) {
super(name, subComponents);
}
@Override
public String getType() {
return "WinForm";
}
}

4.3、Frame组件

import java.util.List;
public class Frame extends Container {
public Frame(String name, List<Component> subComponents) {
super(name, subComponents);
}
@Override
public String getType() {
return "Frame";
}
}

5、登录窗口主类

import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Component> frameSubComponents = new ArrayList();
frameSubComponents.add(new Label("用户名"));
frameSubComponents.add(new TextBox("文本框"));
frameSubComponents.add(new Label("密码框"));
frameSubComponents.add(new PasswordBox("密码"));
frameSubComponents.add(new CheckBox("复选框"));
frameSubComponents.add(new TextBox("记住用户名"));
frameSubComponents.add(new LinkLabel("忘记密码"));
List<Component> windowSubComponents = new ArrayList();
windowSubComponents.add(new Picture("LOGO图片"));
windowSubComponents.add(new Button("登录"));
windowSubComponents.add(new Button("注册"));
windowSubComponents.add(new Frame("FRAME1", frameSubComponents));
Component window = new WinForm("WINDOW窗口", windowSubComponents);
window.print();
}
}



用户头像

天天向上

关注

还未添加个人签名 2018.09.20 加入

还未添加个人简介

评论

发布
暂无评论
组合模式程序案例