J2EE Design patterns are divided into 3 types
1. Presentation Tier
2. business Tier
3. Integration Tier
Presentation Tier Design Patterns
1. Front Controller
2. Service to workers
3. Dispatcher view
4. Composite View
5. View Helper
6. Intercepting Filter
7. Application Controller
8. Context Object pattern
Business Tier Design Patterns
1. Business Delegate
2. Service Locator
3. Session Façade
4. Application Service
5. Business Object
6. Message Façade
7. Transfer Object(Value Object)
8. Transfer Object Assembler
9. Value List Handler
10. Composite Entity
>Integration Tier Design Patterns
1. Service Activator pattern
2. Data Access Object (DAO)
3. Domain Store
Tuesday, September 21, 2010
Design Patterns
What is a design pattern
a practical, proven solution to a recurring design problem
Design patterns are divided into two types
GOF Design Patterns
Core J2EE patterns
GOF Design patterns:
Gang of four design patterns are divided into 3 trypes
1. Creational
2. Structural
3. Behavioral
Creational patterns are ones that create objects for you, rather than having you instantiate objects directly
Structural patterns help you compose groups of objects into larger structures
Behavioral patterns help you define the communication between objects in your system and how the flow is controlled in a complex program.
Creational Patterns:
Abstract Factory
Builder
Factory
Prototype
Singleton
Structural patterns:
adapter
bridge
composite
decorator
facade
flyweight
proxy
Behavioral patterns:
chain of responsibility
command
interpreter
iterator
mediator
memento
observer
state
strategy
template method
Visitor
Creational Patterns Types
Factory Method : define an interface for creating an object, but let sub-class decide which class to instantiate. This lets a class to defer instantiation to subclasses.
Builder : separate the construction of a complex object from its representation so that the same construction process can create diff. representations.
Abstract Factory : provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Prototype : specify the kinds of objects to create using a prototypical instance and create new objects by copying this prototype.
Singleton : Ensure a class only has one instance, and provide a global point of access to it.
Structural Patterns Types:
Adapter : convert the interface of a class into another interface clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces.
The Adapter pattern is used so that two unrelated interfaces can work together. The joining between them is called an Adapter
Bridge : Decouple an abstraction from its implementation so that the two can vary independently.
Composite : compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly
Decorator : Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub classing for extending functionality.
Façade : Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher level interface that makes the subsystem easier to use
Flyweight : use sharing to support large numbers of fine-grained objects efficiently.
Proxy : which provides a simple place-holder class for a more complex class which is expensive to instantiate.
Behavioral Patterns Types:
Chain of responsibility : The responsibility of handling the request data is given to any of the members of the “chain”. If the first link of the chain cannot handle the responsibility, it passes the request data to the next level in the chain, i.e. to the next link. (a chain of classes that process request )
Command : The client invokes a particular module using a command. The client passes a request, this request gets propagated as a command. The command request maps to particular modules. According to the command, a module is invoked. (request encapsulated in an object )
Interpreter : It converts the code written in English to a byte code format so as to make possible for all the operating systems to understand it.
Iterator : which allows you to navigate through a collection of data using a common interface without knowing about the underlying implementation.
Mediator : It promotes loose-coupling of classes such that only one class (Mediator) has the knowledge of all the classes, rest of the classes have their responsibilities and they only interact with the Mediator.
Memento : without violating encapsulation, capture and externalize an objects' internal state so that the object can be restored to this state later. (store and restore object's internal state )
Observer : define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
State : allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
(provides a memory for a class’s instance variables.)
Strategy : Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
(group of classes that represent set of possible behaviors )
Template Method : define the skeleton of an algorithm in an operation, deferring some steps to subclasses. provides a method that allows sub-classes to override parts of method without re-writing it. (provides an abstract definition of an algorithm. )
Visitor : Represent an operation to be performed on the elements of an object structure. Visitor to add new function to a set of classes without having to modify them.
a practical, proven solution to a recurring design problem
Design patterns are divided into two types
GOF Design Patterns
Core J2EE patterns
GOF Design patterns:
Gang of four design patterns are divided into 3 trypes
1. Creational
2. Structural
3. Behavioral
Creational patterns are ones that create objects for you, rather than having you instantiate objects directly
Structural patterns help you compose groups of objects into larger structures
Behavioral patterns help you define the communication between objects in your system and how the flow is controlled in a complex program.
Creational Patterns:
Abstract Factory
Builder
Factory
Prototype
Singleton
Structural patterns:
adapter
bridge
composite
decorator
facade
flyweight
proxy
Behavioral patterns:
chain of responsibility
command
interpreter
iterator
mediator
memento
observer
state
strategy
template method
Visitor
Creational Patterns Types
Factory Method : define an interface for creating an object, but let sub-class decide which class to instantiate. This lets a class to defer instantiation to subclasses.
Builder : separate the construction of a complex object from its representation so that the same construction process can create diff. representations.
Abstract Factory : provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Prototype : specify the kinds of objects to create using a prototypical instance and create new objects by copying this prototype.
Singleton : Ensure a class only has one instance, and provide a global point of access to it.
Structural Patterns Types:
Adapter : convert the interface of a class into another interface clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces.
The Adapter pattern is used so that two unrelated interfaces can work together. The joining between them is called an Adapter
Bridge : Decouple an abstraction from its implementation so that the two can vary independently.
Composite : compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly
Decorator : Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub classing for extending functionality.
Façade : Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher level interface that makes the subsystem easier to use
Flyweight : use sharing to support large numbers of fine-grained objects efficiently.
Proxy : which provides a simple place-holder class for a more complex class which is expensive to instantiate.
Behavioral Patterns Types:
Chain of responsibility : The responsibility of handling the request data is given to any of the members of the “chain”. If the first link of the chain cannot handle the responsibility, it passes the request data to the next level in the chain, i.e. to the next link. (a chain of classes that process request )
Command : The client invokes a particular module using a command. The client passes a request, this request gets propagated as a command. The command request maps to particular modules. According to the command, a module is invoked. (request encapsulated in an object )
Interpreter : It converts the code written in English to a byte code format so as to make possible for all the operating systems to understand it.
Iterator : which allows you to navigate through a collection of data using a common interface without knowing about the underlying implementation.
Mediator : It promotes loose-coupling of classes such that only one class (Mediator) has the knowledge of all the classes, rest of the classes have their responsibilities and they only interact with the Mediator.
Memento : without violating encapsulation, capture and externalize an objects' internal state so that the object can be restored to this state later. (store and restore object's internal state )
Observer : define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
State : allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
(provides a memory for a class’s instance variables.)
Strategy : Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
(group of classes that represent set of possible behaviors )
Template Method : define the skeleton of an algorithm in an operation, deferring some steps to subclasses. provides a method that allows sub-classes to override parts of method without re-writing it. (provides an abstract definition of an algorithm. )
Visitor : Represent an operation to be performed on the elements of an object structure. Visitor to add new function to a set of classes without having to modify them.
How to remove HTML Tags from Java String
1. You can try like this to remove html tags from string
String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");
System.out.println(noHTMLString);
2. Another complicated example
public static String removeHTML(String htmlString)
{
// Remove HTML tag from java String
String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");
// Remove Carriage return from java String
noHTMLString = noHTMLString.replaceAll("\r", "
");
// Remove New line from java string and replace html break
noHTMLString = noHTMLString.replaceAll("\n", " ");
noHTMLString = noHTMLString.replaceAll("\'", "'");
noHTMLString = noHTMLString.replaceAll("\"", """);
return noHTMLString;
}
String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");
System.out.println(noHTMLString);
2. Another complicated example
public static String removeHTML(String htmlString)
{
// Remove HTML tag from java String
String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");
// Remove Carriage return from java String
noHTMLString = noHTMLString.replaceAll("\r", "
");
// Remove New line from java string and replace html break
noHTMLString = noHTMLString.replaceAll("\n", " ");
noHTMLString = noHTMLString.replaceAll("\'", "'");
noHTMLString = noHTMLString.replaceAll("\"", """);
return noHTMLString;
}
Friday, September 17, 2010
Load the xml file from the class which is in the same Jar file
Lets assume you have a class called samle and xml file called sample.xml file. Both class and xml files are in the jar file.
If you want to load the sample.xml from the sample.class file, you need to use the following code.
this.getClass().getClassLoader().getResource("sample.xml")
Sample examples:
public class Sample {
public void callXmlMapping() {
URL url = this.getClass().getClassLoader().getResource("sample.xml");
}
}
You can create jar file with these both files and add this jar file in class path of your webapplication.
If you want to load the sample.xml from the sample.class file, you need to use the following code.
this.getClass().getClassLoader().getResource("sample.xml")
Sample examples:
public class Sample {
public void callXmlMapping() {
URL url = this.getClass().getClassLoader().getResource("sample.xml");
}
}
You can create jar file with these both files and add this jar file in class path of your webapplication.
Log4j setup and configuration specific to module
Lets assume you would like to enable logging for the modules which starts with the package called com.sample.*.*.*. You need to create a log4j.properties file in \web-inf\classes folder. And place foloowing entries in the log4j.properties file. And make sure that log4j.jar file needs to be there in your class path (Ex: web-inf\lib folder)
log4j.logger.com.sample=DEBUG, sample
log4j.appender.sample.File=${user.dir}/sample/logs/sample.log
log4j.appender.sample.MaxFileSize=10MB
log4j.appender.sample.MaxBackupIndex=10
log4j.appender.sample.Append=false
log4j.appender.sample=org.apache.log4j.RollingFileAppender
log4j.appender.sample.layout=org.apache.log4j.PatternLayout
log4j.appender.sample.layout.ConversionPattern=%d{ABSOLUTE} %5p [%t] %c - %m%n
Now, the log messages will redirect to the location ${user.dir}/sample/logs/sample.log.
log4j.logger.com.sample=DEBUG, sample
log4j.appender.sample.File=${user.dir}/sample/logs/sample.log
log4j.appender.sample.MaxFileSize=10MB
log4j.appender.sample.MaxBackupIndex=10
log4j.appender.sample.Append=false
log4j.appender.sample=org.apache.log4j.RollingFileAppender
log4j.appender.sample.layout=org.apache.log4j.PatternLayout
log4j.appender.sample.layout.ConversionPattern=%d{ABSOLUTE} %5p [%t] %c - %m%n
Now, the log messages will redirect to the location ${user.dir}/sample/logs/sample.log.
Wednesday, September 8, 2010
Repeat HashMap using JSTL tags
Lets assume You have a HashMap like this:
Map map = new HashMap();
map.put("fruits", "Apples");
map.put("Vegetables", "Tomota");
map.put("Vehicles", "Hero Honda");
request.setAttribute("itemsMap", map);
We need the following code to repeat the HashMap Using JSTL tags
c:forEach var="item" items="${itemsMap}">
Key is : ${item.key} and value is: ${item.value}
/c:forEach>
Please note that "<" is missing before c:forEach tag
Map map = new HashMap();
map.put("fruits", "Apples");
map.put("Vegetables", "Tomota");
map.put("Vehicles", "Hero Honda");
request.setAttribute("itemsMap", map);
We need the following code to repeat the HashMap Using JSTL tags
c:forEach var="item" items="${itemsMap}">
Key is : ${item.key} and value is: ${item.value}
/c:forEach>
Please note that "<" is missing before c:forEach tag
Subscribe to:
Posts (Atom)