写点什么

设计模式作业

用户头像
qihuajun
关注
发布于: 2020 年 06 月 20 日

手写单例模式





组合模式示例



代码主要包含三部分:

第一部分包含接口定义了基本的概念

第二部分包含两个与接口对应的基类,以及它们的具体子类

第三部分是主程序实现构建窗口并打印



public interface IComponent {
public void print();
}
public interface IContainer extends IComponent {
public void addComponent(IComponent component);
}
public class Component implements IComponent{
protected String name;
public Component(String name){
this.name = name;
}
public void print() {
System.out.println(this.name);
}
}
import java.util.ArrayList;
public class Container extends Component implements IContainer {
private ArrayList<IComponent> components = new ArrayList<IComponent>();
public Container(String name) {
super(name);
}
public void addComponent(IComponent component) {
components.add(component);
}
@Override
public void print() {
super.print();
for (IComponent component : components) {
component.print();
}
}
}
public class PasswordBox extends Component {
public PasswordBox(String name) {
super(name);
}
}
public class Picture extends Component {
public Picture(String name) {
super(name);
}
}
public class TextBox extends Component {
public TextBox(String name) {
super(name);
}
}
public class WinForm extends Container {
public WinForm(String name) {
super(name);
}
}
public class Button extends Component {
public Button(String name) {
super(name);
}
}
public class CheckBox extends Component {
public CheckBox(String name) {
super(name);
}
}
public class Frame extends Container {
public Frame(String name) {
super(name);
}
}
public class Lable extends Component {
public Lable(String name) {
super(name);
}
}
public class LinkLable extends Component {
public LinkLable(String name) {
super(name);
}
}
public class Login {
public static void main(String[] args) {
WinForm winForm = new WinForm("Window窗口");
winForm.addComponent(new Picture("LOGO图片"));
Frame frame = new Frame("FRAME1");
frame.addComponent(new Lable("用户名"));
frame.addComponent(new TextBox("文本框"));
frame.addComponent(new Lable("密码"));
frame.addComponent(new PasswordBox("密码框"));
frame.addComponent(new CheckBox("复选框"));
frame.addComponent(new TextBox("记住用户名"));
frame.addComponent(new LinkLable("忘记密码"));
winForm.addComponent(frame);
winForm.addComponent(new Button("登录"));
winForm.addComponent(new Button("注册"));
winForm.print();
}
}



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

qihuajun

关注

还未添加个人签名 2009.05.15 加入

还未添加个人简介

评论

发布
暂无评论
设计模式作业