Java patterns

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

Showing Answers 1 - 3 of 3 Answers


  • Mar 10th, 2008

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 dependent 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. Fa?ade--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.

It can't be explained in one or two lines. You gotta read nearly 23 patterns in OO. And, write yourself some application. You have to have application knowledge of patterns than theoritical knowledge. I'll recommend you to go through the book written by GangOfFour

  Was this answer useful?  Yes

In java total 23 design patterns are there those 23 is subdivided into three types as given below

1. Creational design patterns
2. Structural design patterns
3. Behavioural design patterns

1.Creational design pattern: The main intension of this design pattern is how to create object in different ways .As we know that objects are created by using new operator it is true.But in creational design pattern he will show creation of objects by following some design pattern.

2.Structural design pattern: It is some what difficult to understand but i will try with my level best.This design pattern is going to concentrate on classes and objects i.e how to use classes and objects to make large structures.In structural design pattern can be subdivided into two design patterns like class design pattern and object design pattern.Class design pattern is used to make inheritance and object design pattern say's how objects are associated to make large structures.

3. Behavioural design pattern: It is easy to understand .The interactions between the objects should be such that they are talking each other.It is also called as loose coupled.That is consider 3-tier architecture like MVC here the objects are talking each other but the are loosely coupled and also to avoid dependencies i.e one is depending on other objects like that it is done by xml files in MVC architectures and also to avoid hardcoding.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions