bridge桥模式
将抽象和行为划分开来,各自独立但能动态结合
抽象的接口
/**
* 咖啡抽象类
*
* @time 下午09:14:27
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public abstract class Coffee {
//
CoffeeImp coffeeImp;
//有木有点DI的感觉,有木有
public void setCoffeeImp() {
this.coffeeImp = CoffeeImpSingleton.getTheCoffeeImp();
}
public CoffeeImp getCoffeeImp() {
return coffeeImp;
}
/**
* 冲咖啡
*/
public abstract void pourCoffee();
}
/**
* 中杯
*
* @time 下午09:29:11
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MediumCoffee extends Coffee {
/**
* 构造器
*/
public MediumCoffee() {
// 是否添加咖啡
setCoffeeImp();
}
/**
* 冲咖啡
*/
@Override
public void pourCoffee() {
CoffeeImp coffeeImp = this.getCoffeeImp();
for (int i = 0; i < 2; i++) {
coffeeImp.pourCoffeeImp();
}
}
}
/**
* 大杯咖啡
*
* @time 下午09:37:03
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class SuperSizeCoffee extends Coffee {
/**
* 构造器
*/
public SuperSizeCoffee() {
// 是否加奶
setCoffeeImp();
}
/**
* 冲咖啡
*/
@Override
public void pourCoffee() {
CoffeeImp coffeeImp = this.getCoffeeImp();
for (int i = 0; i < 5; i++) {
coffeeImp.pourCoffeeImp();
}
}
}
=========================================================================
行为接口:
/**
* coffee是否加奶
*
* @time 下午09:18:41
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public abstract class CoffeeImp {
public abstract void pourCoffeeImp();
}
/**
* 不加奶
*
* @time 下午09:45:20
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class FragrantCoffeeImp extends CoffeeImp {
/**
* 构造器
*/
public FragrantCoffeeImp() {
}
/**
* 不加奶冲咖啡
*/
@Override
public void pourCoffeeImp() {
System.out.println("啥也木加怀!详香连!");
}
}
/**
* 加奶
*
* @time 下午09:41:56
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MilkCoffeeImp extends CoffeeImp {
/**
* 构造器
*/
public MilkCoffeeImp() {
}
/**
* 加奶冲
*/
@Override
public void pourCoffeeImp() {
System.out.println("加新鲜人初乳!");
}
}
/**
* 控制是否加奶
*
* @time 下午09:23:06
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
@SuppressWarnings("static-access")
public class CoffeeImpSingleton {
private static CoffeeImp coffeeImp;
public CoffeeImpSingleton(CoffeeImp coffeeImp) {
this.coffeeImp = coffeeImp;
}
public static CoffeeImp getTheCoffeeImp() {
return coffeeImp;
}
}
===========================================================================
/**
* 测试冲咖啡
*
* @time 下午09:49:05
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class TestPourCoffee {
/**
* @param args
*/
@SuppressWarnings("unused")
public static void main(String[] args) {
// 加奶
// CoffeeImpSingleton singleton = new CoffeeImpSingleton(new
// MilkCoffeeImp());
// 不加奶
CoffeeImpSingleton coffeeImpSingleton = new CoffeeImpSingleton
(new FragrantCoffeeImp());
// 中杯加奶
// MediumCoffee mediumCoffee = new MediumCoffee();
// mediumCoffee.pourCoffee();
// 大杯加奶
SuperSizeCoffee superSizeCoffee = new SuperSizeCoffee();
superSizeCoffee.pourCoffee();
}
}
将抽象和行为划分开来,各自独立但能动态结合
抽象的接口
/**
* 咖啡抽象类
*
* @time 下午09:14:27
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public abstract class Coffee {
//
CoffeeImp coffeeImp;
//有木有点DI的感觉,有木有
public void setCoffeeImp() {
this.coffeeImp = CoffeeImpSingleton.getTheCoffeeImp();
}
public CoffeeImp getCoffeeImp() {
return coffeeImp;
}
/**
* 冲咖啡
*/
public abstract void pourCoffee();
}
/**
* 中杯
*
* @time 下午09:29:11
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MediumCoffee extends Coffee {
/**
* 构造器
*/
public MediumCoffee() {
// 是否添加咖啡
setCoffeeImp();
}
/**
* 冲咖啡
*/
@Override
public void pourCoffee() {
CoffeeImp coffeeImp = this.getCoffeeImp();
for (int i = 0; i < 2; i++) {
coffeeImp.pourCoffeeImp();
}
}
}
/**
* 大杯咖啡
*
* @time 下午09:37:03
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class SuperSizeCoffee extends Coffee {
/**
* 构造器
*/
public SuperSizeCoffee() {
// 是否加奶
setCoffeeImp();
}
/**
* 冲咖啡
*/
@Override
public void pourCoffee() {
CoffeeImp coffeeImp = this.getCoffeeImp();
for (int i = 0; i < 5; i++) {
coffeeImp.pourCoffeeImp();
}
}
}
=========================================================================
行为接口:
/**
* coffee是否加奶
*
* @time 下午09:18:41
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public abstract class CoffeeImp {
public abstract void pourCoffeeImp();
}
/**
* 不加奶
*
* @time 下午09:45:20
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class FragrantCoffeeImp extends CoffeeImp {
/**
* 构造器
*/
public FragrantCoffeeImp() {
}
/**
* 不加奶冲咖啡
*/
@Override
public void pourCoffeeImp() {
System.out.println("啥也木加怀!详香连!");
}
}
/**
* 加奶
*
* @time 下午09:41:56
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MilkCoffeeImp extends CoffeeImp {
/**
* 构造器
*/
public MilkCoffeeImp() {
}
/**
* 加奶冲
*/
@Override
public void pourCoffeeImp() {
System.out.println("加新鲜人初乳!");
}
}
/**
* 控制是否加奶
*
* @time 下午09:23:06
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
@SuppressWarnings("static-access")
public class CoffeeImpSingleton {
private static CoffeeImp coffeeImp;
public CoffeeImpSingleton(CoffeeImp coffeeImp) {
this.coffeeImp = coffeeImp;
}
public static CoffeeImp getTheCoffeeImp() {
return coffeeImp;
}
}
===========================================================================
/**
* 测试冲咖啡
*
* @time 下午09:49:05
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class TestPourCoffee {
/**
* @param args
*/
@SuppressWarnings("unused")
public static void main(String[] args) {
// 加奶
// CoffeeImpSingleton singleton = new CoffeeImpSingleton(new
// MilkCoffeeImp());
// 不加奶
CoffeeImpSingleton coffeeImpSingleton = new CoffeeImpSingleton
(new FragrantCoffeeImp());
// 中杯加奶
// MediumCoffee mediumCoffee = new MediumCoffee();
// mediumCoffee.pourCoffee();
// 大杯加奶
SuperSizeCoffee superSizeCoffee = new SuperSizeCoffee();
superSizeCoffee.pourCoffee();
}
}