Posts

Showing posts with the label OOP

Java Keywords (Part XVI): The many uses of the super keyword

We are up to 40 keywords covered in previous articles! That's 83% keywords covered. We have only 8 keywords to cover and I will be covering 1 of those in this article. We are almost done with all the basic keywords. This article will illustrate the use of the keyword super . I suggest you start with Java Keywords (Part I) before proceeding further, if you have not read any of the previous articles in the Java Keyword series. Also, go back and read the one about Data Types. All of these articles are from September 2018. That should help you find them quickly. You can also use the "search" option at the top of this page. The series was written with natural progression in mind. Therefore, some of the keywords already covered may be used in code examples illustrated here. Java keyword list abstract continue for new switch assert default goto * package synchronized boolean do if private this break double implements protected throw

Java Keywords (Part XV): The many uses of the this keyword

We are up to 39 keywords covered in previous articles! That's 81% keywords covered. We have only 9 keywords to cover and I will be covering 1 of those in this article. We are almost done with all the basic keywords. This article will illustrate the use of the keyword this . I suggest you start with Java Keywords (Part I) before proceeding further, if you have not read any of the previous articles in the Java Keyword series. Also, go back and read the one about Data Types. All of these articles are from September 2018. That should help you find them quickly. You can also use the "search" option at the top of this page. The series was written with natural progression in mind. Therefore, some of the keywords already covered may be used in code examples illustrated here. Java keyword list abstract continue for new switch assert default goto * package synchronized boolean do if private this break double implements protected throw

Java Keywords (Part XIV): Using the instanceof Operator

We are up to 38 keywords covered in previous articles! That's 79% keywords covered. We have only 10 keywords to cover and I will be covering 1 of those in this article. We are almost done with all the basic keywords. This article will illustrate the use of the keyword instanceof . I suggest you start with Java Keywords (Part I) before proceeding further, if you have not read any of the previous articles in the Java Keyword series. Also, go back and read the one about Data Types. All of these articles are from September 2018. That should help you find them quickly. You can also use the "search" option at the top of this page. The series was written with natural progression in mind. Therefore, some of the keywords already covered may be used in code examples illustrated here. Java keyword list abstract continue for new switch assert default goto * package synchronized boolean do if private this break double implements protected

Application of the Builder Pattern

Image

Using Builder Pattern with Class Hierarchies

Image

Static Factory Method

Image

The Builder Pattern (using immutability)

Image

The Beauty of the Null Object Pattern

Image
Before we start... Let me start by saying this: NEVER dismiss an idea without giving it any consideration. That was the mistake I made when I first encountered an article about this design pattern many years ago. I was thrown off by the name and without even attempting to find out what this design pattern was all about, I moved on to other things. That was a huge mistake and I am not afraid to admit it. I urge you not to make that mistake. And if you have already, never make that mistake again. Trust me, you will thank me later. For now, let me tell you my story and attempt to explain when to use it along with a simple example on how to implement this pattern. The Null Object Pattern By definition, the main purpose of this pattern is to have an Object-Oriented alternative to the absence of objects by creating objects that display "do nothing" behavior. That sounds like a mouthful. In simplest terms, it is meant to provide a mechanism to replace null references w

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.

Using a State-Machine to Control a Wizard

On previous articles, I wrote about real-world usages for the State and Singleton design patterns. I also wrote about using these two patterns together to create a state-machine. Now, I am going use that state-machine to control a Wizard. I am going to use Java Swing to create the User Interface classes. To create my interface using Java Swing, I am going to create three panels for contents, one panel to provide the navigation controls, and a frame to hold it all together. It will be a simple example, but one you could easily expand should you have the need to create your own wizard. The Frame The frame is the application window. I will not go to much into details about the Java JFrame class, as I assume you have some basic knowledge of Java and Java Swing. If you don't much about Java Swing, I suggest you visit this Oracle tutorial on how to create frames . Main.java public class Main extends JFrame { private CardsPanel cards = new CardsPanel(); private Navi

Combining State and Singleton Patterns to Create a State-Machine

Image
In my previous two posts, I discussed real-world applications for the Singleton and State design patterns. In this article, I am going to illustrate how to combine both of these patterns to create a simple wizard. Simple State Design Pattern Implementation In a typical implementation of the design pattern, State is either an interface or abstract class, with each state of the state machine being Singleton classes. My example is going to be slightly different. I will implement the state machine using a state interface and Java enums to implement the Singleton. Using an enum is the recommended way to implement a Singleton in Java. First, let's come up with a simple state interface State.java public interface State { void goNext(Context input); void goPrevious(Context input); } Now that we have an interface defined, we can derive as many states as we need. For this example, three states should be sufficient to demonstrate the wizard's functionality. Jav

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