Статья
Шаблон проектирования "Мост"

Шаблон проектирования "Мост"

3 июня 2020

Относится к структурным шаблонам.

В данном шаблоне используется предпочтение компоновки наследованию.


Пример на PHP

Пусть имеется интерфейс Formatter и 2 разных вида форматирования - PlainTextFormatter и HtmlFormatter:

interface Formatter
{
    public function format(string $text): string;
}

class PlainTextFormatter implements Formatter
{
    public function format(string $text): string
    {
        return $text;
    }
}

class HtmlFormatter implements Formatter
{
    public function format(string $text): string
    {
        return sprintf("<p>" . $text . "</p>");
    }
}

Также пусть имеются 2 сервиса - HelloService и GoodbyeService:

abstract class Service
{
    protected Formatter $implementation;

    public function __construct(Formatter $impl)
    {
        $this->setImplementation($impl);
    }

    public function setImplementation(Formatter $impl)
    {
        $this->implementation = $impl;
    }

    abstract public function get(): string;
}

class HelloService extends Service
{
    public function get(): string
    {
        return $this->implementation->format('Hello!');
    }
}

class GoodbyeService extends Service
{
    public function get(): string
    {
        return $this->implementation->format('Goodbye!');
    }
}

Использование:

$helloService = new HelloService(new PlainTextFormatter());
$helloService->get(); // Hello!
$helloService->setImplementation(new HtmlFormatter());
$helloService->get(); // <p>Hello!</p>

$goodbyeService = new GoodbyeService(new HtmlFormatter());
$goodbyeService->get(); // <p>Goodbye!</p>
$goodbyeService->setImplementation(new PlainTextFormatter());
$goodbyeService->get(); // Goodbye!


Пример на Go

package main

type IFormatter interface {
   Format(text string) string
}

type PlainTextFormatter struct {
}

func (f *PlainTextFormatter) Format(text string) string {
   return text
}

type HtmlFormatter struct {
}

func (f *HtmlFormatter) Format(text string) string {
   return "<p>" + text + "</p>"
}

type IService interface {
   SetFormatter(formatter IFormatter)
   Get() string
}

type BaseService struct {
   formatter IFormatter
}

func (s *BaseService) SetFormatter(formatter IFormatter) {
   s.formatter = formatter
}

type HelloService struct {
   BaseService
}

func (s *HelloService) Get() string {
   return s.formatter.Format("Hello!")
}

type GoodbyeService struct {
   BaseService
}

func (s *GoodbyeService) Get() string {
   return s.formatter.Format("Goodbye!")
}

func main() {
   plainTextFormatter := PlainTextFormatter{}
   htmlFormatter := HtmlFormatter{}

   helloService := HelloService{}
   helloService.SetFormatter(&plainTextFormatter)
   helloService.Get() // Hello!

   helloService.SetFormatter(&htmlFormatter)
   helloService.Get() // <p>Hello!</p>

   goodbyeService := GoodbyeService{}
   goodbyeService.SetFormatter(&plainTextFormatter)
   goodbyeService.Get() // Goodbye!

   goodbyeService.SetFormatter(&htmlFormatter)
   goodbyeService.Get() // <p>Goodbye!</p>
}



Источники:
http://designpatternsphp.readthedocs.io/ru/latest/Structural/Bridge/README.html
https://habrahabr.ru/company/mailru/blog/325492/