gusucode.com > bigdata 工具箱 matlab源码程序 > bigdata/+matlab/+bigdata/+internal/+util/UniqueIdFactory.m

    %UniqueIdFactory
% A helper class that generates unique IDs using a combination of tempname
% and a local ID count.
%
% This will generate string IDs.

% Copyright 2015 The MathWorks, Inc.

classdef (Sealed) UniqueIdFactory < handle
    properties (SetAccess = immutable)
        % The name to attach to all generated IDs.
        Name;
    end
    
    properties (SetAccess = private)
        % The number of IDs generated by this object.
        NumIds = 0;
    end
    
    methods
        % The main constructor.
        function obj = UniqueIdFactory(name)
            [~, randomString] = fileparts(tempname);
            obj.Name = sprintf('%s_%s', name, randomString);
        end
        
        % Generate a new ID.
        function id = nextId(obj)
            obj.NumIds = obj.NumIds + 1;
            id = sprintf('%s_%i', obj.Name, obj.NumIds);
        end
    end
end