Skip to main content

Java Keywords (Part XXIV): native

Java keyword list abstract continue for new switch assert default goto * package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const * float native super while Keyword marked with an asterisk (*) are keywords that, although valid, are not used by programmers. This is the last chapter of the Java Keyword series. This is probably the keyword I have used the least. In my 20 year career as a software developer, I have used this keyword once, and that was to make some addition to legacy code. The keyword native is a method modifier . Basically, it is a keyword that can only be applied to methods. According to the Java Language Specification (JLS), A method that is native is implemented i...

Applying the Singleton Design Pattern to Real-World Scenarios

There are many who believe that the Singleton pattern is an anti-pattern. By the title you probably assumed I am not one of those people, and you will be right. I believe the term "anti-pattern" is being used for cases where a pattern is not being used for what it was originally designed for or just implemented incorrectly. I am going to try to keep this short. But I need to explain what the Singleton pattern is before I provide a real-life example of its applicability. The Singleton pattern is one of the original Gang of Four Design Patterns. The Singleton pattern falls under the Creational Pattern category. This means that the purpose of the Singleton pattern is to create objects. The creational purpose of the pattern is to ensure that one and only one object is instantiated in the life of the application. By definition, an anti-pattern is a pattern that is usually ineffective and highly counterproductive. So, why do classify Singleton as an anti-pattern? I found an interesting article by Michael Safyan where he expresses his opinion on the subject. However, if you examine his article closely, his conclusion are more based on the misuse of the pattern rather than in inherent qualities with the pattern itself. In my opinion, if a tool is not used for its intended purpose, that doesn't make the tool bad. But, I digress. Perhaps I should write another article about real anti-patterns. The Singleton Pattern's purpose is to ensure that one and only one object is created during an application's life cycle. Now that we understand its purpose, we must understand how and when to apply it. The "when" will address the use-cases for this pattern. For this blog, I will provide just o. The "how" will address the implementation techniques to create a Singleton.

When to use Singleton

Contrary to what some people believe, there are many valid cases for using Singletons. One of such cases is to create states on a Finite-State-Machine (FSM). In software, FSMs are implemented following the State Design Pattern. I will expand on this topic by writing about Real-Life applicability of the State pattern. For now, I simply wish to focus on using Singleton pattern to create a State object. Consider the daily cycle of a human being. In a single day, human beings engage in activities we could call "states". In the most basic sense, in a single day, humans are either asleep or awake. If you were to model this behavior in software, how would you model it? Ask yourself this simple question: Is the "asleep state" of a given person today different from the "asleep state" of the same person tomorrow? If you think about it, the state itself is the same. So, wouldn't it make sense to model this state as a Singleton? The answer is yes. In fact, when you examine the State design pattern, it is recommended that the Singleton Pattern is used to create state objects.

How to Create a Singleton

In Java, the easiest (and recommended) way to implement a Singleton is by using the Java
enum.

public enum MyEnum
{
  INSTANCE;

  private MyEnum()
  {
    // DO SOMETHING?
  }
}

Part II of this blog will be coming out in a few days. In that blog, I will be writing about applicability of the State Pattern. I will make use of the Singleton Pattern create the State objects in a FSM that I will use to implement a simple Wizard. Stay tuned!

Comments

Popular posts from this blog

Implementing Interfaces with Java Records

If you have not read my article on Java records and do not know about this topic, please read my blog titled " Customizing Java Records " first and then come back to this one. Now that you know how to customize Java records, implementing an interface using Java records should be very easy to understand. If you don't know about interfaces in Java, you can read more on my article about interfaces. The recipe for implementing an interface is simply an expansion of what you learned in my previous blog on how to customize a Java record. Following our Rectangle example, let's create an interface with the same two methods we used before. public interface Shape { double area(); double perimeter(); } Now, let's further customize the previous example by doing two things: Add implements Shape at the end of the record declaration (after the record constructor), and Add @Override to the existing methods to ensure these methods com...

Customizing Java Records

If you have not read my article on Java records and do not know about this topic, please read my blog titled " Java Keywords Addendum: The Java Record " first and then come back to this one. What is a customization of a record? A customization of a record is simply the addition of code inside the body of the class. Before proceeding further, let's recap important aspects of a Java Record: Java records are immutable Because of item 1 above, you cannot add new fields unless defined in the record constructor Java records already override: Object#equals(Object) and Object#hashCode() , and then override Object#toString() You could redefine overridden methods as part of your customization if you would like. For example, if you want a fancier implementation of the Object#toString() method, you could do so. Let's look at our first customization example. Using the example from my previous blog, public record Student(...

Object-Oriented Programming Basics: What is in a Class?

EDITORIAL NOTE : This article was published briefly back in 2016 and quickly set back to draft because I wasn't happy with its contents. It is a shame that it was taking me three years to revisit this topic and work on a new and improved version. At least, I'm hoping it will be to the liking you the reader. Keep in mind that the opening paragraph will still read as if I just wrote it for my (former) students at Texas Wesleyan. I started working on lecture on the topic of Object-Oriented (OO) Programming by gathering some material, old and new, when I realized this might be good and simple post for my second attempt at blogging. To be completely honest, in the 8 hours I spent collecting information and preparing material for this week's lecture, I realized I still made some of the mistakes I am about to blog about. I am actually hoping I can write a series of postings regarding Object-Oriented Programming (OOP). But to do so, I must start from the very beginning. ...