Author Archives: Michael Farouk

Michael Farouk

Decorator Pattern is a structural pattern that allows for the dynamic wrapping of objects in order to modify their existing responsibilities and behaviors and leads to the Open Close Principle (Closed for Modification, Open for Extension) One of the most important software principles of the development process that developers have to consider in change, the Decorator pattern helps us to design code avoiding modification, while still extending that code if needed.

Problem

We want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class at the compile time.

Solving

Description: D:\TIEC Android Program\Books\decorator-design-pattern-example-uml-class-diagram.png

We solved this using the decorator pattern by designing a new decorator class that wraps the original class. This wrapping could be achieved by the following sequence of steps:

  1. Create Subclass from the original "Component" class called "Decorator" Class.
  2. In this subclass - "Decorator" Class - add a Component pointer as a field.
  3. Pass a Component to the Decorator constructor to initialize the Component field pointer.
  4. In the Decorator Class, redirect all "Component" methods to the "Component" field pointer.
  5. In the ConcreteDecorator Class, override any Component method(s) whose behavior needs to be modified.

Summary

Finally, when we want to add some functionality to individual object or change the state of particular object at run time it is not possible; however, we can provide the specific behavior to all the objects of that class at design time by the help of inheritance or using subclass, but Decorator pattern makes it possible to provide individual object of same class a specific behavior or state at run time. This doesn’t affect other object of same Class.

Reference : http://en.wikipedia.org/wiki/Decorator_pattern

Michael Farouk

The main issue every programmer find is the exception handling. Let us together by the means of this article try to see what the best ways available for exception handling are. The idea of the all the programmers like us is to come up with a quality code that  instead of making the problems worst, resolves the same at the earliest.

What is an Exception?

Exception is an error which occurs during the execution of a program.

There are mainly two types of exceptions:

1-Checked exception

2-Unchecked exception

Checked exception is considered at compile time so it greatly reduces the occurrence of unhandled exceptions during runtime. They extend Exception and are intended to be handled at compile time. A checked exception indicates an expected problem which might occur during normal application execution.

Following is the list of checked exceptions:

  • ClassNotFoundException
  • NoSuchMethodException
  • NoSuchFieldException
  • InstantiationException
  • CloneNotSupportedException
  • InterruptedException
  • IllegalAccessException

Unchecked exceptions extend RuntimeException. An unchecked exception indicates an unexpected problem which occurs due to coding bug. These are exceptions introduced by the application developer during coding. These exceptions are not checked during compile time but they are checked during runtime.

Following is the list of unchecked exceptions

  • NullPointerException
  • IndexOutOfBoundsException
  • ArrayIndexOutOfBoundsException
  • ClassCastException
  • IllegalStateException
  • ArithmeticException
  • SecurityException

Description: JavaExceptionHandling.jpg

The Hierarchy of Exception

Handling Exceptions

1-Using try and catch

Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. For example, if you call a method that opens a file but the file cannot be opened, execution of that method will stop, and code that you wrote to deal with this situation will be run. Therefore, we need a way to tell the JVM what code to execute when a certain exception happens. To do this, we use the try and catch keywords. The try is used to define a block of code in which exceptions may occur. This block of code is called a guarded region (which really means "risky code goes here").

Example:

try{

The risky code goes here which is may causes some kind of exceptions. }catch(HandeledException){

Handling of exception goes here

}

2-Using finally

A finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. Even if there is a return statement in the try block. The finally block executes right after the return statement, and before the return executes! This is the right place to close your files, release your network sockets, and perform any other cleanup your code requires. If the try block executes with no exceptions, the finally block is executed immediately after the try block completes. If there was an exception thrown, the finally block executes immediately after the proper catch block completes.

Example:

try{

The risky code goes here which is may causes some kind of exceptions.

}catch(Handeled Exception){

Handling of exception goes here

}

finally{

The code goes Here is always excute after the try or the catch blocks.

// Put code here to release any resource we

// allocated in the try clause.

}

Custom Exception Handling

Custom exception handler is designed to meet the application requirement. The normal exception APIs provided by the java are basic building blocks. Now the developer is expected to use these building blocks to make their own custom exception handler.

To define their own custom exception class, developer needs to extend the Exception class. All custom exceptions are considered to be checked exceptions. The custom exception class is like any other normal class but they provide useful methods and directs customized execution path

Following rules needs to be followed to define custom exception class:

  • All exceptions must be a child of Throwable.
  • Must extend the Exception class for checked exception.
  • For runtime exception, extend the RuntimeException class.

Example:

class MyCustomException extends Exception{

// Write useful methods and execution path

}

Conclusion

 A program can catch exceptions by using a combination of the try, catch, and finally blocks.

  • The try block identifies a block of code in which an exception can occur.
  • The catch block identifies a block of code, known as an exception handler that can handle a particular type of exception.
  • The finally block identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the try block.

The try statement should contain at least one catch block or a finally block and may have multiple catch blocks.

 The class of the exception object indicates the type of exception thrown. The exception object can contain further information about the error, including an error message. With exception chaining, an exception can point to the exception that caused it, which can in turn point to the exception that caused it, and so on.