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

     

  • 相关阅读:
    解耦和耦合
    python os.remove()方法
    python中split()、os.path.split()函数用法
    P7116-[NOIP2020]微信步数【数学】
    2021牛客OI赛前集训营-方格计数【计数,dp】
    2021牛客OI赛前集训营-树数树【树上启发式合并,堆】
    Ybtoj-排列计数【矩阵乘法,分块幂】
    P7888-「MCOI-06」Distinct Subsequences【dp】
    号爸十一集训 Logs
    数据结构 专项题解
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/7641775.html
Copyright © 2011-2022 走看看