如有错误请及时指正
桥接模式适用于管理一个类包含的不同维度的属性,一个长方形包含长和宽两个属性,他分别是可以变化的,我会这么设计这个类
public class Rectangle {
private int length;
private int width;
}
如果现在要设计一个和图形相关的结构,有长方形,圆形,不需要表示具体长和宽或半径
public class Shape{
private int type;//1表示长方形,2表示圆形
}
按上面的结构,我只用一个属性就可以表示图形的类型,现在我要给图形加入一个颜色属性,有红色,黄色,蓝色,我还是省事用123表示
public class Shape{
private int type;
private int color;//1表示红色,2表示黄色,3表示蓝色
}
这时我需求变了,我要做的图形有些是RGB色彩模式的,有些是CMYK色彩模式的,RGB用三个属性表示颜色,CMYK用四个属性表示颜色
此时颜色这个属性不是一维的了,它还有类型之分
public class Shape{
private int type;
private Color color;
public Shape(Color color){
this.color =color;
}
}
public class Color{
private int type;//1表示RGB模式,2表示CMYK模式
private int R;//RGB色彩的R值
private int G;
private int B;
private int C;
private int M;
private int Y;
private int K;
}
这样在new Shape()的时候创建一个Color对象设置好颜色放进参数就可以了,但是后期加其他色彩模式怎么办?给Color继续加字段?
我本着需求扩展但尽量不改动原有类的基础上,尽力把现有类设计得足够扩展。
所以我把Color这个类改成抽象的,新建RGB类和CMYK类来分别定义自己表示色彩的属性,以后拓展其他色彩模式也更方便,接口就是定义实现类的相同部分的,所以我可以给接口添加几个方法由实现类自己实现,比如告诉方法调用者自己的色彩模式
public class Shape{
private int type;
private Color color;
public Shape(Color color){
this.color =color;
}
}
public interface Color{
int desc();
}
public class RGB implements Color{
private int R;
private int G;
private int B;
public int desc(){
System.out.println("我是RGB色彩模式");
return 1;
}
}
public class CMYK implements Color{
private int C;
private int M;
private int Y;
private int K;
public int desc(){
System.out.println("我是CMYK色彩模式");
return 2;
}
}
这时候Shape和Color就是一个桥接啦,这时我需求又变了,我不只是要用Shape类的type属性表示图形是长方形还是圆形了,如果是
长方形我需要表示长方形的长,宽,如果是圆形我需要表示半径,然后它们还要有各自的方法计算面积和周长
没问题,这时候type属性在Shape类中它的维度上升了,在Shape中表示越来越麻烦,我需要再次桥接。
我这次把Shape上升一层进行抽象,把Shape改为抽象类,添加获取面积和周长的抽象方法(其实抽象方法和接口差不多),那为什么用
抽象类呢?如果我要在具体图形类中设置颜色,就要分别写颜色属性和设置颜色的方法,抽象就是为了提取共性,接口不能带有实际方法和
未初始化的属性,那就用抽象类,以后扩展别的图形也不用再写,直接继承了
public abstract class Shape{
private Color color;
public Shape (Color color){
this.color = color;
}
public abstract void getArea();//获取面积
public abstract void getPerimeter();//获取周长
}
public class Rectangle extends Shape {
private int length;//长
private int width;//宽
public Rectangle(Color color) {
super(color);
}
@Override
public int getArea() {
return length * width;
}
@Override
public int getPerimeter() {
return ( length + width ) * 2;
}
}
public class Circle extends Shape {
private int radius;//半径
public Circle(Color color) {
super(color);
}
@Override
public int getArea() {
return new Double(Math.PI * (Math.pow(radius,2))).intValue();
}
@Override
public int getPerimeter() {
return new Double(2 * Math.PI * radius).intValue();
}
}
public interface Color{
int desc();
}
public class RGB implements Color{
private int R;
private int G;
private int B;
public int desc(){
System.out.println("我是RGB色彩模式");
return 1;
}
}
public class CMYK implements Color{
private int C;
private int M;
private int Y;
private int K;
public int desc(){
System.out.println("我是CMYK色彩模式");
return 2;
}
}
这样就搞完了,色彩模式扩展,图形扩展都支持了,写个main方法看看
public static void main(String[] args) {
RGB rgb =new RGB();
rgb.setR(1);
rgb.setG(1);
rgb.setB(1);
rgb.desc();
Rectangle rectangle =new Rectangle(rgb);
rectangle.setLength(10);
rectangle.setWidth(5);
int area = rectangle.getArea();
int perimeter = rectangle.getPerimeter();
System.out.println("图形面积:" + area +",图形周长:" + perimeter);
}
输出结果
我是RGB色彩模式
图形面积:50,图形周长:30
网友评论