zoukankan      html  css  js  c++  java
  • 桥接模式

     

    1、画圆接口

    package Bridge;
    
    public interface DrawAPI {
    
    	public void drawCircle(int radius, int x, int y);
    
    }
    

      

    2、画圆接口实现类——绿圆

    package Bridge;
    
    public class GreenCircle implements DrawAPI{
    
    	@Override
    	public void drawCircle(int radius, int x, int y) {
    		System.out.println("Drawing Circle[ color: green, radius: "
    				+ radius +", x: " +x+", "+ y +"]");
    	}
    
    	
    }
    

     

    3、画圆接口实现类——红圆

     

    package Bridge;
    
    public class RedCircle implements DrawAPI{
    
    	@Override
    	public void drawCircle(int radius, int x, int y) {
    		System.out.println("Drawing Circle[ color: red, radius: "
    				+ radius +", x: " +x+", "+ y +"]");
    	}
    
    }
    

      

    4、形状父类

     

    package Bridge;
    
    public abstract class Shape {
    
    	public abstract void draw();
    	
    }
    

      

    5、形状子类

     

    package Bridge;
    
    public class Circle extends Shape{
    
    	protected DrawAPI drawAPI;
    	private int x, y, radius;
    	
    	public Circle(int x, int y, int radius, DrawAPI drawAPI) {
    
    		this.drawAPI = drawAPI;
    		this.x = x;
    		this.y = y;
    		this.radius = radius;
    		
    	}
    	
    	public void draw(){
    		drawAPI.drawCircle(radius, x, y);
    	}
    
    }
    

      在Circle类中引用DrawAPI 的对象,即可使用DrawAPI 的实现类:

      GreenCircleRedCircle ,完成桥接

     

    6、测试类

    package Bridge;
    
    public class BridgePatternDemo {
    
    	public static void main(String[] args) {
    		
    		Shape redCircle = new Circle(100,100,10,new RedCircle());
    		
    		Shape greenCircle = new Circle(100,100,10,new GreenCircle());
    		
    		redCircle.draw();	//调用了RedCircle的draw()
    		greenCircle.draw();	//调用了GreenCircle的draw()
    	}
    }
    

      

     

     测试结果:

    Drawing Circle[ color: red, radius: 10, x: 100, 100]
    Drawing Circle[ color: green, radius: 10, x: 100, 100]

     

     ————————————————————————————————————————————————

    修改上面第四个和第五个Shape类和Circle类,效果相同:

     

    4、形状父类

    package Bridge;
    
    public abstract class Shape {
    
        protected DrawAPI drawAPI;
        
        protected Shape(DrawAPI drawAPI){
            this.drawAPI = drawAPI;
        }
        
        public abstract void draw();
        
    }

      

      在Shape类中引用DrawAPI 的对象,即可使用DrawAPI 的实现类:

      GreenCircle RedCircle ,完成桥接

     

     

    5、形状子类

    package Bridge;
    
    public class Circle extends Shape{
    
        private int x, y, radius;
        
        public Circle(int x, int y, int radius, DrawAPI drawAPI) {
            super(drawAPI);
            this.x = x;
            this.y = y;
            this.radius = radius;
            
        }
        
        public void draw(){
            drawAPI.drawCircle(radius, x, y);
        }
    
    }

     

  • 相关阅读:
    如何设置范围,使透视数据源记录可以自适应地改变
    Atitit..文件上传组件选择and最佳实践的总结(2)----HTTP
    AIDL(1)
    最好的年龄减肥
    2012在数据库技术会议上的讲话PPT打包
    左右 Java 于 finally 深度分析语句块
    R0-R37它是Arm 寄存器,那是,CPU内部。和GPIO注册所有外设。换句话说,要是arm的cpu,它包含了其他芯片公司将有R0-R37,和GPIO寄存器只有一个特定的芯片。
    使用方便 正则表达式grep,sed,awk(一)
    经验36--C#无名(大事,物...)
    IOS 图片压缩
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/7641775.html
Copyright © 2011-2022 走看看