Managing similar apps using inheritance?

6 ビュー (過去 30 日間)
Derek Wolfe
Derek Wolfe 2021 年 6 月 15 日
コメント済み: Derek Wolfe 2021 年 9 月 27 日
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:

採用された回答

Lawrence
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
  1 件のコメント
Derek Wolfe
Derek Wolfe 2021 年 9 月 27 日
Thanks for the info on component containers. I ended up doing something similar where I created plugin classes that can be loaded by the app to add specific functionality. That avoids having to make so many versions of the base app itself

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by