GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  J2EE  >  Java Patterns

 Print  |  
Question:  java patterns

Answer: Explain java patterns in detail? Any difference between Design patterns and java patterns ? . Explain about advantages of using patterns with an example.


March 03, 2008 15:30:33 #1
 vibha_kant   Member Since: May 2007    Total Comments: 5 

RE: java patterns
 

Let's first understand what a design pattern is. When a problem occurs repetitively in all application across programmers, the solution to that problem is defined as a design pattern. Most of design patterns are language independent strategies to common object oriented problems.
Typically java has three design patterns.

1. Creational Design Pattern

As the name suggests, this pattern is all about instantiated a class and how object is managed across the program to reduce duplicate objects and unnecessary coding which might be caused by using the basic form of object creation. This pattern is again subdivided in to class creational patterns and object creational patterns. Common examples of such patterns are -

a. Factory

Delegates the work of creating the object instance to derived classes of the interface.

b. Abstract Factory

used for creating groups of dependant instances of classes without specifying their concrete classes. It's the centralize decision of what factory to instantiate.

c. Builder

It's used to separate the instantiation of a complex object from its representation so that same construction process can be used for different representations.

d. Singleton

Ensures at every point in the application, there is only and only one instance of that class and also provides a global access point to the same.

e. Prototype

When the typical way of instantiating an object (i.e. "new") is costly. In this pattern new instances are created by copying the prototype of that class.

f. Lazy initiation

Supply when demanded. Instead of already having a created object, create only when it's required.

g. Objects pool

Recycling the objects when they are not in used, rather than releasing them and created new ones when they are required.

2. Structural Design Patterns

These are the patterns used for realizing the relationship between different entities. It defines ways to compose an object to obtain new functionality. Common examples of such patterns are:

a. Adapter/Wrapper

Its used to convert an interface into the one that client expects to see. That is, it adapts the interface to an expected class. It makes incompatible interfaces work together by wrapping them with its own interface.

b. Composite

It's used for defining a whole hierarchy tree structure of objects where each object has the same interface. This allows client to treat a group of different objects in the same way.

c. Bridge

It separates an abstraction from its implementation so that two can vary differently. That is the implemented class can be launched independently and work as its own system.

d. Decorator

Used for adding functionality to the objects at runtime. It's an alternative for sub classing. Sub classing ads functionality to an object at compile time where as decorating adds functionality at the runtime.

e. Facade

Providing an additional layer to existing subsystems to hide complexities. It makes an entry system to the sub systems.

f. Flyweight

When data is shared between numbers of objects to prevent memory consumptions. It's not possible to share all the object state data; therefore it's commonly put in external data structures to populate object's property at runtime (On the Fly).

g. Proxy

Using a mirror substitute of original object if creation of original object is time taking or memory consuming (when that memory consuming operation is not needed for this object to perform). This is often used in as a placeholder typically used in conjunction which flyweight pattern where one instance and multiple proxy are created all pointing to same reference.

     

 

Back To Question