适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式,它们是关注类和对象的组合为目的的设计模式,它们也被称为结构型构建模式。
其中代理模式之前已经介绍过,可以查看往期内容:
适配器模式 Adapter Pattern
Adapter Pattern 是 Java 中常见的设计模式,它提供了一种将类、对象或接口转换成所需要的类、对象或接口的能力。
一个简单演示
演示代码
public class WeekendAdaptee {
public void enjoy() {
System.out.println("享受周末,沙发、电视剧、玩游戏。");
}
}
public interface WorkTarget {
void working();
}
public class WeekendAdapter extends WeekendAdaptee implements WorkTarget {
@Override
public void working() {
System.out.println("工作任务紧急,取消周末认真工作。");
}
}
// 测试类
public class WeekendAdapterTest {
@org.junit.Test
public void working() {
WeekendAdapter weekendAdapter = new WeekendAdapter();
weekendAdapter.enjoy();
weekendAdapter.working();
}
}
运行结果
装饰器模式 Decorator Pattern
Decorator Pattern 是 Java 中常见的设计模式,它提供了一种不改变原有类结构的情况下,为原有类增加新功能的能力。
一个简单演示
演示代码
public interface Weekend {
void enjoy();
}
public class Cartoon implements Weekend {
@Override
public void enjoy() {
System.out.println("全职猎人追剧中。");
}
}
public class Game implements Weekend {
@Override
public void enjoy() {
System.out.println("三个房子三周目中。");
}
}
public class WeekendDecorator implements Weekend {
private Weekend weekend;
public WeekendDecorator(Weekend weekend) {
this.weekend = weekend;
}
@Override
public void enjoy() {
weekend.enjoy();
System.out.println("晚上学习3小时。");
}
}
// 测试类
public class WeekendDecoratorTest {
@org.junit.Test
public void weekend() {
Weekend cartoon = new Cartoon();
cartoon.enjoy();
Weekend game = new Game();
game.enjoy();
System.out.println("----- 使用适配器 -----");
WeekendDecorator cartoonDecorator = new WeekendDecorator(cartoon);
cartoonDecorator.enjoy();
System.out.println("----- 使用适配器 -----");
WeekendDecorator gameDecorator = new WeekendDecorator(game);
gameDecorator.enjoy();
}
}
运行结果
外观模式 Facade Pattern
Facade Pattern 是 Java 中常见的设计模式,它提供了一种将复杂接口定义成简单高层接口的能力。
一个简单演示
演示代码
public class WeekendFacade {
private Weekend cartoonWeekend;
private Weekend gameWeekend;
public WeekendFacade() {
this.cartoonWeekend = new Cartoon();
this.gameWeekend = new Game();
}
public void cartoonEnjoy() {
cartoonWeekend.enjoy();
}
public void gameEnjoy() {
gameWeekend.enjoy();
}
}
// 测试类
public class WeekendFacadeTest {
@org.junit.Test
public void facade() {
WeekendFacade facade = new WeekendFacade();
facade.cartoonEnjoy();
facade.gameEnjoy();
}
}
运行结果
桥接模式 Bridge Pattern
Bridge Pattern 是 Java 中常见的设计模式,它提供了一种将抽象与实现两者解耦的能力。
一个简单演示
演示代码
public abstract class WeekendBridge {
protected Weekend weekend;
protected WeekendBridge(Weekend weekend) {
this.weekend = weekend;
}
public abstract void enjoyBridge();
}
public class WeekendSchedule extends WeekendBridge {
private String time;
public WeekendSchedule(String time, Weekend weekend) {
super(weekend);
this.time = time;
}
@Override
public void enjoyBridge() {
System.out.print(time);
weekend.enjoy();
}
}
// 测试类
public class WeekendScheduleTest {
@org.junit.Test
public void schedule() {
WeekendBridge am = new WeekendSchedule("上午,", new Cartoon());
WeekendBridge pm = new WeekendSchedule("下午,", new Game());
am.enjoyBridge();
pm.enjoyBridge();
}
}
运行结果
组合模式 Composite Pattern
Composite Pattern 是 Java 中常见的设计模式,它提供了一种包含本身的对象组的能力。
一个简单演示
演示代码
public class Ghost {
private String name;
private List<Ghost> ghosts;
public Ghost(String name) {
this.name = name;
ghosts = new ArrayList<>();
}
public void add(Ghost ghost) {
this.ghosts.add(ghost);
}
public void remove(Ghost ghost) {
this.ghosts.remove(ghost);
}
public String getName() {
return name;
}
public List<Ghost> getGhosts() {
return ghosts;
}
@Override
public String toString() {
return "Ghost{" +
"name='" + name + '\'' +
'}';
}
}
// 测试类
public class GhostTest {
@org.junit.Test
public void composite() {
Ghost g0 = new Ghost("库洛洛·鲁西鲁");
g0.add(new Ghost("信长·哈查马"));
g0.add(new Ghost("飞坦"));
g0.add(new Ghost("玛奇"));
g0.add(new Ghost("柯特·揍敌客"));
g0.add(new Ghost("芬克斯"));
g0.add(new Ghost("侠客"));
g0.add(new Ghost("富兰克林"));
g0.add(new Ghost("小滴"));
g0.add(new Ghost("派克诺妲"));
g0.add(new Ghost("剥落列夫"));
g0.add(new Ghost("伊路米·揍敌客"));
g0.add(new Ghost("库哔"));
System.out.println(g0);
for (Ghost g : g0.getGhosts()) {
System.out.println(g);
}
}
}
运行结果
享元模式 Flyweight Pattern
Flyweight Pattern 是 Java 中常见的设计模式,它提供了一种减少创建对象数量的能力。
一个简单演示
演示代码
public interface Play {
void play();
}
public class Card implements Play {
private String name;
public Card(String name) {
this.name = name;
}
@Override
public void play() {
System.out.println("play " + name);
}
}
public class CardFactory {
private static HashMap<String, Card> cards = new HashMap<>();
public static Card getCard(String name) {
Card card = cards.get(name);
if (card == null) {
card = new Card(name);
cards.put(name, card);
System.out.println("创建对象,name:" + name);
}
return card;
}
}
// 测试类
public class CardFactoryTest {
@org.junit.Test
public void card() {
for (int i=0; i<10; i++) {
Card card = CardFactory.getCard("三个房子");
card.play();
}
}
}
运行结果
总结
以上,通过一些简单的演示案例,展示了结构型构建模式的使用方法。