画类图
以文件夹浏览为例;
具体代码实现
//AbstractFile.java
package org.example.design008;
public abstract class AbstractFile {
public abstract void add(AbstractFile element);
public abstract void remove(AbstractFile element);
public abstract void display();
}
//TextFile.java
package org.example.design008;
public class TextFile extends AbstractFile{
public TextFile(String filename) {
this.filename=new String();
this.filename = filename;
}
private String filename;
@Override
public void add(AbstractFile element) {
System.out.println("add textFile");
}
@Override
public void remove(AbstractFile element) {
System.out.println("remove textFile");
}
@Override
public void display() {
System.out.print(filename);
System.out.println(" TextFile");
}
}
//ImageFile.java
package org.example.design008;
import javax.xml.transform.Source;
public class ImageFile extends AbstractFile{
public ImageFile(String filename) {
this.filename=new String();
this.filename = filename;
}
private String filename;
@Override
public void add(AbstractFile element) {
System.out.println("add imageFile");
}
@Override
public void remove(AbstractFile element) {
System.out.println("remove imageFile");
}
@Override
public void display() {
System.out.print(filename);
System.out.println(" ImageFile");
}
}
//VideoFile.java
package org.example.design008;
public class VideoFile extends AbstractFile{
public VideoFile(String filename) {
this.filename=new String();
this.filename = filename;
}
private String filename;
@Override
public void add(AbstractFile element) {
System.out.println("add videoFile");
}
@Override
public void remove(AbstractFile element) {
System.out.println("remove videoFile");
}
@Override
public void display() {
System.out.print(filename);
System.out.println(" VideoFile");
}
}
//Folder.java
package org.example.design008;
import java.util.ArrayList;
public class Folder extends AbstractFile{
public Folder(String filename) {
this.filename=new String();
this.filename = filename;
}
private String filename;
private ArrayList<AbstractFile> list=new ArrayList<>();
@Override
public void add(AbstractFile element) {
list.add(element);
System.out.println("add folder");
}
@Override
public void remove(AbstractFile element) {
list.remove(element);
System.out.println("remove folder");
}
@Override
public void display() {
System.out.println(filename);
for(Object object:list){
((AbstractFile)object).display();
}
System.out.println("====Folder Display====");
}
}
//Client.java
package org.example.design008;
public class Client {
public static void main(String[] args) {
AbstractFile obj1,obj2,obj3,obj4;
Folder f1,f2,f3;
obj1=new ImageFile("a.jpg");
obj2=new TextFile("b.txt");
f1=new Folder("Image_Text");
f1.add(obj1);
f1.add(obj2);
obj3=new VideoFile("c.mp4");
f2=new Folder("Image_Video");
f2.add(obj1);
f2.add(obj3);
obj4=new ImageFile("d.jpg");
f3=new Folder("Image_Text_Video");
f3.add(f1);
f3.add(f2);
f3.add(obj4);
f3.display();
}
}
标签:Java,组合,--,AbstractFile,System,filename,add,public,out
From: https://www.cnblogs.com/liuzijin/p/17767124.html