0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MATLAB言語で学ぶ「Java言語で学ぶデザインパターン入門」ノート #9 Bridge

Posted at

概要

Bridge パターンは、機能と実装を分離することで機能拡張をしやすくする、デザインパターンの一つである。

ソースコード

Display.m
classdef Display < handle
    properties (Access = private)
        impl(1,1) % DisplayImpl
    end
    methods (Access = public)
        function obj = Display(impl)
            arguments (Input)
                impl(1,1) DisplayImpl
            end
            arguments (Output)
                obj(1,1) Display
            end
            obj.impl = impl;
        end
        function open(obj)
            arguments (Input)
                obj(1,1) Display
            end
            obj.impl.rawOpen();
        end
        function print(obj)
            arguments (Input)
                obj(1,1) Display
            end
            obj.impl.rawPrint();
        end
        function close(obj)
            arguments (Input)
                obj(1,1) Display
            end
            obj.impl.rawClose();
        end
    end
    methods (Access = public, Sealed)
        function display_(obj)
            arguments (Input)
                obj(1,1) Display
            end
            obj.open();
            obj.print();
            obj.close();
        end
    end
end
CountDisplay.m
classdef CountDisplay < Display
    methods (Access = public)
        function obj = CountDisplay(impl)
            arguments (Input)
                impl(1,1) DisplayImpl
            end
            arguments (Output)
                obj(1,1) CountDisplay
            end
            obj = obj@Display(impl);
        end
        function multiDisplay(obj, times)
            arguments (Input)
                obj(1,1) CountDisplay
                times(1,1) int32
            end
            obj.open();
            for k = 1:times
                obj.print();
            end
            obj.close();
        end
    end
end
DisplayImpl.m
classdef (Abstract) DisplayImpl
    methods (Abstract, Access = public)
        rawOpen(obj)
        rawPrint(obj)
        rawClose(obj)
    end
end
StringDisplayImpl.m
classdef StringDisplayImpl < DisplayImpl
    properties (Access = private)
        string(1,:) char {mustBeTextScalar}
        width(1,1) double {mustBeInteger}
    end
    methods (Access = public)
        function obj = StringDisplayImpl(string)
            obj.string = string;
            obj.width = length(string);
        end
        function rawOpen(obj)
            obj.printLine();
        end
        function rawPrint(obj)
            disp(['|', obj.string, '|']);
        end
        function rawClose(obj)
            obj.printLine();
        end
    end
    methods (Access = private)
        function printLine(obj)
            fprintf('+');
            for k = 1:obj.width
                fprintf('-');
            end
            disp('+');
        end
    end
end
main.m
function main()
d1 = Display(StringDisplayImpl('Hello, Japan.'));
d2 = CountDisplay(StringDisplayImpl('Hello, World.'));
d3 = CountDisplay(StringDisplayImpl('Hello, Universe.'));
d1.display_();
d2.display_();
d3.display_();
d3.multiDisplay(5);
end

実行結果

>> main
+-------------+
|Hello, Japan.|
+-------------+
+-------------+
|Hello, World.|
+-------------+
+----------------+
|Hello, Universe.|
+----------------+
+----------------+
|Hello, Universe.|
|Hello, Universe.|
|Hello, Universe.|
|Hello, Universe.|
|Hello, Universe.|
+----------------+
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?