Managing similar apps using inheritance?
조회 수: 7 (최근 30일)
이전 댓글 표시
I need to make approx. 20 apps that share a base set of app designer components and code and then provide extra functionality for a specific purpose. For example: a set of data processing programs with the same base app structure but each customized for a certain experiment. Is there a way to do this using inheritance or similar that would allow changes to the base code to propagate to the children?
The best idea I have is to create a base app and copy/paste that as a starting point for the children. That works for creating the apps but maintaining them could be difficult because every change to the base functionality needs to be repeated for each child.
I haven't found any examples where one app inherited another to extend the functionality. App merging sounded like a possible compromise (by making it easy to merge changes into child apps) but this only works for white editable code blocks and would not handle any changes to UI components or gray code blocks:
댓글 수: 0
채택된 답변
Lawrence
2021년 9월 8일
편집: Lawrence
2021년 9월 8일
A more supported way of re-using ui elements is by building custom component containers and adding them to your app: https://www.mathworks.com/help/matlab/developing-custom-ui-component-classes.html
That being said, you can subclass from apps built in App Designer, though your subclass needs to be in a ".m" file rather than a ".mlapp" file and can't itself be edited in App Designer. These subclass then unlock many more OOP design patterns. Note that there's no easy way to remove the app.UIFigure.Visible = 'on'; from the end of the superclass constructor, so be careful using this to change visual elements on construction.
subclass.m:
classdef subclass < superclass
methods
function app = subclass
app.SuperclassButton.Text = 'Subclass';
end
end
end
superclass.mlapp:
classdef superclass < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
SuperclassButton matlab.ui.control.Button
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create SuperclassButton
app.SuperclassButton = uibutton(app.UIFigure, 'push');
app.SuperclassButton.Position = [261 278 100 22];
app.SuperclassButton.Text = 'Superclass';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = superclass
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!