• 桥接模式


     

    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);
        }
    
    }

     

  • 相关阅读:
    分析ASP.NET读取XML文件4种方法
    WordPress 主题教程 #4a:Header 模板
    WordPress 主题教程 #4b:Header 模板 2
    倍受好评的美国主机JustHost使用全攻略教程
    单链表的创建、插入、删除、倒置操作
    WordPress 主题教程:从零开始制作 WordPress 主题
    google adsense 设置建议
    PHP:10个不常见却非常有用的PHP函数
    WordPress 主题教程 #2:模板文件和模板
    必须掌握的八个cmd命令行
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/7641775.html
Copyright © 2020-2023  润新知