桥接(Bridge)模式的定义如下:将抽象与实现分离,使它们可以独立变化。它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度。
桥接(Bridge)模式包含以下主要角色。
- 抽象化(Abstraction)角色:定义抽象类,并包含一个对实现化对象的引用。
- 扩展抽象化(Refined Abstraction)角色:是抽象化角色的子类,实现父类中的业务方法,并通过组合关系调用实现化角色中的业务方法。
- 实现化(Implementor)角色:定义实现化角色的接口,供扩展抽象化角色调用。
- 具体实现化(Concrete Implementor)角色:给出实现化角色接口的具体实现。
扩展抽象化调用具体实例化
继承抽象类 组合接口实现类
package lee.bridge;
public abstract class Car {
protected Colour color;
public Car(Colour color) {
this.color = color;
}
public abstract void des();
}
package lee.bridge;
public class Ford extends Car {
public Ford(Colour color) {
super(color);
}
public void des(){
System.out.println("这是一辆"+color.getName()+ this.getClass().getSimpleName());
}
}
package lee.bridge;
public class Tesla extends Car {
public Tesla(Colour color) {
super(color);
}
public void des(){
System.out.println("这是一辆"+color.getName()+ this.getClass().getSimpleName());
};
}
package lee.bridge;
public interface Colour {
String getName();
}
package lee.bridge;
public class Blue implements Colour {
private String name=" 蓝色的";
public Blue() {
}
public String getName(){
return this.name;
}
}
package lee.bridge;
public class Red implements Colour {
private String name="红色的";
public Red() {
}
public String getName(){
return this.name;
}
}
package lee.bridge;
public class CarTest {
public static void main(String[] args) {
Car ford=new Ford(new Red());
ford.des();
Car tesla=new Tesla(new Blue());
tesla.des();
}
}
外观模式:一个类组合多个类
通过为多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式。
package lee.t3;
public class System1 {
public void system1Start() {
System.out.println("system1Start被调用!");
}
}
class System2 {
public void system2Start() {
System.out.println("system2Start被调用!");
}
}
class System3 {
public void system3Start() {
System.out.println("system3Start被调用!");
}
}
package lee.t3;
public class Facade {
private System1 s1 = new System1();
private System2 s2 = new System2();
private System3 s3 = new System3();
public void method() {
s1.system1Start();
s2.system2Start();
s3.system3Start();
}
}
package lee.t3;
public class FacadeTest {
public static void main(String[] args) {
Facade f = new Facade();
f.method();
}
}
享元模式:相同对象只要保存一份,降低了系统中对象的数量
package lee.t5;
public class Test {
public static void main(String[] args) {
CarMouldFactory carMouldFactory= new CarMouldFactory();
CarMould carMould=carMouldFactory.getMould("1");
CarMould carMould2=carMouldFactory.getMould("2");
CarMould carMould3=carMouldFactory.getMould("3");
CarMould carMould4=carMouldFactory.getMould("1");
CarMould carMould5=carMouldFactory.getMould("1");
System.out.println(carMould);
System.out.println(carMould2);
System.out.println(carMould3);
System.out.println(carMould4);
System.out.println(carMould5);
carMould.makeCar(new Colour("red"));
carMould2.makeCar(new Colour("blue"));
carMould3.makeCar(new Colour("red"));
carMould4.makeCar(new Colour("blue"));
carMould5.makeCar(new Colour("red"));
}
}
package lee.t5;
import java.util.HashMap;
import java.util.Map;
public class CarMouldFactory {
private Map<String,CarMould> map = new HashMap<>();
public CarMould getMould(String key){
CarMould carMould=null;
if(map.get(key)==null){
carMould=new CarMould(key);
map.put(key,carMould);
}else{
carMould=map.get(key);
}
return carMould;
}
}
package lee.t5;
public class CarMould implements Mould {
public String key ;
public CarMould(String key) {
this.key=key;
}
//享元类依赖非享元类
public void makeCar( Colour colour){
System.out.println("用第" +key+ "种模板制造 "+colour.name+" 颜色的汽车");
}
}
package lee.t5;
public class Colour {
public Colour(String name) {
this.name = name;
}
public String name;
public String getName(){
return name;
}
}
组合模式的定义:是一种将对象组合成树状的层次结构的模式
(1) 透明方式
package lee.t6;
public class TestTree {
public static void main(String[] args) {
Component c0 = new Branch();
Component c1 = new Branch();
Component leaf1 = new Leaf("1");
Component leaf2 = new Leaf("2");
Component leaf3 = new Leaf("3");
c0.add(leaf1);
c0.add(c1);
c1.add(leaf2);
c1.add(leaf3);
c0.visit();
}
}
package lee.t6;
public interface Component {
void add(Component c);
void remove(Component c);
Component getChild(int i);
void visit();
}
package lee.t6;
import java.util.ArrayList;
public class Branch implements Component {
private ArrayList<Component> children = new ArrayList<Component>();
public void add(Component c) {
children.add(c);
}
public void remove(Component c) {
children.remove(c);
}
public Component getChild(int i) {
return children.get(i);
}
public void visit() {
for (Object obj : children) {
((Component) obj).visit();
}
}
}
package lee.t6;
public class Leaf implements Component {
private String name;
public Leaf(String name) {
this.name = name;
}
public void add(Component c) {
}
public void remove(Component c) {
}
public Component getChild(int i) {
return null;
}
public void visit() {
System.out.println(name + "Leaf" );
}
}
(2) 安全方式
package lee.t7;
public class TestTree {
public static void main(String[] args) {
Branch c0 = new Branch();
Branch c1 = new Branch();
Component leaf1 = new Leaf("1");
Component leaf2 = new Leaf("2");
Component leaf3 = new Leaf("3");
c0.add(leaf1);
c0.add(c1);
c1.add(leaf2);
c1.add(leaf3);
c0.visit();
}
}
package lee.t7;
public interface Component {
void visit();
}
package lee.t7;
import java.util.ArrayList;
public class Branch implements Component {
private ArrayList<Component> children = new ArrayList<Component>();
public void add(Component c) {
children.add(c);
}
public void remove(Component c) {
children.remove(c);
}
public Component getChild(int i) {
return children.get(i);
}
public void visit() {
for (Object obj : children) {
((Component) obj).visit();
}
}
}
package lee.t7;
public class Leaf implements Component {
private String name;
public Leaf(String name) {
this.name = name;
}
public void visit() {
System.out.println(name + "Leaf" );
}
}