GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Scruff the Cat       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II More Occam's Razor


A Super Constructor design pattern Free Stuff

Abstract

This article presents a Super Constructor design pattern. It is equally suitable for C++ and Java.

The design pattern

This design pattern is applicable in cases where the constructor could return a null value or where you want to have a virtual constructor. To do this, the constructor needs to be renamed to a static method called for arguments sake: ctor. Here is the design pattern for null return values in action in the Java language:


class Foo
{
   int aProperty;

   private Foo(/* parameters */)
   {
      /* your code goes here */
   }

   public static Foo ctor(/* parameters */)
   {
      Foo returnValue = new Foo(/* arguments */);
      /* set properties here */
      returnValue.aProperty = 123;
      /* decide whether or not to return null here */
      if (/* code omitted */)
      {
         return returnValue;
      }
      else
      {
         return null;
      }
   }
}

The name ctor is used to remind the client that the method serves in the role of a constructor. The actual constructor is private so that it can only invoked indirectly by the ctor function. Here is the design pattern in action when you want to have a virtual constructor:


class Root
{
   private Root(/* parameters */)
   {
      /* your code goes here */
   }

   public static Root ctor(/* parameters */)
   {
      if (/* code omitted */)
      {
         return new Root(/* arguments */);
      }
      else if (/* code omitted */)
      {
         return new SubClass(/* arguments */);
      }
      else
      {
         return null;
      }
   }

   /* your code goes here */
}

class SubClass extends Root
{
   public SubClass(/* parameters */)
   {
      /* your code goes here */
   }

   /* your code goes here */
}

Here is an alternative implementation of the Super Constructor design pattern:


class Root
{
   public Root(/* parameters */)
   {
      /* your code goes here */
   }
}

class SubClass extends Root
{
   public static Root ctor(/* parameters */)
   {
      if (/* code omitted */)
      {
         return new Root(/* arguments */);
      }
      else if (/* code omitted */)
      {
         return new SubClass(/* arguments */);
      }
      else
      {
         return null;
      }
   }
}


Back to Research Projects
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II | More Occam's Razor
Last modified: Sun Sep 25 16:12:22 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to