RSS Feed for JavaCategory: Java

Java: Create Immutable Object using Builder Pattern »

To create immutable Java object, we should use the Builder Pattern, as described in Effective Java. This is the pattern I normally used now for immutable class. 1: public class Person { 2: private final String name; 3: private final int age; 4: private final String email; 5: private final String mobileNo; 6:  7:  8: [...]

Design of a Java Application Framework Part 2 – DAO Layer using Hibernate »

This is the sequel of my previous post on the AppFuse source code, a good MVC framework. Thanks to Matt Raible and the team for this wonderful framework. By reading the code, I can understand the framework better in order to use it correctly. For AppFuse, for the data access layer you can use Hibernate, [...]

Design of a Java Application Framework Part 1 – Model Layer »

I have been using AppFuse in several projects. It is a excellent framework which implemented a proper MVC architecture. In order to better understand the framework, I spent sometime browsing through the source code. In Part I of this articles I am going to show you the model layer. For all the model classes in [...]

Singleton is a bad design pattern »

I am not totally against Singleton design pattern. However, based on experience, normally if there is Singleton design pattern in your application, you should carefully study your code. Usually there is always a better way of doing it other than using Singleton design pattern. Consider the following code snippet I encountered for one of the [...]

Writing Easily Maintainable Code ? »

Download Source Code This is the design pattern I came across while browsing through my RSS feed reader. Not very confirmed now from where I read about this. It is quite hard to apply this to the data access layer since I use ORM tools but I am not sure how this can be applied [...]

Design Pattern: Design a Simple Workflow using Chain of Responsibility Pattern »

Download Source Code I was trying Apache Commons Chain when I was trying to design a simple workflow system for my back-end application using the Chain of Responsibility design pattern. Chain of Responsibility is a popular technique for organizing the execution of complex processing flows. It is not difficult if you want to write it [...]

Design Pattern in Java 101 – Command Pattern (Behavioral Pattern) »

Download Sample Code A Command pattern is an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. It allows you to encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests. Here is a rewrite of the C# example provided [...]

Design Pattern in Java 101 – Lazy Load Part II »

Download Sample Code This is Part II of the previously described Lazy Load pattern. value holder A value holder is used as a generic Lazy Load. It is an object that wraps some other object. To get the underlying object you ask the value holder for the value, and the first access will pull the [...]

Design Pattern in Java 101 – Lazy Load Part I »

Download Sample Code Lazy Load is one of the object relational behavioral patterns. With lazy load, an object does not contain all of the data you need but knows how to get it when it is needed. This pattern is commonly found in most of the OR mappers, e.g. Hibernate. E.g. when you load the [...]

Design Pattern in Java 101 – Builder Pattern (Creational Pattern) »

Download Source Code Builder pattern is used to construct a complex object from simple objects step by step. E.g., I have a Message class public class Message { private String sender; private String recipient; private String header; private String body; public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; [...]

Design Pattern in Java 101 – Visitor Pattern (Behavioral Pattern) »

Download Source Code NOTE: This is written for me to recap and relearn what I learnt before…. This article is written in order for me to recap what I have learnt and used before, though I know there are already many sites which talked about all these. Visitor Pattern defines a new operation to deal [...]

Java Fluent Interface – Return Instance from Set Method »

Normally we create a class with the constructor, set/get methods as below public class Person { private String name; private Integer age; private String email; public Person() { } public Person(String name, Integer age, String email) { this.name = name; this.age = age; this.email = email; } public String getName() { return name; } public [...]