The Bridge pattern allows the programmer to decouple an abstraction from its implementation so that the two can vary independently. This important design pattern is used in many professional software applications since it’s debut in 1994. For this review of the design pattern, I’ve used varying datatypes, which makes use of multiple spell checking libraries in PHP, each of which can vary independently from each other. I’ve used some PHP code to describe the behaviours underlying the ideas in the pattern. As shown below, this pattern does allow implementations to vary independently from one another.

Implementation Code



<?php 
/** 
* Bridge Pattern Implementation Example. 
* 
* @package Bridge Pattern 
* @version 0.1 
* @author CodeSnippetsandTutorials.com 
* @copyright 2018 CodeSnippetsandTutorials.com 
*/ 
require_once '../vendor/FigStandards/autoload.php'; 
//Load Dependencies 
$loader = new figStandards\Psr4AutoloaderClass; 
$loader->register();
$loader->addNamespace('bridge', 'datatype');
$loader->addNamespace('spellchecker', 'spellchecker');

class Client {

    function __construct() {

        $html_contents = "This is a sentence which is to be tested for a mistkkate.";
        
        $csv = new \bridge\CSV($html_contents);
        new \spellchecker\Pspell($csv->parse());


        $text_contents = "This is a collection of comma, seperated, values, which is to be tested, for a mistiake.\r\n";
        $text_contents .= "to be tested, testing\r\n";
        
        $t = new Text($text_contents);
        new \spellchecker\Enchant($t->parse());
    }

}

new Client();


Spell Checker Interface



<?php

/**
 * Bridge Pattern Implementation Example.
 * 
 * @package   Bridge
 * @version   0.1
 */

namespace spellchecker;

abstract class spellChecker {
    
   abstract public function __construct(Array $words);
   
   abstract public function word_check(string $word);
    
}

Enchant Implementation



namespace spellchecker;

class Enchant extends \spellchecker\spellChecker {

    protected $link;
    protected $tag = "en_US";

    /**
     * Iterates of the enumeration of words
     * 
     * @param array $words
     */
    public function __construct(Array $words) {

        $this->link = enchant_broker_init();

        $dicts = enchant_broker_list_dicts($this->link);

        $this->word_check($dicts);

        $this->iterate_collection($words);

        $this->free_enchant();
    }

    /**
     * Checks if a broker dictionary exists
     * 
     * @return type
     */
    public function broker_exists() {
        return (enchant_broker_dict_exists($this->link, $this->tag) ? true : false);
    }

    /**
     * Checks the word against speller
     * 
     * @param string $word
     * @return type
     * @throws Exception
     */
    public function word_check(string $word) {

        if ($this->broker_exists()) {
            return (enchant_dict_check(enchant_broker_request_dict($this->link, $this->tag), $word) ? true : false);
        }

        throw new Exception('Error!');
    }

    /**
     * Iterates the collection of words
     * @param type $words
     */
    public function iterate_collection($words) {
        foreach ($words as $word) {
            $this->word_check($word);
        }
    }

    /**
     * Free the enchant resource
     */
    public function free_enchant() {
        enchant_broker_free_dict($d);
    }

}

pSpell Implementation



namespace spellchecker;

class Pspell extends \spellchecker\spellChecker {

    protected $link;

    /**
     * Iterates of the enumeration of words
     * 
     * @param array $words
     */
    public function __construct(Array $words) {

        //instaniate pspell
        $this->pspell_link = \pspell_new("en");

        foreach ($words as $word) {
            $this->word_check($word);
        }
    }

    /**
     * Checks the word against the speller
     * 
     * @param string $word
     * @return type
     */
    public function word_check(string $word) {
        return ((\pspell_check($this->pspell_link, $word)) ? true : false);
    }

}

DataType Abstract Class


namespace bridge;

abstract class DataType {

     protected $data;
    
    /**
     * Implementation specific contents assignment to object
     * 
     * @param type $contents
     */
    abstract protected function __construct(string $contents);

    /**
     * Implementation specific Heuristic specific parser
     */
    protected function sanitise(): Array {
        preg_match_all("/(\w+)/", $this->data, $matches);
        return $matches[0];
    }
  
}

CSV Class


namespace bridge;

class CSV extends DataType {

    /**
     * {@inheritdoc}
     */
    public function __construct(string $contents) {
        $this->data = $contents;
    }

    /**
     * Important : Heuristic parsing for examples sake... Implement a Parser such as Parle.
     * 
     * {@inheritdoc}
     */
    public function parse(): Array {
        $this->data = strip_tags($this->data);
        return $this->sanitise();
    }

}

HTML Class


namespace bridge;

class HTML extends DataType {

    /**
     * {@inheritdoc}
     */
    public function __construct(string $contents) {
        $this->data = $contents;
    }

    /**
     * Important : Heuristic parsing for examples sake... Implement a Parser such as Parle.
     * 
     * {@inheritdoc}
     */
    public function parse(): Array {
        $this->data = strip_tags($this->data);
        return $this->sanitise();
    }

}

Text Class



namespace bridge;

class Text extends DataType {

    /**
     * {@inheritdoc}
     */
    public function __construct(string $contents) {
        $this->data = $contents;
    }

    /**
     * Important : Heuristic parsing for examples sake... Implement a Parser such as Parle.
     * 
     * {@inheritdoc}
     */
    public function parse(): Array {
        return $this->sanitise();
    }

}