男生射精是什么感觉| 三候是什么意思| 冰箱不制冷是什么问题| 接档是什么意思| 仁爱是什么意思| 炖鸽子汤放什么调料| 10月25日什么星座| 时来运转是什么意思| 文理分科什么时候开始| 燕窝什么人不适合吃| 一本万利是什么意思| 补办结婚证需要什么手续| 糖耐筛查主要检查什么| 怀孕1个月有什么症状| 金丝玉是什么玉| 云吞面是什么面| 邵字五行属什么| 小儿割包皮挂什么科| 一件代发是什么意思| 忆苦思甜下一句是什么| 朗格手表什么档次| 五月26日是什么星座| 平均血小板体积偏高是什么原因| 熬夜吃什么补回来| 蹀愫女鞋什么档次| 熊猫为什么叫熊猫| 健康证都检查什么项目| 跑步什么时候跑最好| 公主和郡主有什么区别| 大云是什么| 治疗早泄吃什么药| 一什么柳树| 饿了手抖是什么原因| 吃三七粉有什么效果| 梦见在水里游泳是什么意思| 山竹不能和什么一起吃| 口蜜什么剑| 44岁月经量少是什么原因| 消炎药吃多了有什么副作用| bu是什么颜色| 大祭司是什么意思| 猎德有什么好玩的| 无聊可以干什么| 脾肾阳虚是什么意思| 龙涎是什么| 高考450分能上什么学校| 孙子兵法到底说什么| 陌上花开可缓缓归矣什么意思| 为什么没人穿卡帕| 肾气亏虚吃什么中成药| 出现的反义词是什么| 老人流口水是什么原因引起的| 为什么小便会带血| 中国第一大姓是什么| hm是什么牌子的衣服| 女人适合喝什么茶最好| 怀孕什么时候建档| 耳蜗是什么东西| 四书五经是什么| 清明节一般开什么生肖| 大姨妈可以吃什么水果| 稽是什么意思| rap是什么意思| 食管反流吃什么药| ct胸部平扫检查出什么| 办护照照片有什么要求| 生理期为什么会肚子疼| 心口疼是什么原因女性| 什么叫做绝对值| 血沉高是什么病| 总是感觉口渴是什么原因| 龙虾的血是什么颜色的| 一个车一个罔是什么字| 猫的尾巴有什么用处| 什么水果含叶酸最多| 胆结石有什么症状有哪些| 什么应什么合| 三十六计最后一计是什么| 热忱是什么意思| 流鼻血是什么病的前兆| 支原体肺炎用什么药| 梦见长白头发是什么意思| 3岁宝宝流鼻血是什么原因| 64是什么| 扁平疣是什么原因引起的| 99新是什么意思| hbsab是什么意思| 怀孕什么时候打掉最好| 雷特综合症是什么症状| 孩子睡觉出汗多是什么原因| 什么火没有烟| 三纲指的是什么| a股是什么| 看甲状腺挂什么科| 老公护着婆婆说明什么| 盐酸吗啉胍片是什么药| 蕾丝是什么| 三观不正是什么意思| 月经量突然减少是什么原因| 人体缺钾是什么原因引起的| 胃肠炎可以吃什么水果| 仰面朝天是什么生肖| 喉咙不舒服吃什么药| 阴道炎用什么栓剂| 订盟是什么意思| 碧螺春是什么茶| 右侧卵巢多囊样改变是什么意思| 想念是什么意思| 为什么鼻子无缘无故流鼻血| 熊是什么意思| 2000年龙是什么命| 粘纤是什么材料| 男人不够硬吃什么好| 什么学海无涯苦作舟| 为什么头发会变白| 肺的作用和功能是什么| 黑松露什么味道| 上午十点多是什么时辰| 什么有条| 平常平时叫什么日| 1975年是什么年| 鬓角长痘痘是什么原因| 网络维护是做什么的| 血尿是什么原因| wpc是什么意思| 秋后问斩是什么意思| 导管子是什么意思| 什么可以误诊为畸胎瘤| 2016年是属什么年| 标准员是干什么的| 一什么面包| 肌酸什么时候喝| 锁骨疼挂什么科| 室性期前收缩是什么意思| 海菜是什么| 梦见自己准备结婚是什么意思| 笑口常开是什么生肖| 黄喉是什么动物身上的| 大人吃什么排黄疸快| 常染色体是什么| 尿路感染吃什么药效果最好| 教师节应该送老师什么花| 初恋是什么意思| 今年67岁属什么生肖| 火箭军是干什么的| 伏羲姓什么| 眼角发痒是什么原因| qy是什么意思| 什么是处男| 啤酒加味精有什么作用| 湿疹是什么病的前兆| 什么是埋线双眼皮| 羊宝是什么东西| sdnn是什么意思| 莫名心慌是什么原因| 铁观音什么季节喝最好| 什么好| 慢性浅表性胃炎吃什么药好| 因果循环是什么意思| 淋巴净排是什么服务| 新生儿老打嗝什么原因| 祛湿吃什么食物| 睡觉咳嗽是什么原因| 封顶是什么意思| 蚊子长什么样| 血管瘤吃什么药| 修心是什么意思| 什么是修行| 蜈蚣是什么生肖| 什么自行车最贵| 荔枝长什么样| 暴龙眼镜什么档次| 老公的姐姐叫什么| 梦到刷牙什么意思| 中药什么时候吃最好| 白月光是什么意思| 1994年是什么年| 女生生日送什么礼物好| ca代表什么病| 小腿麻木是什么原因| sm是什么意思| 月经期间吃什么对身体好| 腰椎间盘突出什么症状| 丹参长什么样子图片| 看口臭挂什么科| 梦见蛇是什么意思| 社区医院属于什么级别| 麦粒肿滴什么眼药水| 九月初八是什么星座| 3月12是什么星座| 什么在千里| 百香果吃了有什么好处| 258是什么意思| 肺寒咳嗽吃什么药| 夏天可以玩什么| 气管炎吃什么好| 良知是什么意思| 甲抗是什么原因引起的| 内痔有什么症状与感觉| 男的为什么喜欢男的| 脾胃不好吃什么食物好| hhh是什么意思| 古今内衣是什么档次| 压力山大什么意思| 老年人爱出汗是什么原因| 嘴巴里长血泡是什么原因| 肝火旺是什么症状| 胆囊结石需要注意什么| 什么的水珠| 老虎头是什么牌子衣服| 桑树枝煮水有什么功效| 胃不好的人吃什么养胃| 梦到离婚是什么征兆| 刘伯温属什么生肖| 氢化植物油是什么| 石花膏是什么做的| 子宫肌瘤是什么| 七月十三日是什么日子| 咖啡是什么做的| 免疫什么意思| 钱学森发明了什么| 核磁共振是检查什么的| 飞字五行属什么| 玖字五行属什么| 亚麻是什么植物| 吸气是什么意思| 速度是70迈心情是自由自在什么歌| 劫财是什么意思| 射精什么感觉| 给孕妇送什么礼物好| maxco是什么牌子| UDCA是什么药| yair是什么牌子的空调| 比心是什么意思| 18年属什么| 为什么女追男没好下场| 哔哩哔哩是干什么的| 牛头马面是什么生肖| 胆摘除对身体有什么影响| 中暑什么感觉| 一什么眉毛| 男生的蛋蛋长什么样| 手指甲有黑色条纹是什么原因| 冬天喝什么茶好呢| 16年属什么| 心影饱满是什么意思| d是什么元素| 宫颈异常是什么意思| 什么洗面奶好| 下午2点是什么时辰| 小ck属于什么档次| 疼风是什么原因引起的| 为什么吃辣的就拉肚子| 癫狂是什么意思| 什么叫吐槽| 什么是非甾体抗炎药| 碧玺五行属什么| 女人梦见下雪是什么征兆| 骨髓水肿是什么意思| 1942年属什么生肖| 疴是什么意思| 热锅上的蚂蚁是什么意思| 血糖低吃什么补得最快| 喜欢花的女人是什么性格| 百度Jump to content

探访萨珊古都比沙普尔

From Wikipedia, the free encyclopedia
百度 北京时间3月20日,湖人在客场以10分差距不敌步行者,但菜鸟新秀库兹马依然打出极为神勇一战,他首发作战出场37分钟,哪怕三分球仅有10中3准星,却奉献8中7的超高两分球效率,成为全场当之无愧的中投王,并且最终轰下27分8篮板的准两双数据,力压奥拉迪波等成为全场得分王。

The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently", introduced by the Gang of Four.[1] The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes.

When a class varies often, the features of object-oriented programming become very useful because changes to a program's code can be made easily with minimal prior knowledge about the program. The bridge pattern is useful when both the class and what it does vary often. The class itself can be thought of as the abstraction and what the class can do as the implementation. The bridge pattern can also be thought of as two layers of abstraction.

When there is only one fixed implementation, this pattern is known as the Pimpl idiom in the C++ world.

The bridge pattern is often confused with the adapter pattern, and is often implemented using the object adapter pattern; e.g., in the Java code below.

Variant: The implementation can be decoupled even more by deferring the presence of the implementation to the point where the abstraction is utilized.

Overview

[edit]

The Bridge design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.[1]

What problems can the Bridge design pattern solve?[2]

  • An abstraction and its implementation should be defined and extended independently from each other.
  • A compile-time binding between an abstraction and its implementation should be avoided so that an implementation can be selected at run-time.

When using subclassing, different subclasses implement an abstract class in different ways. But an implementation is bound to the abstraction at compile-time and cannot be changed at run-time.

What solution does the Bridge design pattern describe?

  • Separate an abstraction (Abstraction) from its implementation (Implementor) by putting them in separate class hierarchies.
  • Implement the Abstraction in terms of (by delegating to) an Implementor object.

This enables to configure an Abstraction with an Implementor object at run-time.
See also the Unified Modeling Language class and sequence diagram below.

Structure

[edit]

UML class and sequence diagram

[edit]
A sample UML class and sequence diagram for the Bridge design pattern.[3]

In the above Unified Modeling Language class diagram, an abstraction (Abstraction) is not implemented as usual in a single inheritance hierarchy. Instead, there is one hierarchy for an abstraction (Abstraction) and a separate hierarchy for its implementation (Implementor), which makes the two independent from each other. The Abstraction interface (operation()) is implemented in terms of (by delegating to) the Implementor interface (imp.operationImp()).
The UML sequence diagram shows the run-time interactions: The Abstraction1 object delegates implementation to the Implementor1 object (by calling operationImp() on Implementor1), which performs the operation and returns to Abstraction1.

Class diagram

[edit]

Abstraction (abstract class)
defines the abstract interface
maintains the Implementor reference.
RefinedAbstraction (normal class)
extends the interface defined by Abstraction
Implementor (interface)
defines the interface for implementation classes
ConcreteImplementor (normal class)
implements the Implementor interface
Bridge in LePUS3 (legend)

Example

[edit]

C#

[edit]

Bridge pattern compose objects in tree structure. It decouples abstraction from implementation. Here abstraction represents the client from which the objects will be called. An example implemented in C# is given below

// Helps in providing truly decoupled architecture
public interface IBridge
{
    void Function1();
    void Function2();
}

public class Bridge1 : IBridge
{
    public void Function1()
    {
        Console.WriteLine("Bridge1.Function1");
    }

    public void Function2()
    {
        Console.WriteLine("Bridge1.Function2");
    }
}

public class Bridge2 : IBridge
{
    public void Function1()
    {
        Console.WriteLine("Bridge2.Function1");
    }

    public void Function2()
    {
        Console.WriteLine("Bridge2.Function2");
    }
}

public interface IAbstractBridge
{
    void CallMethod1();
    void CallMethod2();
}

public class AbstractBridge : IAbstractBridge
{
    public IBridge bridge;

    public AbstractBridge(IBridge bridge)
    {
        this.bridge = bridge;
    }

    public void CallMethod1()
    {
        this.bridge.Function1();
    }

    public void CallMethod2()
    {
        this.bridge.Function2();
    }
}

The Bridge classes are the Implementation that uses the same interface-oriented architecture to create objects. On the other hand, the abstraction takes an instance of the implementation class and runs its method. Thus, they are completely decoupled from one another.

Crystal

[edit]
abstract class DrawingAPI
  abstract def draw_circle(x : Float64, y : Float64, radius : Float64)
end

class DrawingAPI1 < DrawingAPI
  def draw_circle(x : Float, y : Float, radius : Float)
    "API1.circle at #{x}:#{y} - radius: #{radius}"
  end
end

class DrawingAPI2 < DrawingAPI
  def draw_circle(x : Float64, y : Float64, radius : Float64)
    "API2.circle at #{x}:#{y} - radius: #{radius}"
  end
end

abstract class Shape
  protected getter drawing_api : DrawingAPI

  def initialize(@drawing_api)
  end

  abstract def draw
  abstract def resize_by_percentage(percent : Float64)
end

class CircleShape < Shape
  getter x : Float64
  getter y : Float64
  getter radius : Float64

  def initialize(@x, @y, @radius, drawing_api : DrawingAPI)
    super(drawing_api)
  end

  def draw
    @drawing_api.draw_circle(@x, @y, @radius)
  end

  def resize_by_percentage(percent : Float64)
    @radius *= (1 + percent/100)
  end
end

class BridgePattern
  def self.test
    shapes = [] of Shape
    shapes << CircleShape.new(1.0, 2.0, 3.0, DrawingAPI1.new)
    shapes << CircleShape.new(5.0, 7.0, 11.0, DrawingAPI2.new)

    shapes.each do |shape|
      shape.resize_by_percentage(2.5)
      puts shape.draw
    end
  end
end

BridgePattern.test

Output

API1.circle at 1.0:2.0 - radius: 3.075
API2.circle at 5.0:7.0 - radius: 11.275

C++

[edit]
import std;

class DrawingAPI {
  public:
    virtual ~DrawingAPI() = default;
    virtual std::string DrawCircle(float x, float y, float radius) const = 0;
};

class DrawingAPI01 : public DrawingAPI {
  public:
    std::string DrawCircle(float x, float y, float radius) const override {
      return std::format("API01.circle at {}:{} - radius: {}", std::to_string(x), std::to_string(y), std::to_string(radius)); 
    }
};

class DrawingAPI02 : public DrawingAPI {
  public:
    std::string DrawCircle(float x, float y, float radius) const override {
      return std::format("API02.circle at {}:{} - radius: {}", std::to_string(x), std::to_string(y), std::to_string(radius));
    }
};

class Shape {
  public:
    Shape(const DrawingAPI& drawing_api):
    drawing_api_(drawing_api) {}
    virtual ~Shape() = default;

    virtual std::string Draw() const = 0;
    virtual float ResizeByPercentage(const float percent) = 0;

  protected:
    const DrawingAPI& drawing_api_;
};

class CircleShape: public Shape {
  public:    
    CircleShape(float x, float y, float radius, const DrawingAPI& drawing_api): 
    Shape(drawing_api), x_(x), y_(y), radius_(radius) {}

    std::string Draw() const override {
        return drawing_api_.DrawCircle(x_, y_, radius_);
    }

    float ResizeByPercentage(const float percent) override {
      return radius_ *= (1.0f + percent/100.0f);
    }
  
  private:
    float x_, y_, radius_;
};

int main(int argc, char** argv) {
  const DrawingAPI01 api1{};
  const DrawingAPI02 api2{};
  std::vector<CircleShape> shapes {
    CircleShape{1.0f, 2.0f, 3.0f, api1},
    CircleShape{5.0f, 7.0f, 11.0f, api2}
  }; 

  for (CircleShape& shape: shapes) {
    shape.ResizeByPercentage(2.5);
    std::println("{}", shape.Draw());
  }

  return 0;
}

Output:

API01.circle at 1.000000:2.000000 - radius: 3.075000
API02.circle at 5.000000:7.000000 - radius: 11.275000

Java

[edit]

The following Java program defines a bank account that separates the account operations from the logging of these operations.

// Logger has two implementations: info and warning
@FunctionalInterface
interface Logger {
    void log(String message);
    
    static Logger info() {
        return message -> System.out.println("info: " + message);
    }
    static Logger warning() {
        return message -> System.out.println("warning: " + message);
    }
}

abstract class AbstractAccount {
    private Logger logger = Logger.info();
    
    public void setLogger(Logger logger) {
        this.logger = logger;
    }
    
    // the logging part is delegated to the Logger implementation
    protected void operate(String message, boolean result) {
        logger.log(message + " result " + result);
    }
}

class SimpleAccount extends AbstractAccount {
    private int balance;
    
    public SimpleAccount(int balance) {
        this.balance = balance;
    }
    
    public boolean isBalanceLow() {
        return balance < 50;
    }
    
    public void withdraw(int amount) {
        boolean shouldPerform = balance >= amount;
        if (shouldPerform) {
            balance -= amount;
        }
        operate("withdraw " + amount, shouldPerform);
    }
}

public class BridgeDemo {
    public static void main(String[] args) {
        SimpleAccount account = new SimpleAccount(100);
        account.withdraw(75);
        
        if (account.isBalanceLow()) {
            // you can also change the Logger implementation at runtime
            account.setLogger(Logger.warning());
        }
        
        account.withdraw(10);
        account.withdraw(100);
    }
}

It will output:

info: withdraw 75 result true
warning: withdraw 10 result true
warning: withdraw 100 result false

PHP

[edit]
interface DrawingAPI
{
    function drawCircle($x, $y, $radius);
}

class DrawingAPI1 implements DrawingAPI
{
    public function drawCircle($x, $y, $radius)
    {
        echo "API1.circle at $x:$y radius $radius.\n";
    }
}

class DrawingAPI2 implements DrawingAPI
{
    public function drawCircle($x, $y, $radius)
    {
        echo "API2.circle at $x:$y radius $radius.\n";
    }
}

abstract class Shape
{
    protected $drawingAPI;

    public abstract function draw();
    public abstract function resizeByPercentage($pct);

    protected function __construct(DrawingAPI $drawingAPI)
    {
        $this->drawingAPI = $drawingAPI;
    }
}

class CircleShape extends Shape
{
    private $x;
    private $y;
    private $radius;

    public function __construct($x, $y, $radius, DrawingAPI $drawingAPI)
    {
        parent::__construct($drawingAPI);
        $this->x = $x;
        $this->y = $y;
        $this->radius = $radius;
    }

    public function draw()
    {
        $this->drawingAPI->drawCircle($this->x, $this->y, $this->radius);
    }

    public function resizeByPercentage($pct)
    {
        $this->radius *= $pct;
    }
}

class Tester
{
    public static function main()
    {
        $shapes = array(
            new CircleShape(1, 3, 7,  new DrawingAPI1()),
            new CircleShape(5, 7, 11, new DrawingAPI2()),
        );

        foreach ($shapes as $shape) {
            $shape->resizeByPercentage(2.5);
            $shape->draw();
        }
    }
}

Tester::main();

Output:

API1.circle at 1:3 radius 17.5
API2.circle at 5:7 radius 27.5

Scala

[edit]
trait DrawingAPI {
  def drawCircle(x: Double, y: Double, radius: Double)
}

class DrawingAPI1 extends DrawingAPI {
  def drawCircle(x: Double, y: Double, radius: Double) = println(s"API #1 $x $y $radius")
}

class DrawingAPI2 extends DrawingAPI {
  def drawCircle(x: Double, y: Double, radius: Double) = println(s"API #2 $x $y $radius")
}

abstract class Shape(drawingAPI: DrawingAPI) {
  def draw()
  def resizePercentage(pct: Double)
}

class CircleShape(x: Double, y: Double, var radius: Double, drawingAPI: DrawingAPI)
    extends Shape(drawingAPI: DrawingAPI) {

  def draw() = drawingAPI.drawCircle(x, y, radius)

  def resizePercentage(pct: Double) { radius *= pct }
}

object BridgePattern {
  def main(args: Array[String]) {
    Seq (
	new CircleShape(1, 3, 5, new DrawingAPI1),
	new CircleShape(4, 5, 6, new DrawingAPI2)
    ) foreach { x =>
        x.resizePercentage(3)
        x.draw()			
      }	
  }
}

Python

[edit]
"""
Bridge pattern example.
"""
from abc import ABCMeta, abstractmethod


NOT_IMPLEMENTED = "You should implement this."


class DrawingAPI:
    __metaclass__ = ABCMeta

    @abstractmethod
    def draw_circle(self, x, y, radius):
        raise NotImplementedError(NOT_IMPLEMENTED)


class DrawingAPI1(DrawingAPI):
    def draw_circle(self, x, y, radius):
        return f"API1.circle at {x}:{y} - radius: {radius}"


class DrawingAPI2(DrawingAPI):
    def draw_circle(self, x, y, radius):
        return f"API2.circle at {x}:{y} - radius: {radius}"


class DrawingAPI3(DrawingAPI):
    def draw_circle(self, x, y, radius):
        return f"API3.circle at {x}:{y} - radius: {radius}"


class Shape:
    __metaclass__ = ABCMeta

    drawing_api = None
    def __init__(self, drawing_api):
        self.drawing_api = drawing_api

    @abstractmethod
    def draw(self):
        raise NotImplementedError(NOT_IMPLEMENTED)

    @abstractmethod
    def resize_by_percentage(self, percent):
        raise NotImplementedError(NOT_IMPLEMENTED)


class CircleShape(Shape):
    def __init__(self, x, y, radius, drawing_api):
        self.x = x
        self.y = y
        self.radius = radius
        super(CircleShape, self).__init__(drawing_api)


    def draw(self):
        return self.drawing_api.draw_circle(self.x, self.y, self.radius)

    def resize_by_percentage(self, percent):
        self.radius *= 1 + percent / 100


class BridgePattern:
    @staticmethod
    def test():
        shapes = [
            CircleShape(1.0, 2.0, 3.0, DrawingAPI1()),
            CircleShape(5.0, 7.0, 11.0, DrawingAPI2()),
            CircleShape(5.0, 4.0, 12.0, DrawingAPI3()),
        ]

        for shape in shapes:
            shape.resize_by_percentage(2.5)
            print(shape.draw())


BridgePattern.test()

See also

[edit]

References

[edit]
  1. ^ a b Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. p. 151. ISBN 0-201-63361-2.
  2. ^ "The Bridge design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2025-08-06.
  3. ^ "The Bridge design pattern - Structure and Collaboration". w3sDesign.com. Retrieved 2025-08-06.
[edit]
阳是什么意思 脸部出油多是什么原因 肚脐眼中间疼是什么原因 裸妆是什么意思 人生得意须尽欢什么意思
梦见蚯蚓是什么预兆 肠系膜多发淋巴结是什么意思 3月11日什么星座 虎是什么意思 gn是什么意思
此什么非彼什么的意思 36周检查什么项目 官杀旺是什么意思 走婚是什么意思 cmb是什么意思
三天不打上房揭瓦的下一句是什么 肚子胀打嗝是什么原因 额头和下巴长痘痘是什么原因 消化内科主要看什么病 由来是什么意思
沂字五行属什么xinjiangjialails.com 白头发吃什么能变黑hcv7jop4ns6r.cn 反流性食管炎吃什么药最好dayuxmw.com 行了是什么意思hcv9jop5ns1r.cn 走投无路是什么意思hcv7jop5ns0r.cn
海澜之家属于什么档次hcv8jop2ns1r.cn 人这一生什么最重要hcv9jop4ns9r.cn 为什么智齿老是发炎hcv7jop5ns6r.cn 巴结是什么意思hcv8jop5ns8r.cn 碧玺是什么hcv7jop4ns8r.cn
c1e驾照能开什么车clwhiglsz.com 为什么会得肠胃炎hcv9jop2ns9r.cn 乳腺癌多发于什么年龄hcv9jop3ns3r.cn 读书破万卷下一句是什么hcv9jop5ns6r.cn 后话是什么意思hcv9jop2ns1r.cn
成人礼送什么礼物hcv9jop1ns6r.cn 为什么学习不好cl108k.com 蕌头是什么hcv9jop7ns2r.cn 蒙古族不吃什么肉hcv8jop4ns9r.cn 刘邦是什么生肖jasonfriends.com
百度