Monday, December 14, 2009

Design Patterns .used in Java API

Following is the list of Design Patterns used in Java API

Creational Pattern

  • Factory Method - getInstance() method in java.util.Calendar
  • Singleton - java.lang.System and java.sql.DriverManager although they are not implemented using the approach recommended in the GoF book but with static methods.
  • Prototype pattern - clone() method defined in class Object and the use of java.lang.Cloneable interface to grant permission for cloning.

Structural Pattern

  • The Proxy pattern - is used extensively in the implementation of Java's Remote Method Invocation (RMI) and Interface Definition Language (IDL) features.
  • Composite pattern - The structure of Component and Container classes in java.awt provide a good example of the same
  • Bridge pattern - can be found in the separation of the components in java.awt (e.g., Button and List), and their counterparts in java.awt.peer.
  • Adapter pattern - is used extensively by the adapter classes in java.awt.event. and wrapper classes.
  • Decorator - The java.io package provides, among other things, a set of classes for reading input streams. Known as readers, each of those classes has a name that follows the pattern: xxxReader.

    Readers provide specialized functionality; for example, one reader reads from a file, another tracks line numbers, and yet another pushes characters back on the input stream. In all, eight different readers exist to read input streams.

    It's often necessary to combine the capabilities offered by java.io readers; for example, you might want to read from a file, keep track of line numbers, and push certain characters back on the input stream, all at the same time. The java.io package's designers could have used inheritance to provide a wide array of such reader combinations; for example, a FileReader class could have a LineNumberFileReader subclass, which could in turn have a PushBackLineNumberFileReader subclass. But using inheritance to compose the most widely used combinations of reader functionality would result in a veritable explosion of classes. So how did the java.io package's designers create a design that allows you to combine reader functionality in any way you desire with only 10 reader classes? As you might guess, they used the Decorator pattern.

Behavioural Pattern

  • Observer pattern - The Java 1.1 event model. In addition, the interface java.util.Observable and the class java.util.Observer provide support for this pattern.
  • Command pattern - The Java Swing classes implement this pattern by providing an Action interface and an AbstractAction class.
  • Strategy pattern - Java's Abstract Window Toolkit (AWT) provides implementations of common user interface components such as buttons, menus, scrollbars, and lists. Those components are laid out -- meaning sized and positioned -- inside containers such as panels, dialog boxes, and windows. But AWT containers do not perform the actual layout; instead, those containers delegate layout functionality to another object known as a layout manager. That delegation is an example of the Strategy design pattern.

Labels:

Thursday, April 16, 2009

eclipse short cut keys

yet to come.......

Labels:

Java Naming Convention

Monday, March 30, 2009

Concatenate arrays to a List

This is a simple code snippet which explains, how to concatenate two arrays
into a list

private List getChildren(ContextNode parentItem) {
List siblings = null;
siblings = new ArrayList();
Collections.addAll(siblings, ((ContextNode)parentItem).getChildNodes());
Collections.addAll(siblings, ((ContextNode)parentItem).getAttributes());
return siblings;
}

Labels:

Convert HashMap to an array
Map aMap = new HashMap (  ) ; 
aList = new ArrayList ( aMap.values ( ) ) ;
aMap.clear ( ) ;

Labels:

Sunday, November 30, 2008

Singleton Pattern

Singleton pattern governs the instantiation process. It is a type of creational pattern. Singleton proposes that at any time there can only be one instance of a singleton (object) . Implementation has two stpes:
Step 1: Provide a default Private constructor
Step 2: Create a Method for getting the reference to the Singleton Object
Singleton Implementation


Sample code that uses singleton object


Labels:

Saturday, November 29, 2008

Design Patterns

Design patterns are commonly defined as time-tested solutions to recurring design problems. 

Initially Software  Design Patterns were published by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides in 1995.In their book(Design Patterns: Elements of Reusable Object-Oriented Software), they present 23 design patterns organized into three categories:

·        Creational

·        Structural

·        Behavioral design patterns

 Now the design pattern community has grown in size and hence lots of patterns are available

To conclude Design patterns have two major benefits.

1.      They provide the way to solve issues related to software development using a proven solution. The solution facilitates the development of highly cohesive modules with minimal coupling.

2.      Design patterns make communication between designers more efficient.