-
Notifications
You must be signed in to change notification settings - Fork 1
/
Factory.m
55 lines (48 loc) · 1.23 KB
/
Factory.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
classdef Factory < handle
properties (Constant, Abstract, Access = protected)
% Default type-constructor pairs, e.g. {'type', @Constructor, ...}
defaults;
% Message for constructor exception errors
exception;
end
methods
% Factory Constructor
function self = Factory(cellarr)
if ~isempty(cellarr)
arr = cellarr{1};
map = containers.Map(arr(1:2:end), arr(2:2:end));
self.dictionary(map);
elseif isempty(self.dictionary)
map = containers.Map(self.defaults(1:2:end), self.defaults(2:2:end));
self.dictionary(map);
end
end
% Object Constructor
function obj = constructor(self, key, varargin)
try
dict = self.dictionary;
constructor = dict(key);
obj = constructor(varargin{:});
catch ME
if strcmp(ME.identifier, 'MATLAB:Containers:Map:NoKey')
util.exception(self.exception);
end
rethrow(ME);
end
end
% Expose append method to easily add new constructors
function append(self, key, val)
dict = self.dictionary;
keys = [dict.keys key];
vals = [dict.values {val}];
map = containers.Map(keys, vals);
self.dictionary(map)
end
end
methods (Abstract)
sth = generate(self, data);
end
methods (Static, Abstract)
out = dictionary(in);
end
end