架构师训练营第三周作业

发布于: 2020 年 06 月 24 日

1.手写案例模式

(2)组合模式应用

package composite;

public class Component extends Printer{

private String element;

public Component(String element) {

this.element = element;

}

@Override

public void print() {

System.out.println(element);

}

}

package composite;

public class Constants {

public static final String WINDOWSFORM = "WinForm";

public static final String PICTURE = "Picture";

public static final String LOGIN = "Button";

public static final String REGISTER = "Button";

public static final String FRAME = "FRAME";

public static final String USERNAME = "Lable";

public static final String TEXTBOX = "TextBox";

public static final String PWD = "Lable";

public static final String PWDBOX = "PasswordBox";

public static final String CHECKBOX = "CheckBox";

public static final String REMEMBER = "TextBox";

public static final String LINK = "LinkLable";

}

package composite;

import java.util.ArrayList;

import java.util.List;

public class FormPrinter extends Printer{

public FormPrinter(List<Printer> framePrinters) {

framePrinters.forEach(this::add);

}

public FormPrinter(String ... components) {

for(String component:components){

this.add(new Component(component));

}

}

}

package composite;

import java.util.ArrayList;

import java.util.List;

public class FramePrinter extends Printer{

public FramePrinter(String... components) {

for(String component:components){

this.add(new Component(component));

}

}

@Override

protected void printThisBefore() {

System.out.println();

}

public FramePrinter(List<Component> components) {

components.forEach(this::add);

}

}

package composite;

import java.util.List;

public class OperSysPrinter extends Printer{

public OperSysPrinter(String... components) {

for(String component:components){

this.add(new Component(component));

}

}

@Override

protected void printThisBefore() {

System.out.println();

}

public OperSysPrinter(List<Printer> components) {

components.forEach(this::add);

}

}

package composite;

import java.util.ArrayList;

import java.util.List;

public abstract class Printer {

private List<Printer> elements = new ArrayList<Printer>();

public void add(Printer printer) {

elements.add(printer);

}

protected void printThisBefore() {

}

public void print() {

elements.forEach(Printer::print);

}

}

package composite;

import java.util.ArrayList;

import java.util.List;

public class WinConp {

public void print(){

//form子节点

FormPrinter subForm = new FormPrinter(Constants.PICTURE,Constants.LOGIN,Constants.REGISTER);

FramePrinter fram = new FramePrinter(Constants.FRAME,Constants.USERNAME,Constants.TEXTBOX,

Constants.PWD,Constants.PWDBOX,Constants.CHECKBOX,Constants.REMEMBER,Constants.LINK);

//组装frame层

List<Printer> list = new ArrayList<Printer>();

list.add(subForm);

list.add(fram);

FormPrinter formPrinter = new FormPrinter(list);

//组装window form层

List<Printer> windowsList = new ArrayList<Printer>();

OperSysPrinter subOperSys = new OperSysPrinter(Constants.WINDOWSFORM);

windowsList.add(subOperSys);

windowsList.add(formPrinter);

OperSysPrinter operSysPrinter = new OperSysPrinter(windowsList);

operSysPrinter.print();

}

}

package composite;

public class App {

public static void main(String[] args) {

WinConp winConp = new WinConp();

winConp.print();

}

}

用户头像

air

关注

还未添加个人签名 2018.12.04 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第三周作业