Matlab单例模式
classdef SingletonClass < handle methods(Access = private) function obj = SingletonClass() disp('SingletonClass construtor called!'); end end methods(Static) function obj = getIns() persistent ins; if isempty(ins) || ~isvalid(ins) ins = SingletonClass(); end obj = ins; end end end
注:persistent变量不会随着.m文件运行结束而被清除,如果需要清除,可以有以下几种方法:
1、clear all
2、clear classes
3、clear SingletonClass
4、delete(SingletonClass.getIns())