本文共 5646 字,大约阅读时间需要 18 分钟。
public abstract class CourseComponent { public void addChild(CourseComponent component){ throw new UnsupportedOperationException("不支持添加操作"); } public void removeChild(CourseComponent component){ throw new UnsupportedOperationException("不支持删除操作"); } public String getName(CourseComponent component){ throw new UnsupportedOperationException("不支持获取名称操作"); } public double getPrice(CourseComponent component){ throw new UnsupportedOperationException("不支持获取价格操作"); } public void print(){ throw new UnsupportedOperationException("不支持打印操作"); }}
public class CoursePage extends CourseComponent{ private Listitmes = new ArrayList<>(); private String name; private Integer level; public CoursePage(String name, Integer level) { this.name = name; this.level = level; } @Override public void addChild(CourseComponent component) { itmes.add(component); } @Override public void removeChild(CourseComponent component) { itmes.remove(component); } @Override public String getName(CourseComponent component) { return this.name; } @Override public void print() { System.out.println(this.name); for(CourseComponent component1 : itmes){ if(this.level != null){ for(int i = 0; i < this.level; i ++){ System.out.print(" "); } for(int i = 0 ; i < this.level; i ++){ if(i == 0){ System.out.print("+"); } System.out.print("-"); } } component1.print(); } }}
public class Course extends CourseComponent{ private String name; private double price; public Course(String name, double price) { this.name = name; this.price = price; } @Override public String getName(CourseComponent component) { return this.name; } @Override public double getPrice(CourseComponent component) { return this.price; } @Override public void print() { System.out.println(name + ",价格: " + price + "元"); }}
public class Test { public static void main(String[] args) { System.out.println("======透明的组合模式=========="); CourseComponent javaBase = new Course("Java入门课程",6300); CourseComponent ai = new Course("人工智能",5000); CourseComponent packageCourse = new CoursePage("Java架构师课程",2); CourseComponent design = new Course("Java设计模式",1500); CourseComponent source = new Course("源码分析",3000); CourseComponent softSkill = new Course("软技能",3000); packageCourse.addChild(design); packageCourse.addChild(source); packageCourse.addChild(softSkill); CourseComponent catalog = new CoursePage("my课程目录",1); catalog.addChild(javaBase); catalog.addChild(ai); catalog.addChild(packageCourse); catalog.print(); }}
public abstract class Directory { protected String name; public Directory(String name){ this.name = name; } public abstract void show();}
public class File extends Directory{ public File(String name) { super(name); } @Override public void show() { System.out.println(this.name); }}
public class Folder extends Directory{ private Listdirs ; private Integer level; public Folder(String name,Integer level) { super(name); this.level = level; this.dirs = new ArrayList<>(); } @Override public void show() { System.out.println(this.name); for(Directory directory : dirs){ if(this.level != null){ for(int i = 0; i < this.level; i ++){ System.out.print(" "); } for(int i = 0 ; i < this.level; i ++){ if(i == 0){ System.out.print("+"); } System.out.print("-"); } } directory.show(); } } public boolean add(Directory dir){ return this.dirs.add(dir); } public boolean remove(Directory dir){ return this.dirs.remove(dir); } public Directory get(int index){ return this.dirs.get(index); } public void list(){ for(Directory dir : dirs){ System.out.println(dir.name); } }}
public class Test { public static void main(String[] args) { System.out.println("==========安全模式写法========="); File qq = new File("qq.exe"); File wx = new File("微信.exe"); Folder office = new Folder("办公软件",2); File word = new File("Word.exe"); File ppt = new File("PowerPoint.exe"); File excel = new File("Excel.exe"); office.add(word); office.add(ppt); office.add(excel); Folder root = new Folder("/opt/",1); root.add(qq); root.add(wx); root.add(office); System.out.println("=======show()========"); root.show(); System.out.println("======list()==========="); root.list(); }}
转载地址:http://skjrn.baihongyu.com/