设计模式--桥接模式、组合模式
桥接模式
类图
源码
package bridge; public interface Vehicle { public void drive(); } Car.java package bridge; public class Car implements Vehicle { @Override public void drive() { System.out.print("小轿车"); } } Bus.java package bridge; public class Bus implements Vehicle{ @Override public void drive() { System.out.print("公共汽车"); } } Road.java package bridge; public abstract class Road { protected Vehicle vehicle; public Road(Vehicle vehicle) { this.vehicle = vehicle; } public abstract void driveOnRoad(); } ShuiRoad.java package bridge; public class ShuiRoad extends Road { public ShuiRoad(Vehicle vehicle) { super(vehicle); } @Override public void driveOnRoad() { super.vehicle.drive(); System.out.println("行驶在水泥路"); } } LiRoad.java package bridge; public class LiRoad extends Road { public LiRoad(Vehicle vehicle) { super(vehicle); } @Override public void driveOnRoad() { super.vehicle.drive(); System.out.println("行驶在沥青路"); } } Tester.java package bridge; public class Tester { public static void main(String[] args) { Road road = new ShuiRoad(new Car()); road.driveOnRoad(); Road road1 = new ShuiRoad(new Bus()); road1.driveOnRoad(); } }
测试截图
组合模式
类图
源码
package composite;
public interface IFile {
//下面两个方法,相当于类图中operation方法
void delete();
String getName();
/* 以上为公共行为,以下为文件夹才有的行为 */
//创建新文件,相当于add方法
void createNewFile(String name);
//相当于remove方法
void deleteFile(String name);
//相当于GetChild方法
IFile getIFile(int index);
}
Folder.java
package composite;
import java.util.ArrayList;
import java.util.List;
public class Folder implements IFile{
private String name;
private IFile folder;
private List<IFile> files;
public Folder(String name) {
this(name, null);
}
public Folder(String name,IFile folder) {
super();
this.name = name;
this.folder = folder;
files = new ArrayList<IFile>();
}
public String getName() {
return name;
}
//与File的删除方法不同,先删除下面的文件以及文件夹后再删除自己
public void delete() {
List<IFile> copy = new ArrayList<IFile>(files);
System.out.println("------------删除子文件-------------");
for (IFile file : copy) {
file.delete();
}
System.out.println("----------删除子文件结束-------------");
if (folder != null) {
folder.deleteFile(name);
}
System.out.println("---删除[" + name + "]---");
}
public void createNewFile(String name) {
if (name.contains(".")) {
files.add(new File(name,this));
}else {
files.add(new Folder(name,this));
}
}
public void deleteFile(String name) {
for (IFile file : files) {
if (file.getName().equals(name)) {
files.remove(file);
break;
}
}
}
public IFile getIFile(int index) {
return files.get(index);
}
}
File.java
package composite;
public class File implements IFile{
private String name;
private IFile folder;
public File(String name,IFile folder) {
super();
this.name = name;
this.folder = folder;
}
public String getName() {
return name;
}
public void delete() {
folder.deleteFile(name);
System.out.println("---删除[" + name + "]---");
}
//文件不支持创建新文件
public void createNewFile(String name) {
throw new UnsupportedOperationException();
}
//文件不支持删除文件
public void deleteFile(String name) {
throw new UnsupportedOperationException();
}
//文件不支持获取下面的文件列表
public IFile getIFile(int index) {
throw new UnsupportedOperationException();
}
}
Client.java
package composite;
public class Client {
public static void main(String[] args) {
IFile root = new Folder("我的电脑");
root.createNewFile("C盘");
root.createNewFile("D盘");
root.createNewFile("E盘");
IFile D = root.getIFile(1);
D.createNewFile("project");
D.createNewFile("电影");
IFile project = D.getIFile(0);
project.createNewFile("test1.java");
project.createNewFile("test2.java");
project.createNewFile("test3.java");
IFile movie = D.getIFile(1);
movie.createNewFile("电影1.avi");
movie.createNewFile("电影2.avi");
/* 以上为当前文件系统的情况,下面我们尝试删除文件和文件夹 */
display(null, root);
System.out.println();
project.delete();
movie.getIFile(1).delete();
System.out.println();
display(null, root);
}
//打印文件系统
public static void display(String prefix,IFile iFile){
if (prefix == null) {
prefix = "";
}
System.out.println(prefix + iFile.getName());
if(iFile instanceof Folder){
for (int i = 0; ; i++) {
try {
if (iFile.getIFile(i) != null) {
display(prefix + "--", iFile.getIFile(i));
}
} catch (Exception e) {
break;
}
}
}
}
}
测试截图