Advertisement
RandomClear

How to subclass without changing class name

Nov 25th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.39 KB | None | 0 0
  1. // Suppose that you want to alter original behavior/class, but don't want to create a new class (with new name), because this will require you to alter options as well.
  2. // Instead, you want just to set up options at design time and provide altered behavior at run-time.
  3. // For example, when you post bug to Mantis, a issue title is composed.
  4. // EurekaLog does not provide way to alter it, because it's used to identify tickets.
  5. // You may want to alter it (say, by appending more info, so title becomes more descriptive).
  6. // Note: please note that this is not recommended way to work with tickets in Mantis.
  7. // We suggest you to create custom fields for your project (this is done in Mantis configuration).
  8. // Fill custom fields with information (this is done in EurekaLog configuration).
  9. // And show some of these fields in list of tickets (this is done in Mantis configuration).
  10. // That way you will archive the desired effect (to see more info about each ticket in list),
  11. // but also additionally gain some benefits: you will be able to sort/filter by custom fields,
  12. // you will not break default EurekaLog tickets identification.
  13. // Anyway, here is how you can do that:
  14.  
  15. uses
  16.   EConsts,
  17.   ESend,
  18.   ESendAPIMantis;
  19.  
  20. type
  21.   // Trick: use the same class name as original class
  22.   // You'll have to append unit name to class ident to avoid compiler confusion
  23.   // With this trick you don't have to change options.
  24.   // You can just set options at design-time.
  25.   // The important part here is to register your class first,
  26.   // which is archived by using registering function with "First" suffix (see below).
  27.   TELTrackerMantisSender = class(ESendAPIMantis.TELTrackerMantisSender)
  28.   protected
  29.     function ComposeTitle: String; override;
  30.   end;
  31.  
  32. // This is a default implementation of the method,
  33. // you can replace it with arbitrary code
  34. function TELTrackerMantisSender.ComposeTitle: String;
  35. begin
  36.   if BugAppVersion <> '' then
  37.     Result := Format('%s (Bug %s; v%s)', [BugType, BugID, BugAppVersion])  
  38.   else
  39.     Result := Format('%s (Bug %s)', [BugType, BugID]);
  40.   Log(Format('Title = ''%s''', [Result]));
  41. end;
  42.  
  43. initialization
  44.  
  45.   // Register send class to be the first in the list.
  46.   // Default class (by EurekaLog) will be listed later.
  47.   // Any search for class by name will find our class, because it's listed first
  48.   RegisterSenderFirst(TELTrackerMantisSender);
  49.  
  50. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement