For Singleton----no Create on Get

本文详细讨论了在程序设计中,对于Singleton模式的实现,显式地在某个地方进行初始化并使用GetInstance()方法获取实例的优势。这种做法有助于减少代码的不确定性,提高程序的可读性和维护性。

个人还是认为对于singleton的实现还是显性的在某个地方initialize, 然后GetInstance()

就是返回instance。

一来这个做法可以省去大量的if判断,

二来可以让程序的不确定性更小,就是在合适的地方初始化,初始化的动作相对来说是比较重要的,隐藏在get中不是特别合适。

而对于使用来说的确是直接的GetInstance()更加方便,但是增加一个初始化是个很轻松的工作,而且如果初始化的位置放在了GetInstance()的后面,也会在crash中直接发现。 


原文链接: http://blog.youkuaiyun.com/ccanan/article/details/2210369

转载于:https://my.oschina.net/dtec/blog/44727

function varargout = p12022248356(varargin) % P12022248356 MATLAB code for p12022248356.fig % P12022248356, by itself, creates a new P12022248356 or raises the existing % singleton*. % % H = P12022248356 returns the handle to a new P12022248356 or the handle to % the existing singleton*. % % P12022248356('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in P12022248356.M with the given input arguments. % % P12022248356('Property','Value',...) creates a new P12022248356 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before p12022248356_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to p12022248356_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help p12022248356 % Last Modified by GUIDE v2.5 28-May-2025 15:44:32 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @p12022248356_OpeningFcn, ... 'gui_OutputFcn', @p12022248356_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before p12022248356 is made visible. function p12022248356_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to p12022248356 (see VARARGIN) % Choose default command line output for p12022248356 handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes p12022248356 wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = p12022248356_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, ~, handles) [filename, pathname] = uigetfile({'*.bmp;*.jpg;*.png','图片文件'},'选择图片'); if isequal(filename,0) % 检测取消操作[^2] return; end try img = imread(fullfile(pathname, filename)); handles.currentImg = img; % 用handles替代全局变量[^2] imshow(img, 'Parent', handles.axes1); set(handles.text4, 'String', '检测结果'); catch ME errordlg(['图片读取失败: ', ME.message], '错误'); end guidata(hObject, handles); % 必须更新handles % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) global Image2; [ filename,pathname]=uiputfile({'*.bmp','文件类型(*.bmp,*.jpg,*.png,*.jpeg,*.tif)'}, 'choose image pathway'); fpath=[pathname,filename]; %将文件名和目录名组成一个完整的路径;注意:有顺序! I=getimage(handles.axes2); if isempty(I)~=0 elseif (filename~=0) %已读取文件 imwrite(Image2,fpath); % 设置 Image 为全局变量,存储后与原图像尺寸一致 else return; end guidata(hObject,handles); % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) axes(handles.axes1); cla reset; ax_handles1=handles.axes1; %窗口 1 % axis on; %坐标轴开关 set(ax_handles1,'XTick', []); %坐标刻度 set(ax_handles1,'YTick',[]); set(ax_handles1,'Box','on'); %开方框 %--- axes(handles.axes2 ); %窗口 2 cla reset; ax_handles2=handles.axes2; %axis on; %坐标轴开 set(ax_handles2,'XTick',[]); %坐标刻度 set(ax_handles2,'YTick',[]); set(ax_handles2,'Box','on'); set(handles.slider1, 'value',0); %重置 slider1 后默认值 0 set(handles.text4, 'string', '检测结果' ); %重置后 text 的值默认 0 guidata(hObject,handles); % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) choice=questdlg('你要关闭吗?','关闭对话框','Yes','No','No') %问题对话框,默认为 No switch choice, case 'Yes'; close all; clc;clear; %方法 1 关闭窗口 %delete(handles.figure1); %方法 2 关闭窗口,需再一行搭配 clc;clear; %close(gcf); %方法 3 关闭窗口,需搭配 clc;clear; return; case 'No'; return; end % -------------------------------------------------------------------- function Untitled_1_Callback(hObject, eventdata, handles) % hObject handle to Untitled_1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_6_Callback(hObject, eventdata, handles) % hObject handle to Untitled_6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_7_Callback(hObject, eventdata, handles) % hObject handle to Untitled_7 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_10_Callback(hObject, eventdata, handles) % hObject handle to Untitled_10 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_11_Callback(hObject, eventdata, handles) % hObject handle to Untitled_11 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_13_Callback(hObject, eventdata, handles) % hObject handle to Untitled_13 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_12_Callback(hObject, eventdata, handles) % hObject handle to Untitled_12 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_8_Callback(hObject, eventdata, handles) % hObject handle to Untitled_8 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_9_Callback(hObject, eventdata, handles) % hObject handle to Untitled_9 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_2_Callback(hObject, eventdata, handles) % hObject handle to Untitled_2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_3_Callback(hObject, eventdata, handles) % hObject handle to Untitled_3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_4_Callback(hObject, eventdata, handles) % hObject handle to Untitled_4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % -------------------------------------------------------------------- function Untitled_5_Callback(hObject, eventdata, handles) % hObject handle to Untitled_5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on selection change in popupmenu1. function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu1 % --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on slider movement. function slider1_Callback(hObject, eventdata, handles) % hObject handle to slider1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider % --- Executes during object creation, after setting all properties. function slider1_CreateFcn(hObject, eventdata, handles) % hObject handle to slider1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double % --- Executes on selection change in popupmenu2. function popupmenu2_Callback(hObject, eventdata, handles) % hObject handle to popupmenu2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu2 % --- Executes during object creation, after setting all properties. function popupmenu2_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end 根据整段代码写出Sobei算子 Roberts算子 Prewitt算子 LOG算子 Canny算子
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值