Posts

Applying the State Design Pattern to Real-World Scenarios

In my previous article, I wrote about the Singleton Design Pattern and a possible real-world application for it. In this article, I will do the same for the State Pattern. In addition, I will use the example from my previous post to implement the states in my state machine. But I cannot start showing examples unless I help you understand first the basic concept of this topic. The first question I must answer is what is the State Design Pattern. What is the State Design Pattern? The State Design Pattern is one of the original Design Patterns proposed by the Gang of Four (GoF). The pattern falls under the Behavioral classification or category. This means that the purpose of this design is to come up with a scheme to control the behavior of objects. Specifically, it is used to model behavior of objects that must behave like a state machine. A state machine is a device that can be in one of a set number of stable conditions depending on its previous condition and on the values

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 fou

Encapsulation

I believe this is the most important aspect of Object-Oriented Design (OOD) and Programming (OOP). I also believe this is the most disregarded. It is disregarded because it is not fully understood. It is my opinion that this aspect of OOP is misunderstood because beginner programmers focus in learning WHAT encapsulation is, and not why it is important or needed. This article aims to explain why encapsulation is important and how to achieve true encapsulation. Although anyone could look up its definition in a dictionary, I would like to start by defining the word 'encapsulation.' Encapsulation simply means to enclose something in. To completely cover or hide something especially so that it will not touch (or come in contact with) anything else. In the real world, there are many reasons to encapsulate. For example, certain medication come in the form of capsules. The real value of medicinal capsules in in the medicine that it encloses. To make a long story short, med

Exception Handling: File CRUD Operations Example

Image
Introduction to Exception Handling This is a supplement to my previous blog Creating Custom Exception Classes in Java . I recommend you read that one first and then read this one. Java provides many Exception (and Error) Handling classes. Both of these categories implement the Throwable interface. Although it is not imperative that you develop your own custom exception classes, it is often a good idea to do so; particularly when dealing with large systems. The main reason to create custom exception classes is to quickly identify which custom class is being affected or causing these custom exceptions. For example, referencing a null object and trying to use it will result in a NullPointerException . However, in your code, there could be literally hundreds or even thousands of statements which could result in such exception. Designing your Custom Exception Class Suppose that you are designing a class that will serve as a message receiver. For simplicity,

Creating Custom Exception Classes in Java

Image
Introduction My first piece of advice: Search the web. There is a ton of information that is available to you on the Internet. However, beware where you go for information. There are many places with bad information as well. For this topic, the safest source of information is the Oracle The Java Exceptions Tutorials . Starting with the basics, the first thing to do is determine whether or not you need the custom exception class to begin with. We are going to assume we need to handle an exception that it is not clearly represented already in Java. To create a custom exception class, we must use a name that it is meaningful. Typically, the exception class is named after a process or a class name that will throw the exception. For instance, if I create a MessageReceiver class that could result in an exception, I might consider naming my exception class MessageReceiverException. The UML diagram below depicts this relationship between the class and the custom

How to create Javadoc comments

Image
This document is a summary of the information found on the Oracle website: http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html My goal is to give you a summarized version of the following topics: ·          What is Javadoc and why is it needed? ·          How to document your code using Javadoc ·          How to generate Javadoc documentation ·          How to publish Javadoc What is Javadoc and why is it needed? In simple terms, Javadoc is a tool for documenting your code in HTML format. Because this documentation is in HTML, it can be easily uploaded to a web server and made available online for the whole world to see. An example of this is the Java API ( https://docs.oracle.com/javase/8/docs/api/ ). For many developers, one of the selling points of Java over other languages like C++ was in fact Javadoc. Why? If you consider one of the most common libraries in C++, stdio, most of the documentation found online is

Aggregation and Composition

Image
When determining the relationship between object at design time, it is easy to succumb to the habit of using inheritance; simply because it is easy.  In a language such as C++, it is even easier to do so because the language supports multiple inheritance. Figure  1 : the java.util. List  Interface Assume that Java does not have a Stack (java.util.Stack) class and you need to create a custom stack (call it MyStack).  You could do this in three different ways, but for the purposes of this paper I will discard the creation of the Stack class as shown in the image above.  Therefore, assume that there are only two ways.  The first way is to extend the ArrayList class.  The second way is to include an ArrayList object as a data member.  The first solution uses inheritance to solve the problem.  Remember, inheritance establishes an “is-a” relationship between objects.  Therefore, the relationship between MyStack and ArrayList is incorrect since a stack is not