事物变化存在两个因素时,将变化的两个因素进行解藕.
例如某个画图程序,图形可能有圆形,正方形等,画图的方法可能有点化线,虚线等,可以使用该模式进行设计.
java代码如下
package com.liu.pattern8;
public class BridgeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Drawing drawing = new MyDrawing();
Shape shape = new Circle(drawing);
shape.doDraw();
}
}
interface Shape{
public void doDraw();
}
interface Drawing{
public void draw();
}
class Circle implements Shape{
private Drawing drawing;
public Circle(Drawing drawing){
this.drawing = drawing;
}
public void doDraw(){
drawing.draw();
System.out.println("圆形");
}
}
class MyDrawing implements Drawing{
public void draw(){
System.out.println("用点化线描画");
}
}
c#代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace com.liu.pattern8
{
class BridgeTest
{
static void Main(string[] args)
{
Drawing drawing = new MyDrawing();
Shape shape = new Circle(drawing);
shape.DoDraw();
System.Console.ReadKey();
}
}
interface Shape{
void DoDraw();
}
interface Drawing{
void Draw();
}
class Circle : Shape{
private Drawing drawing;
public Circle(Drawing drawing){
this.drawing = drawing;
}
public void DoDraw(){
drawing.Draw();
System.Console.WriteLine("圆形");
}
}
class MyDrawing : Drawing{
public void Draw(){
System.Console.WriteLine("用点化线描画");
}
}
}