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言語で学ぶデザインパターン入門」ノート #3 Template Method

Posted at

概要

Template Method パターンは、スーパークラスで各処理インターフェースを決定し、サブクラスで各処理を具象化する、デザインパターンの一つである。

ソースコード

AbstractDisplay.m
classdef (Abstract) AbstractDisplay
    methods (Abstract)
        open(obj);
        print(obj);
        close(obj);
    end
    methods (Access = public)
        function display_(obj)
            obj.open();
            for i = 1:5
                obj.print();
            end
            obj.close();
        end
    end
end
CharDisplay.m
classdef CharDisplay < AbstractDisplay
    properties (Access = private)
        ch (1,1) char
    end
    methods (Access = public)
        function obj = CharDisplay(ch)
            arguments (Input)
                ch (1,1) char
            end
            arguments (Output)
                obj (1,1) CharDisplay
            end
            obj.ch = ch;
        end
        function open(obj)
            arguments (Input)
                obj (1,1) CharDisplay
            end
            fprintf('<<');
        end
        function print(obj)
            arguments (Input)
                obj (1,1) CharDisplay
            end
            fprintf(obj.ch);
        end
        function close(obj)
            arguments (Input)
                obj (1,1) CharDisplay
            end
            fprintf('>>\n');
        end
    end
end
StringDisplay.m
classdef StringDisplay < AbstractDisplay
    properties (Access = private)
        string char {mustBeTextScalar} = ''
        width (1,1) double {mustBeInteger}
    end
    methods (Access = public)
        function obj = StringDisplay(string)
            arguments (Input)
                string char {mustBeTextScalar}
            end
            arguments (Output)
                obj (1,1) StringDisplay
            end
            obj.string = string;
            obj.width = length(string);
        end
        function open(obj)
            arguments (Input)
                obj (1,1) StringDisplay
            end
            obj.printLine();
        end
        function print(obj)
            arguments (Input)
                obj (1,1) StringDisplay
            end
            disp(['|', obj.string, '|']);
        end
        function close(obj)
            arguments (Input)
                obj (1,1) StringDisplay
            end
            obj.printLine();
        end
        function printLine(obj)
            fprintf('+');
            for i = 1:obj.width
                fprintf('-');
            end
            disp('+');
        end
    end
end
main.m
function main
d1 = CharDisplay('H');
d2 = StringDisplay('Hello, world.');
d3 = StringDisplay('こんにちは');

d1.display_();
d2.display_();
d3.display_();
end

実行結果

>> main
<<HHHHH>>
+-------------+
|Hello, world.|
|Hello, world.|
|Hello, world.|
|Hello, world.|
|Hello, world.|
+-------------+
+-----+
|こんにちは|
|こんにちは|
|こんにちは|
|こんにちは|
|こんにちは|
+-----+
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?