Imagine you’re a financial institution. You’ve built an application that processes financial transactions, and there are a number of flags that need to be set as constants to determine application behavior.

You might choose to write code like this:

  public static final boolean CreateNewReferenceFolderAndFiles = true;

But then your boss would say, “Bro, do you even object-orient?” before firing you, because that isn’t enterprise-y.

Skyfyre” sent us this excerpt, from a four-million line application, which is definitely very enterprise-y.

First, you need to define several classes, and since this is Java, each of them must be in its own file:

  public abstract class ArchiveCreateNewReferenceFolderAndFiles

  public class ArchiveCreateNewReferenceFolderAndFilesTrue extends ArchiveCreateNewReferenceFolderAndFilesAbstract

  public class ArchiveCreateNewReferenceFolderAndFilesFalse extends ArchiveCreateNewReferenceFolderAndFilesAbstract

Then, you’ve got to create your actual constants:

public static final ArchiveCreateNewReferenceFolderAndFilesAbstract ARCHIVE_CreateNewReferenceFolderAndFiles_TRUE = new ArchiveCreateNewReferenceFolderAndFilesTrue();
public static final ArchiveCreateNewReferenceFolderAndFilesAbstract ARCHIVE_CreateNewReferenceFolderAndFiles_FALSE = new ArchiveCreateNewReferenceFolderAndFilesFalse();
/// later…
public static final ArchiveCreateNewReferenceFolderAndFilesAbstract archiveCreateNewReferenceFile = ARCHIVE_CreateNewReferenceFolderAndFiles_TRUE;

Now, you’re ready to write some real conditionals based on flags:

  if(archiveCreateNewReferenceFile instanceof ArchiveCreateNewReferenceFolderAndFilesTrue) {
    //do stuff…

It’s simplicity itself, and just think about how maintainable it is. When you finally need to support FileNotFound, you simply need to inherit from the ArchiveCreateNewReferenceFolderAndFiles class to produce ArchiveCreateNewReferenceFolderAndFilesFileNotFound. Think of the extensibility! Think of the readability!

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!