# Bridge

Когато имаме йерархии на интерфейсите и в двата интерфейса, както и в реализациите, тогава моделът на проектиране на моста се използва за отделяне на интерфейсите от имплементацията и за скриване на детайлите за изпълнение от клиентските програми. Изпълнението на модела на проектиране на моста следва идеята за предпочитане на [композицията](https://app.gitbook.com/o/c8e077E8abnSYoRWFCzu/s/-MUbVVR-jiMUx7iVRyw6/~/changes/338/obektno-orientirano-programirane-2-chast/laboratorno-uprazhnenie-6/bridge/kompoziciya) пред наследяването.

Да разгледаме следния пример с интерфейсите Shape и Color, където двата интерфейса се имплементират от няколко класа.

<figure><img src="https://3165743208-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MUbVVR-jiMUx7iVRyw6%2Fuploads%2FZoPYrAGFJFIKQDdrISRf%2Fimage.png?alt=media&#x26;token=ae4dbe49-ef2d-4bc5-8211-a235eb6d0ec8" alt=""><figcaption></figcaption></figure>

Забележете, че класовете който имплементират интерфейса Color се инстанцират директно в  класовете Triangie, Pentagon.

Когато използваме модела мост този модел ще изглежда по следния начин

{% code title="Color.java" lineNumbers="true" %}

```java
public interface Color {
    public void applyColor();
}
```

{% endcode %}

{% code title="Shape.java" lineNumbers="true" %}

```java
public abstract class Shape {
	//Composition - implementor
	protected Color color;
	
	//constructor with implementor as input argument
	public Shape(Color c){
		this.color=c;
	}
	
	abstract public void applyColor();
}
```

{% endcode %}

Забележете моста между абстракциите и използването на композиция при прилагането на  модела мост.

{% code title="Triangle.java" lineNumbers="true" %}

```java
public class Triangle extends Shape{

	public Triangle(Color c) {
		super(c);
	}

	@Override
	public void applyColor() {
		color.applyColor();
	} 
}
```

{% endcode %}

{% code title="Pentagon.java" lineNumbers="true" %}

```java
public class Pentagon extends Shape{

	public Pentagon(Color c) {
		super(c);
	}

	@Override
	public void applyColor() {
		color.applyColor();
	} 
}
```

{% endcode %}

{% code title="RedColor.java" lineNumbers="true" %}

```java
public class RedColor implements Color{

	public void applyColor(){
		System.out.println("red.");
	}
}
```

{% endcode %}

{% code title="GreenColor.java" lineNumbers="true" %}

```java
public class GreenColor implements Color{

	public void applyColor(){
		System.out.println("green.");
	}
}
```

{% endcode %}

{% code title="Application.java" lineNumbers="true" %}

```java
public class Application {

	public static void main(String[] args) {
		Shape tri = new Triangle(new RedColor());
		tri.applyColor();
		
		Shape pent = new Pentagon(new GreenColor());
		pent.applyColor();
	}

}
```

{% endcode %}

Моделът за проектиране на мостове може да се използва, когато абстракцията, така и имплементацията могат да имат различни йерархии независимо и искаме да скрием имплементацията от клиентското приложение.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://programmingfundamental.gitbook.io/programmingwithjava/obektno-orientirano-programirane-2-chast/laboratorno-uprazhnenie-6/bridge.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
