Magic pushbutton

Magic pushbutton

The magic pushbutton is a common anti-pattern in graphical programming environments.[1] In this scenario, the programmer draws the user interface first and then writes the business logic in the automatically created methods.
In event-driven programming, a few functions or "event handlers" are written by the programmer that respond to the user's actions. This technology means that instead of having to write the core functionality for each new program over and over again, a programmer can just add functionality to an application skeleton provided for them.
In the Magic Pushbutton, the only event handler that does any real work is the one that's called when the user clicks the return button. If the programmer does not deliberately organize the program's code in a better way, then it all accumulates in this one routine, which ends up as a huge, unmanageable blob of code.[2]
Problems with this anti-pattern are:

  • The code behind the Pushbuttons grows unmanageably
  • Changing the user interface (or adding an alternate interface) is difficult
  • Testing the code is difficult

Example

The following is a typical example of a magic pushbutton in Borland Delphi:

procedure TForm1.Button1Click(Sender: TObject);
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := HKey_Current_User;
    if reg.OpenKey('\Software\MyCompany', true) then
    begin
      reg.WriteString('Filename', Edit1.Text);
    end;
  finally
    reg.Free;
  end;
end;

A better way to do this is to refactor the business logic (in this example storing the filename to the registry) into a separate class.

type
  TPreferences = class
  private
    FFilename: String;
    procedure SetFilename(const Value: String);
  public
    property Filename: String read FFilename write SetFilename;
    procedure Load;
    procedure Save;
  end;

and call this class Save method from the Click handler:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Preferences.Save;
end;
 
procedure TForm1.Edit1Change(Sender: TObject);
begin
  Preferences.Filename := Edit1.Text;
end;

References

  1. ^ http://www.scribd.com/doc/99553/AntiPattern-by-Indranil-Nandy-IIT-Kharagpur
  2. ^ Software project secrets: why software projects fail By George Stepanek



Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Magic pushbutton — En computación el antipatrón de diseño magic pushbutton ocurre en el desarrollo de interfaces gráficas, cuando el programador construye primero la interfaz gráfica y luego las llamadas a la lógica de negocio se realizan en los huecos,… …   Wikipedia Español

  • Magic pushbutton — En Computación el antipatrón de diseño magic pushbutton ocurre en el desarrollo de interfaces gráficas, cuando el programador construye primero la interfaz gráfica y luego las llamadas a la lógica del negocio se realizan en los huecos,… …   Enciclopedia Universal

  • Antipatrón de diseño — Saltar a navegación, búsqueda Un antipatrón de diseño es un patrón de diseño que invariablemente conduce a una mala solución para un problema. Al documentarse los antipatrones, además de los patrones de diseño, se dan argumentos a los diseñadores …   Wikipedia Español

  • Anti-pattern — For the book, see AntiPatterns. In software engineering, an anti pattern (or antipattern) is a pattern that may be commonly used but is ineffective and/or counterproductive in practice.[1][2] The term was coined in 1995 by Andrew Koenig,[3]… …   Wikipedia

  • Антипаттерн — Возможно, эта статья содержит оригинальное исследование. Добавьте ссылки на источники, в противном случае она может быть выставлена на удаление. Дополнительные сведения могут быть на странице обсуждения. (25 мая 2011) …   Википедия

  • Business logic — Business logic, or domain logic, is a non technical term generally used to describe the functional algorithms that handle information exchange between a database and a user interface. Contents 1 Scope of business logic 2 Business logic 3 T …   Wikipedia

  • Магическая кнопка — В этом разделе не хватает ссылок на источники информации. Информация должна быть проверяема, иначе она может быть поставлена под сомнение и удалена. Вы можете отредактировать эту статью, добавив ссылки на авторитетные источники. Эта отметка… …   Википедия

  • Electronic component — Various components An electronic component is a basic electronic element and may be available in a discrete form having two or more electrical terminals (or leads). These are intended to be connected together, usually by soldering to a printed… …   Wikipedia

  • PyQt — PyQt …   Википедия

  • Design for Dreaming — (1956) is a musical sponsored film about a woman (played by dancer and choreographer Tad Tadlock; real name Thelma Tadlock ) who dreams about a masked man (dancer and choreographer Marc Breaux) taking her to the 1956 General Motors Motorama at… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”