Design Pattern Interview Questions
1) What is a software design pattern?
A design pattern is a solution to a general software problem within a particular context.
Context : A recurring set of situations where the pattern applies.
Problem : A system of forces (goals and constraints) that occur repeatedly in this context.
Solution : A description of communicating objects and classes (collaboration) that can be applied to resolve those forces.
Design patterns capture solutions that have evolved over time as developers strive for greater flexibility in their software. Whereas class libraries are reusable source code, and components are reusable packaged objects, patterns are generic, reusable design descriptions that are customized to solve a specific problem. The study of design patterns provides a common vocabulary for communication and documentation, and it provides a framework for evolution and improvement of existing patterns.
2) Why is the study of patterns important?
As initial software designs are implemented and deployed, programmers often discover improvements which make the designs more adaptable to change. Design patterns capture solutions that have evolved over time as developers strive for greater flexibility in their software, and they document the solutions in a way which facilitates their reuse in other, possibly unrelated systems. Design patterns allow us to reuse the knowledge of experienced software designers.
Moreover, the study of design patterns provides a common vocabulary for communication and documentation, and it provides a framework for evolution and improvement of existing patterns. As an analogy, consider that during a discussion among programmers, the words “stack” and “tree” can be used freely without explanation. Software developers understand fundamental data structures such as a “stack” because these data structures are well-documented in textbooks and are taught in computer science courses. The study of design patterns will have a similar (but more profound) effect by allowing designers to say “composite pattern” or “observer pattern” in a particular design context, without having to describe all classes, relationships, and collaborations which make up the pattern. Patterns raise the level of abstraction when discussing and documenting software designs.
3) How do I document a design pattern?
A pattern description must address the following major points:
Pattern Name and Classification : A short, meaningful name for the pattern, usually only one or two words. Names provide a vocabulary for patterns, and they have implied semantics – choose names carefully. Following the GoF book, we can also group patterns into higher level classifications such as creational, structural, and behavioral patterns.
Problem : A general description of the problem context and the goals and constraints that occur repeatedly in that context. A concrete motivational scenario can be used to help describe the problem. The problem description should provide guidance to assist others in recognizing situations where the pattern can be applied.
Solution : The classes and/or objects that participate in the design pattern, their structure (e.g., in terms of a UML class diagram), their responsibilities, and their collaborations. The solution provides an abstract description that can be applied in many different situations. Sample Code in an object-oriented language can be used to illustrate a concrete realization of the pattern.
Consequences : A discussion of the results and tradeoffs of applying the pattern. Variations and language-dependent alternatives should also be addressed.
Known Uses : Examples of the pattern in real systems. Look for applications of the pattern in language libraries and frameworks, published system descriptions, text books, etc. Not every good solution represents a pattern. A general rule of thumb is that a candidate pattern (also called a “proto-pattern”) should be discovered in a minimum of three existing systems before it can rightfully be called a pattern.
The following quote by Robert Martin highlights the importance of providing pattern descriptions: “The revolutionary concept of the GoF book is not the fact that there are patterns; it is the way in which those patterns are documented. … Prior to the GoF book, the only good way to learn patterns was to discover them in design documentation, or (more probably) code.”
4) Where can I learn more about design patterns?
The best place to start is the seminal work by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (collectively known as the “Gang of Four” or simply “GoF”) entitled Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995).
Warning: This book is not light reading. From the Preface: “Don’t worry if you don’t understand this book completely on the first reading. We didn’t understand it all on the first writing.”
It is, however, a book which wears well over time, and it is definitely worth the effort required to work through it.
Beyond the GoF book, consider the list of references in the Design Patterns section of this bibliography on object technology, plus the following web links:
5) What is an example of a design pattern?
Following the lead of the “Gang of Four” (GoF), design pattern descriptions usually contain multiple sections including
Intent
Motivation
Applicability
Structure
Participants
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
A complete discussion of even a small pattern is beyond the scope of a simple FAQ entry, but it is possible to get the idea by examining an abbreviated discussion of one of the simplest and most easily understood patterns. Consider the Singleton pattern, whose intent reads as follows:
Intent: Ensure that a class has one instance, and provide a global point of access to it.
Almost every programmer has encountered this problem and formulated an approach for solving it in a general way – some solutions are better than others. The solution offered by the GoF would look something like the following when coded in Java.
public class Singleton {
private static Singleton instance = null;
public static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
protected Singleton() { ... }
// possibly another constructor form
public void someMethod() { ... }
//... other methods }
The programmer would access the single instance of this class by writing something similar to
1
Singleton.getInstance().someMethod()
or similar to
1
2
3
Singleton s = Singleton.getInstance();
s.method1(); ...
s.method2(); ...
For a more complete discussion of the Singleton pattern, see the chapter “Singleton” in the book Design Patterns: Elements of Reusable Object-Oriented Software by the “Gang of Four” (Addison-Wesley, 1995), or the chapter “Singleton” in the book Patterns in Java, Volume 1 by Mark Grand (John Wiley & Sons, 1998). For information about variations on the Singleton Pattern, see the chapter entitled “To Kill a Singleton” in the book Pattern Hatching: Design Patterns Applied by John Vlissides or the article “Implementing the Singleton Pattern in Java” by Rod Waldhoff.
6) Calendar is an abstract class. The getInstance() method tries to instantiate GregorianCalendar() i.e., parent instantiating a derived class. This looks Non-OO? Ex: Calendar a=Calendar.getInstance(); Can somebody explain why is it so?
The Calender class is an abstact class, true, however,the point you missed is that the getInstance() returns the ” Calendar using the default timezone and locale. ” , in your case, the GregorianCalender a class that IS a Calender (a Suzuki IS a car, a 747 IS a plane..the standard OO terminology. So what you get is a class that does some specialized work based on the default locale. Other methods
1
2
3
4
public static synchronized Calendar
getInstance(TimeZone zone,Locale aLocale)
public static synchronized Calendar
getInstance(TimeZone zone)
return Calenders for specific timezones and locales. The closest parallel is possibly the Factory Method design pattern.
7) What major patterns do the Java APIs utilize?
Design patterns are used and supported extensively throughout the Java APIs. Here are some examples:
The Model-View-Controller design pattern is used extensively throughout the Swing API.
The getInstance() method in java.util.Calendar is an example of a simple form of the Factory Method design pattern.
The classes java.lang.System and java.sql.DriverManager are examples of the Singleton pattern, although they are not implemented using the approach recommended in the GoF book but with static methods.
The Prototype pattern is supported in Java through the clone() method defined in class Object and the use of java.lang.Cloneable interface to grant permission for cloning.
The Java Swing classes support the Command pattern by providing an Action interface and an AbstractAction class.
The Java 1.1 event model is based on the observer pattern. In addition, the interface java.util.Observable and the class java.util.Observer provide support for this pattern.
The Adapter pattern is used extensively by the adapter classes in java.awt.event.
The Proxy pattern is used extensively in the implementation of Java’s Remote Method Invocation (RMI) and Interface Definition Language (IDL) features.
The structure of Component and Container classes in java.awt provide a good example of the Composite pattern.
The 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.
8)How can I make sure at most one instance of my class is ever created?
This is an instance where the Singleton design pattern would be used. You need to make the constructor private (so nobody can create an instance) and provide a static method to get the sole instance, where the first time the instance is retrieved it is created:
public class Mine {
private static Mine singleton; private Mine() { }
public static synchronized Mine getInstance() {
if (singleton == null) {
singleton = new Mine();
} return singleton;
}
// other stuff... }
9) When would I use the delegation pattern instead of inheritence to extend a class’s behavior?
Both delegation and inheritance are important concepts in object-oriented software design, but not everyone would label them as patterns. In particular, the seminal book on design patterns by the “Gang of Four” contains a discussion of inheritance and delegation, but the authors do not treat these topics as specific patterns. It is reasonable to think of them as design concepts which are more general than specific design patterns.
Inheritance is a relationship between two classes where one class, called a subclass in this context, inherits the attributes and operations of another class, called its superclass. Inheritance can be a powerful design/reuse technique, especially when it is used in the context of the Liskov Substitution Principle. (The article by Robert Martin at http://www.objectmentor.com/publications/lsp.pdf provides an excellent explanation of the ideas behind Barbara Liskov’s original paper on using inheritance correctly.) The primary advantages of inheritance are
it is directly supported by object-oriented languages, and
it provides the context for polymorphism in strongly-typed object-oriented languages such as C++ and Java.
But since the inheritance relationship is defined at compile-time, a class can’t change its superclass dynamically during program execution. Moreover, modifications to a superclass automatically propagate to the subclass, providing a two-edged sword for software maintenance and reuse. In summary, inheritance creates a strong, static coupling between a superclass and its subclasses.
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate. Delegation can also a powerful design/reuse technique. The primary advantage of delegation is run-time flexibility – the delegate can easily be changed at run-time. But unlike inheritance, delegation is not directly supported by most popular object-oriented languages, and it doesn’t facilitate dynamic polymorphism.
As a simple example, consider the relationship between a Rectangle class and a Window class. With inheritance, a Window class would inherit its rectangular properties from class Rectangle. With delegation, a Window object would maintain a reference or pointer to a Rectangle object, and calls to rectangle-like methods of the Window object would be delegated to corresponding methods of the Rectangle object.
Now let’s consider a slightly more complex example. Suppose employees can classified based on how they are paid; e.g., hourly or salaried. Using inheritance, we might design three classes: an Employee class which encapsulates the functionality common to all employees, and two subclasses HourlyEmployee and SalariedEmployee which encapsulates pay-specific details. While this design might be suitable for some applications, we would encounter problems in a scenario where a person changes, say from hourly to salaried. The class of an object and the inheritance relationship are both static, and objects can’t change their class easily (but see the State pattern for tips on how to fake it).
A more flexible design would involve delegation – an Employee object could delegate pay-related method calls to an object whose responsibilities focused solely on how the employee is paid. In fact, we might still use inheritance here in a slightly different manner by creating an abstract class (or interface) called PayClassification with two subclasses HourlyPayClassification and SalariedPayClassification which implement classification-specific computations. Using delegation as shown, it would be much easier to change the pay classification of an existing Employee object.
This second example illustrates an important point: In implementing delegation, we often want the capability to replace the delegate with another object, possibly of a different class. Therefore delegation will often use inheritance and polymorphism, with classes of potential delegates being subclasses of an abstract class which encapsulates general delegate responsibilities.
One final point. Sometimes, the choice between delegation and inheritance is driven by external factors such as programming language support for multiple inheritance or design constraints requiring polymorphism. Consider threads in Java. You can associate a class with a thread in one of two ways: either by extending (inheriting) directly from class Thread, or by implementing the Runnable interface and then delegating to a Thread object. Often the approach taken is based on the restriction in Java that a class can only extend one class (i.e., Java does not support multiple inheritance). If the class you want to associate with a thread already extends some other class in the design, then you would have to use delegation; otherwise, extending class Thread would usually be the simpler approach.
10) Which patterns were used by Sun in designing the Enterprise JavaBeans model?
Many design patterns were used in EJB, and some of them are clearly identifiable by their naming convention. Here are several:
Factory Method: Define a interface for creating classes, let a subclass (or a helper class) decide which class to instantiate.
This is used in EJB creation model. EJBHome defines an interface for creating the EJBObject implementations. They are actually created by a generated container class. See InitialContextFactory interface that returns an InitialContext based on a properties hashtable.
Singleton: Ensure a class has only one instance, and provide a global point of access to it.
There are many such classes. One example is javax.naming.NamingManager
Abstract Factory: Provide an interface for creating families of relegated or dependent objects without specifying their concrete classes.
We have interfaces called InitialContext, InitialContextFactory. InitialContextFactory has methods to get InitialContext.
Builder: Separate the construction of a complex factory from its representation so that the same construction process can create different representations.
InitialContextFactoryBuilder can create a InitialContextFactory.
Adapter: Convert the interface of a class into another interface clients expect.
In the EJB implementation model, we implement an EJB in a class that extends SessionBean or a EntityBean. We don’t directly implement the EJBObject/home interfaces. EJB container generates a class that adapts the EJBObject interface by forwarding the calls to the enterprise bean class and provides declarative transaction, persistence support.
Proxy: Provide a surrogate for other object to control access to it.
We have remote RMI-CORBA proxies for the EJB’s.
Memento: Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.
Certainly this pattern is used in activating/passivating the enterprise beans by the container/server.
11) What is an analysis pattern?
An analysis pattern is a software pattern not related to a language or implementation problem, but to a business domain, such as accounting or health care. For example, in health care, the patient visit activity would be subject to a number of patterns.
There is a good overview from an OOPSLA ’96 presentation at http://www.jeffsutherland.org/oopsla96/fowler.html
A good text would be: Martin Fowler’s Martin Analysis Patterns : Reusable Object Models, ISBN: 0201895420, published by Addison-Wesley.
In summary, analysis patterns are useful for discovering and capturing business processes.
12) What are the differences between analysis patterns and design patterns?
Analysis pattern are for domain architecture, and design pattern are for implementation mechanism for some aspect of the domain architecture. In brief, analysis pattern are more high level and more (end-user) functional oriented.
13) How does “Extreme Programming” (XP) fit with patterns?
Extreme Programming has a large emphasis on the concept of refactoring: Writing code once and only once.
Patterns, particularly the structural patterns mentioned by the Gang of Four, can give good pointers about how to acheive that goal.
(XP states when and where factoring should happen, patterns can show you how.)
14) What is the disadvantage of using the Singleton pattern? It is enticing to use this pattern for all the classes as it makes it easy to get the reference of the singleton object.
The intent of the Singleton pattern is to ensure a class has only one instance and to provide a global point of access to it. True, the second part about providing a global point of access is enticing, but the primary disadvantage of using the Singleton pattern for all classes is related to the first part of the intent; i.e., that it allows only one instance of the class. For most classes in an application, you will need to create multiple instances. What purpose would a Customer class serve if you could create only one Customer object?
15) How do you write a Thread-Safe Singleton?
I have written plenty of non-thread-safe Singletons but it wasn’t until recently when I tracked it down that I realized that thread-safety could be a big problem.
The problem is that in the typical Singleton implementation (at least the ones I’ve seen) there is the ability to create multiple versions of the single instance…I know, “But How?”.
Well, in the getInstance() call the instance is checked for null, and then immediately constructed if it is null, and then the instance is returned.
The problem is that the thread (Ta) making the call could swap-out immediately after checking for a null. A subsequent thread (Tb) could then make a call to get the instance and construct an instance of the Singleton. When the original thread (Ta) is then swapped back in, it would construct and return a completely separate object. BAD KITTY!
The following code snippet shows an example of a thread-safe Singleton.
package com.jgk.patterns.singleton;
public class JGKSingleton {
/* Here is the instance of the Singleton */
private static JGKSingleton instance_;
/* Need the following object to synchronize */
/* a block */
private static Object syncObject_;
/* Prevent direct access to the constructor
private JGKSingleton() { super(); }
public static JGKSingleton getInstance() {
/* in a non-thread-safe version of a Singleton */
/* the following line could be executed, and the */
/* thread could be immediately swapped out */
if (instance_ == null) {
synchronized(syncObject_) {
if (instance_ == null) {
instance_ = new JGKSingleton();
}
}
}
return instance_;
}
}
NOTE: The 2nd check for if (instance_ == null) is needed to avoid making another unnecessary construct.
Don’t let this byte you!
16) What is the Reactor pattern?
The new book “Pattern-oriented Software Architecture Volume 2″ ISBN 0471606952 has a chapter on the Reactor pattern. It falls under the general category of “Event Handling Patterns”. To quote the leading bit of the chapter,
“The Reactor architectural pattern allows event-driven applications to demultiplex and dispatch service requests that are delivered to an application from one or more clients”
It is used in a synchronous manner, so that if the callback you delegate the event to takes a while to complete you will run into problems with scalability.
17) What are Process Patterns?
Basically process patterns define a collection of best practices, techniques, methods for developing object-oriented software.
18) How and where did the concept of design patterns get started?
Work on patterns has been influenced by the works of Christopher Alexander who published on topics related to urban planning and building architecture in the late 1970s. The history of patterns for software design began in the late 1980s and reached an important milestone with the publishing of the first book fully dedicated to this subject by the “Gang of Four”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Design Patterns – Elements of Reusable Object-Oriented Software). In conjunction, the emergence of object-oriented software development fostered the work on other topics related to design patterns such as application frameworks, analysis patterns, language idioms, and so on.
19) Where can I find good examples of the Prototype pattern?
The prototype pattern is actually quite simple to implement in Java.
Recall that the idea of prototype is that you are passed an object and use that object as a template to create a new object. Because you might not know the implementation details of the object, you cannot create a new instance of the object and copy all of its data. (Some of the data may not be accessible via methods). So you ask the object itself to give you a copy of itself.
Java provides a simple interface named Cloneable that provides an implementation of the Prototype pattern. If you have an object that is Cloneable, you can call its clone() method to create a new instance of the object with the same values.
The trick here is that you must override the clone() method to increase its visibility, and just call super.clone(). This is because the implementation of clone(), defined in java.lang.object, is protected. For example:
public class CopyMe implements Cloneable {
public Object clone() {
return super.clone();
}
}
Note that Cloneable is an empty interface! It merely acts as a tag to state that you really want instance of the class to be cloned. If you don’t implement Cloneable, the super.clone() implementation will throw a CloneNotSupportedException.
The Object implementation of clone() performs a shallow copy of the object in question. That is, it copies the values of the fields in the object, but not any actual objects that may be pointed to. In other words, the new object will point to the same objects the old object pointed to.
As an example of using the cloning:
CopyMe thing = new Copyme();
CopyMe anotherThing = (Copyme)thing.clone();
This example is pretty trivial, but the real power comes when you don’t know what you’re actually cloning.
For example, suppose you define an interface that represents a customer:
public interface Customer extends Cloneable {
public Object clone();
// require making it public! public String getName();
public void setName(String name); ... }
You might have several different implementations of this interface, possibly storing data in a file, database, or using EJB Entity beans. If a shallow copy of the data is sufficient to represent a copy of the object, Java’s clone() method works great.
You might have a method that needs to make a copy of the data to store it in a Hashtable, for example:
public void storeCustomer(Customer customer) {
Customer copy = (Customer)customer.clone();
dataStore.put(copy);
}
Note that this method knows nothing about what type of customer we’re getting. This pattern will work for any actual type of Customer, no matter how the data is stored. For example:
FileBasedCustomer c1 = new FileBasedCustomer(...);
RDBMSBasedCustomer c2 = new RDBMSBasedCustomer(...);
EJBBasedCustomer c3 = new EJBBasedCustomer(...);
manager.storeCustomer(c1);
manager.storeCustomer(c2);
manager.storeCustomer(c3);
20) What are Anti-Patterns?
There isn’t really a “clean-cut” definition out there just yet, unlike Design Patterns. Basically, as Design Patterns (and more particularly, Process Patterns) try to codify a standard vocabulary for working solutions to problems that reappear frequently, Anti-Patterns represent an effort to define and classify reoccuring non-solutions, i.e., things that lots of projects do that fail to yield a solution, or actually prevent a project from working or being finished.
The most basic example I can think of is “The Hammer”, inspired by the old addage, “If your only tool is a hammer, everything looks like a nail” (or the variation, “If your only tool is a hammer, everything looks like your left thumb.” Hammer describes a regular, reoccuring problem in inexperienced engineers (particularly those who’ve only used one language for the bulk of their carreer so far), that of trying to force-feed all problems into the solutions they already know.
21) What patterns are particularly useful in building networked applications?
I suggest starting with Pattern-Oriented Software Architecture: Patterns for Concurrent and Networked Objects (POSA2). POSA2 specifically brings together 17 interrelated patterns addressing Service Access and Configuration, Event Handling, Synchronization, and Concurrency.
The patterns and many of the examples in POSA2 come primarily from the design and implementation of the ACE framework.
22) Are there any good Java-specific patterns books available?
The Java-specific patterns books are:
Java Design Patterns: A Tutorial
Patterns in Java, Volume 1
Patterns in Java, Volume 2
Concurrent Programming in Java , Second Edition: Design Principles and Patterns
SanFrancisco Design Patterns
As far as the good part of the question…. Doug Lea’s Concurrent Programming book is probably the best of the bunch. However, its patterns are specific to concurrent programming. Many people don’t like the quality of Mark Grand’s two Patterns in Java books. [They are rated 3 and 2 stars at Amazon, respectively]. The first printing of the Cooper tutorial book was riddled with errors. If you get that, be sure to get at least the second printing. [Look on last line of page before TOC.] The SanFrancisco book is definitely good, but I’m not sure how good if you aren’t using SF.
23) What are Collaboration Patterns?
Collaboration Patterns are repeatable techniques used by teams of people to help them work together (collaborate). Ellen Gottesdiener of EBG Consulting has created these patterns in order to help facilitate good teamwork. These patterns really have nothing to do with object-oriented development or Java, besides the fact that they can help with requirements gathering or CRC design sessions.
In a nutshell, Collaboration Patterns are techniques to help make meetings useful.
24) Is it correct from a design point of view to make an object both an Observer and Observable at the same time?
Yes, and this can be the preferred pattern in some cases.
For example, suppose you were writing a supply chain management system for a retail chain. Each store object in your system generates item-sold events; when the chain generates enough of these for a particular product, a buyer object generates a purchase order for more of the product. However, the buyer object has no particular interest in individual item sold events. Instead, the buyer (Observer) registers to receive out-of-stock events from the warehouse (Observable); the warehouse, as Observer, registers with the individual stores (Observables) to receive item-sold events. Thus, the warehouse is both Observer and Observable. (Please note that this is a synthetic example, and probably not the way to organize a supply chain.)
Another reason to use one or more central Observer-Observable object in between ultimate Observers and Observables is to fully decouple the two. In some cases, Observer and Observable may exist on different machines, and may rely on the central Observer-Observable to hide this complication.
A good source for more details is the Publisher-Subscriber section of Buschmann et al., Pattern-Oriented Software Architecture: A System of Patterns.
25) How can I maintain a single instance of an object in an applet?
In start(), instead of always creating a new object, return the existing one if it exists or create a new one if it doesn’t.
26) What is the best way to generate a universally unique object ID? Do I need to use an external resource like a file or database, or can I do it all in memory?
I need to generate unique id’s that will be used for node ‘ID’ attribute values within XML documents. This id must be unique system-wide. The generator must be available to a number of servlets that add various node structures to my XML docs as a service. What is the best way to tackle this? The ‘possible’ ways I can see:
Keep the maximum ID value in a flat-file where the service would read it upon start-up and increment it. Upon shutdown or failure, it would write the latest max id to the file.
Calculate the max id by searching the XML itself. This will be tougher since XML requires an alpha-numeric value (not strictly numeric).
Use a database (MySQL) with a two-field table where one field is the incremental counter.
There is an additional way to do that that doesn’t rely on an external file (or database) like the one you have presentred. If has been presented in the EJB Design Patterns book, written by Floyd Marinescu, and available in a pdf format for free from the given link.
The suggested solution is based on the UUID for EJB pattern, that comes out from this question:
How can universally unique primary keys can be generated in menory without requiring a database or a singleton?
Without enetring in the specifics (you can fully check out the pattern by reading the appropriate chapter), the solution is to generate a 32 digit key, encoded in hexadecimal composed as follows:
Unique down to the millisecond. Digits 1-8 are are the hex encoded lower 32 bits of the System.currentTimeMillis() call.
Unique across a cluster. Digits 9-16 are the encoded representation of the 32 bit integer of the underlying IP address.
Unique down to the object in a JVM. Digits 17-24 are the hex representation of the call to System.identityHashCode(), which is guaranteed to return distinct integers for distinct objects within a JVM.
Unique within an object within a millisecond. Finally digits 25-32 represent a random 32 bit integer generated on every method call using the cryptographically strong java.security.SecureRandom class.
27) Is there some kind of Design pattern which would make it possible to use the Same code base in EJB and non EJB context?
A good suggestion would be using Delegation
class PieceOfCode {
public Object myMethod() {}
}
class EJBImpl ... {
PieceOfCode poc = new PieceOfCode();
public Object myMethod() {
return poc.myMethod();
}
}
This should not be a violation of EJB specs, since EJBs can use simple java classes for their use. Think about Dependant Objects and so on.
28) What is session facade?
Session facade is one design pattern that is often used while developing enterprise applications.
It is implemented as a higher level component (i.e.: Session EJB), and it contains all the iteractions between low level components (i.e.: Entity EJB). It then provides a single interface for the functionality of an application or part of it, and it decouples lower level components simplifying the design.
Think of a bank situation, where you have someone that would like to transfer money from one account to another.
In this type of scenario, the client has to check that the user is authorized, get the status of the two accounts, check that there are enough money on the first one, and then call the transfer. The entire transfer has to be done in a single transaction otherwise is something goes south, the situation has to be restored.
As you can see, multiple server-side objects need to be accessed and possibly modified. Multiple fine-grained invocations of Entity (or even Session) Beans add the overhead of network calls, even multiple transaction. In other words, the risk is to have a solution that has a high network overhead, high coupling, poor reusability and mantainability.
The best solution is then to wrap all the calls inside a Session Bean, so the clients will have a single point to access (that is the session bean) that will take care of handling all the rest.
Obviously you need to be very careful when writing Session Facades, to avoid the abusing of it (often called “God-Bean”).
For a detailed description of this pattern, check this page: Core J2EE Patterns – Session Facade or get Floyd Marinescu’s EJB Design Patterns, in PDF format.
29) How is JDO different from VO ?
JDO is a persistence technology that competes against entity beans in enterprise application development. It allows you to create POJOs (plain old java objects) and persist them to the database – letting JDO take care of the storage.
Value objects, on the other hand, represent an abstract design pattern used in conjuction with entity beans, jdbc, and possibly even JDO to overcome commonly found isolation and transactional problems in enterprise apps. Value objects alone do not allow you to persist objects – they are simple data holders used to transfer data from the database to the client and back to the database.
Side note: I know that many books out there still refer to these data holders as value objects but the correct term is DTO: data transfer objects. Value objects refer to objects that hold a value. A good example of this java.lang.Integer object which holds an int.
30) How can I implement the MVC design pattern using JSP?
The MVC (Model View Controller) design pattern is a pattern/architecture that can be used by GUI’s. It seperates the application’s data, user interface and control logic into three separate entities. This ensures the system will be more maintainable in the future as changes to one component will not have an affect on the others.
The MVC pattern also conforms with the JSP Model 2 architecture.
The MVC pattern can be easily implemented in web applications using JSTL core, JSP, Servlets and JavaBeans.
JSTL makes it easy for HTML designers to use dynamic data in their pages without having to learn in depth java. The tags are in a recognizable HTML like format meaning a smaller learning curve than taking on the JSP specification after taking on the Java specification.
JSTL also makes it easy for web developers who are developing all aspects of the application in helping you keep the content separate from the display by keeping your JSP clean from any Java code. Since JSTL and its EL (expression Language) are really only suited for accessing data, it forces you to use a MVC pattern so long as you keep all JSP and Java syntax/code out of the JSP pages.
A common scenario might look like this, a user sends a request to a server. The request is handled by a Servlet (the controller) which will initialize any JavaBeans (the model) required to fulfill the user’s request. The Servlet (the controller) will then forward the request, which contains the JavaBeans (the model), to a JSP (the view) page which contains only HTML and JSTL syntax. The JSTL will format the data into a human readable format before being sent back to the user. Thus completing the full MVC process.
JSTL also has a tag library for XML processing, this could be used in an MVC environment as you could replace the JavaBeans with an XML document to describe your data, so long as the XML is still constructed in a controller such as a Servlet before being passed to the JSP (the view).
JSTL also has a tag library for database access, using this in your JSP (the view) pages would NOT comply to the MVC pattern as there will be no separation between the model and the view.
1. Give an example where you prefer abstract class over interface ?
This is common but yet tricky design interview question. both interface and abstract class follow "writing code for interface than implementation" design principle which adds flexibility in code, quite important to tackle with changing requirement. here are some pointers which help you to answer this question:
1. In Java you can only extend one class but implement multiple interface. So if you extend a class you lost your chance of extending another class.
2. Interface are used to represent adjective or behavior e.g. Runnable, Clonable, Serializable etc, so if you use an abstract class to represent behavior your class can not be Runnable and Clonable at same time because you can not extend two class in Java but if you use interface your class can have multiple behavior at same time.
3. On time critical application prefer abstract class is slightly faster than interface.
4. If there is a genuine common behavior across the inheritance hierarchy which can be coded better at one place than abstract class is preferred choice. Some time interface and abstract class can work together also where defining function in interface and default functionality on abstract class.
2. Design a Vending Machine which can accept different coins, deliver different products?
This is an open design question which you can use as exercise, try producing design document, code and Junit test rather just solving the problem and check how much time it take you to come to solution and produce require artifacts, Ideally this question should be solve in 3 hours, at least a working version.
3. You have a Smartphone class and will have derived classes like IPhone, AndroidPhone, WindowsMobile Phone can be even phone names with brand, how would you design this system of Classes.
This is another design pattern exercise where you need to apply your object oriented design skill to come with a design which is flexible enough to support future products and stable enough to support changes in existing model.
4. When do you overload a method in Java and when do you override it ?
Rather a simple question for experienced designer in Java. if you see different implementation of a class has different way of doing certain thing than overriding is the way to go while overloading is doing same thing but with different input. method signature varies in case of overloading but not in case of overriding in java.
5. Design ATM Machine ?
We all use ATM (Automated Teller Machine) , Just think how will you design an ATM ? for designing financial system one must requirement is that they should work as expected in all situation. so no matter whether its power outage ATM should maintain correct state (transactions), think about locking, transaction, error condition, boundary condition etc. even if you not able to come up exact design but if you be able to point out non functional requirement, raise some question , think about boundary condition will be good progress.
6. You are writing classes to provide Market Data and you know that you can switch to different vendors overtime like Reuters, wombat and may be even to direct exchange feed , how do you design your Market Data system.
This is very interesting design interview question and actually asked in one of big investment bank and rather common scenario if you have been writing code in Java. Key point is you will have a MarketData interface which will have methods required by client e.g. getBid(), getPrice(), getLevel() etc and MarketData should be composed with a MarketDataProvider by using dependency injection. So when you change your MarketData provider Client won't get affected because they access method form MarketData interface or class.
7. Why is access to non-static variables not allowed from static methods in Java
You can not access non-static data from static context in Java simply because non-static variables are associated with a particular instance of object while Static is not associated with any instance. You can also see my post why non static variable are not accessible in static context for more detailed discussion.
8. Design a Concurrent Rule pipeline in Java?
Concurrent programming or concurrent design is very hot now days to leverage power of ever increasing cores in advanced processor and Java being a multi-threaded language has benefit over others. Do design a concurrent system key point to note is thread-safety, immutability, local variables and avoid using static or instance variables. you just to think that one class can be executed by multiple thread a same time, So best approach is that every thread work on its own data, doesn't interfere on other data and have minimal synchronization preferred at start of pipeline. This question can lead from initial discussion to full coding of classes and interface but if you remember key points and issues around concurrency e.g. race condition, deadlock, memory interference, atomicity, ThreadLocal variables etc you can get around it.
1. What is design patterns ? Have you used any design pattern in your code ?
Design patterns are tried and tested way to solve particular design issues by various programmers in the world. Design patterns are extension of code reuse.
2. Can you name few design patterns used in standard JDK library?
Decorator design pattern which is used in various Java IO classes, Singleton pattern which is used in Runtime , Calendar and various other classes, Factory pattern which is used along with various Immutable classes likes Boolean e.g. Boolean.valueOf and Observer pattern which is used in Swing and many event listener frameworks.
3. What is Singleton design pattern in Java ? write code for thread-safe singleton in Java
Singleton pattern focus on sharing of expensive object in whole system. Only one instance of a particular class is maintained in whole application which is shared by all modules. Java.lang.Runtime is a classical example of Singleton design pattern. You can also see my post 10 questions on Singleton pattern in Java for more questions and discussion. From Java 5 onwards you can use enum to thread-safe singleton.
4. What is main benefit of using factory pattern ? Where do you use it?
Factory pattern’s main benefit is increased level of encapsulation while creating objects. If you use Factory to create object you can later replace original implementation of Products or classes with more advanced and high performance implementation without any change on client layer. See my post on Factory pattern for more detailed explanation and benefits.
5. What is observer design pattern in Java
Observer design pattern is based on communicating changes in state of object to observers so that they can take there action. Simple example is a weather system where change in weather must be reflected in Views to show to public. Here weather object is Subject while different views are Observers. Look on this article for complete example of Observer pattern in Java.
6. Give example of decorator design pattern in Java ? Does it operate on object level or class level ?
Decorator pattern enhances capability of individual object. Java IO uses decorator pattern extensively and classical example is Buffered classes like BufferedReader and BufferedWriter which enhances Reader and Writer objects to perform Buffer level reading and writing for improved performance. Read more on Decorator design pattern and Java
What are design patterns?
A pattern is a proven (and recurring) solution to a problem in a context. Each pattern describes a problem which occurs time and again in our environment, and describes its solution to this problem in such a way that we can use this solution any number of times. In simple words, there are a lot of common problems which a lot of developers have faced over time. These common problems ideally should have a common solution too. It is this solution when documented and used over and over becomes a design pattern.
Can we always apply the same solution to different problems at hand?
No. Design patterns would study the different problems at hand. To these problems then it would suggest different design patterns to be used. However, the type of code to be written in that design pattern is solely the discretion of the Project Manager who is handling that project.
What should be the level of detail/abstraction which should be provided by a design pattern?
Design patterns should present a higher abstraction level though it might include details of the solution. However, these details are lower abstractions and are called strategies. There may be more than one way to apply these strategies in implementing the patterns. The details of implementation are usually upto the developer who is coding at the ground level.
What are the most common problems which one faces during the application design phase that are solved by design patterns?
Some are:
1. Identifying components, internal structures of the components, and relationships between components.
2. Determining component granularity and appropriate interactions
3. Defining component interfaces.
How does one decide which Design pattern to use in our application?
We need to follow these steps:
1. We need to understand the problem at hand. Break it down to finer grained problems. Each design pattern is meant to solve certain kinds of problems. This would narrow down our search for design patterns.
2. Read the problem statement again along with the solution which the design pattern will provide. This may instigate to change a few patterns that we are to use.
3. Now figure out the interrelations between different patterns. Also decide what all patterns will remain stable in the application and what all need to change (with respect to Change Requests received from the clients).
What is Refactoring?
Learning different design patterns is not sufficient to becoming a good designer. We have to understand these patterns and use them where they have more benefits. Using too many patterns (more than required) would be over-engineering and using less design patterns than required would be under-engineering. In both these scenarios we use refactoring. Refactoring is a change made to the internal structure of the software to make it easier to understand and cheaper to modify, without changing its observable behaviour.
What are Antipatterns?
Though the use of patterns fulfils our objectives in the applications; there are also several instances where several applications did not fulfill their goals. The architects of these applications too need to document these wrong decisions. This helps others by preventing them from repeating these mistakes in their applications. Such documented mistakes are called antipatterns.
As we do development in tiers, how do we divide patterns in tiers?
The Sun Java Center has classified the patterns in three tiers. These are:
Presentation tier patterns for web-component tier,
Business tier patterns for business logic (EJB) tier, and
Integration tier patterns for connection to the databases.
What are the Presentation Tier Patterns?
The presentation tier patterns are:
Intercepting filter, Front Controller, View Helper, Composite View, Service-to-Worker, and Dispatcher View.
What are the Business Tier Patterns?
The business tier patterns are:
Business delegate, Value Object, Session Façade, Composite Entity, Value Object Assembler, Value List Handler, and Service Locator.
What are the Integration Tier Patterns?
Integration tier patterns are:
Data Access Object (DAO) and Service Activator
What is Intercepting Filter pattern?
Provides a solution for pre-processing and post-processing a request. It allows us to declaratively apply filters for intercepting requests and responses. For ex. Servlet filters.
What is Front Controller pattern?
It manages and handles requests through a centralized code. This could either be through a servlet or a JSP (through a Java Bean). This Controller takes over the common processing which happens on the presentation tier. The front controller manages content retrieval, security, view management and retrieval.
What is View Helper pattern?
There generally are two parts to any application – the presentation and the business logics. The “View” is responsible for the output-view formatting whereas “Helper” component is responsible for the business logic. Helper components do content retrieval, validation and adaptation. Helper components generally use Business delegate pattern to access business classes.
What is Composite View pattern?
This pattern is used for creating aggregate presentations (views) from atomic sub-components. This architecture enables says piecing together of elementary view components which makes the presentation flexible by allowing personalization and customization.
What is Service to Worker pattern?
This is used in larger applications wherein one class is used to process the requests while the other is used to process the view part. This differentiation is done for maintainability.
What is Dispatcher View pattern?
This is similar to Service to Worker pattern except that it is used for smaller applications. In this one class is used for both request and view processing.
What is Business Delegate pattern?
This pattern is used to reduce the coupling between the presentation and business-logic tier. It provides a proxy to the façade from where one could call the business classes or DAO class. This pattern can be used with Service Locator pattern for improving performance.
What is Value Object pattern?
Value Object is a serializable object which would contain lot of atomic values. These are normal java classes which may have different constructors (to fill in the value of different data) and getter methods to get access to these data. VOs are used as a course grained call which gets lots of data in one go (this reduces remote overhead). The VO is made serializable for it to be transferred between different tiers within a single remote method invocation
What is Session Façade pattern?
This pattern hides the complexity of business components and centralizes the workflow. It provides course-grained interfaces to the clients which reduces the remote method overhead. This pattern fits well with declarative transactions and security management.
What is Value Object Assembler pattern?
This pattern allows for composing a Value Object from different sources which could be EJBs, DAOs or Java objects.
What is Value List Handler pattern?
This pattern provides a sound solution for query execution and results processing.
What is Service Locator pattern?
It provides a solution for looking-up, creating and locating services and encapsulating their complexity. It provides a single point of control and it also improves performance.
What is Data Access Object pattern?
It provides a flexible and transparent access to the data, abstracts the data sources and hides the complexity of Data persistence layer. This pattern provides for loose coupling between business and data persistence layer.
What is EJB Command pattern?
Session Façade and EJB Command patterns are competitor patterns. It wraps business logic in command beans, decouples the client and business logic tier, and reduces the number of remote method invocations.
What is Version Number pattern?
This pattern is used for transaction and persistence and provides a solution for maintaining consistency and protects against concurrency. Every time a data is fetched from the database, it comes out with a version number which is saved in the database. Once any update is requested on the same row of the database, this version is checked. If the version is same, the update is allowed else not.
What all patterns are used to improve performance and scalability of the application?
Value Object, Session Façade, Business Delegate and Service Locator.
What design patterns could be used to manage security?
Single Access Point, Check point and Role patterns
Core Java Interview Questions Answers in Finance domain
1. What is immutable object? Can you write immutable object?
You need to make class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.
2. Does all property of immutable object needs to be final?
Not necessary as stated above you can achieve same functionality by making member as non final but private and not modifying them except in constructor.
3. What is the difference between creating String as new () and literal?
When we create string with new () it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap.
String s = new String("Test");
will put the object in String pool , it it does then why do one need String.intern() method which is used to put Strings into String pool explicitly. its only when you create String object as String literal e.g. String s = "Test" it put the String in pool.
4. How does substring () inside String works?
Another good question, I think answer is not sufficient but here it is “Substring creates new object out of string by taking a portion of original string”.
The substring() method is used to return a part (or substring) of the String used to invoke the method. The first argument represents the starting location (zero-based) of the substring. If the call has only one argument, the substring returned will include the characters to the end of the original String. If the call has two arguments, the substring returned will end with the character located in the nth position of the original String where n is the second argument. Unfortunately, the ending argument is not zero-based, so if the second argument is 7, the last character in the returned String will be in the original String’s 7 position, which is index 6. Let’s look at an example:
String x = "0123456789";
System.out.println( x.substring(5) ); // output is "56789"
System.out.println( x.substring(5, 8)); // output is "567"
The first example should be easy: start at index 5 and return the rest of the String. The second example should be read as follows: start at index 5 and return the characters up to and including the 8th position (index 7).
and @Anonymous pointed out some interesting fact:
omething important about String.substring() method, its implementation calls the following String(...) constructor :
// Package private constructor which shares value array for speed.
String(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}
That means the new String() object returned by substring(...) shares the same backing array (this.value) as the original string object.
Thus if your original string object is 1GB long, the substring object will always be 1GB long too!
You will probably want to trim the new array size to the substring range, this should do the job:
String veryLongString = readFromFile(file);
String tinyString = new String(veryLongString.substring(1,10));
The String(String) constructor is implemented that way:
public String(String original) {
...
if (originalValue.length > size) {
// The array representing the String is bigger than the new
// String itself. Perhaps this constructor is being called
// in order to trim the baggage, so make a copy of the array.
int off = original.offset;
v = Arrays.copyOfRange(originalValue, off, off+size);
}
...
}
5. Which two method you need to implement for key in hashMap ?
(equals and hashcode) read How HashMap works in Java for detailed explanation.
6. Where does these two method comes in picture during get operation?
See here How HashMap works in Java for detailed explanation.
7. How do you handle error condition while writing stored procedure or accessing stored procedure from java?
Open for all, my friend didn't know the answer so he didn't mind telling me.
8. What is difference between Executor.submit() and Executer.execute() method ?
There is a difference when looking at exception handling. If your tasks throws an exception and if it was submitted with execute this exception will go to the uncaught exception handler (when you don't have provided one explicitly, the default one will just print the stack trace to System.err). If you submitted the task with submit any thrown exception, checked or not, is then part of the task's return status. For a task that was submitted with submit and that terminates with an exception, the Future.get will rethrow this exception, wrapped in an ExecutionException.
9. What is the difference between factory and abstract factory pattern?
Open for all, he explains about factory pattern and how factory pattern saves maintenance time by encapsulating logic of object creation but didn't know exact answer
Abstract Factory provides one more level of abstraction. Consider different factories each extended from an Abstract Factory and responsible for creation of different hierarchies of objects based on the type of factory. E.g. AbstractFactory extended by AutomobileFactory, UserFactory, RoleFactory etc. Each individual factory would be responsible for creation of objects in that genre.
11. Can you write Critical section code for singleton?
check here 10 Interview questions on Singleton Pattern in Java
12. Can you write code for iterating over hashmap in Java 4 and Java 5 ?
Tricky one but he managed to write using while and for loop.
13. When do you override hashcode and equals() ?
Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap. check this for writing equals method correctly 5 tips on equals in Java
14. What will be the problem if you don't override hashcode() method ?
You will not be able to recover your object from hash Map if that is used as key in HashMap.
See here How HashMap works in Java for detailed explanation.
15. Is it better to synchronize critical section of getInstance() method or whole getInstance() method ?
Answer is critical section because if we lock whole method than every time some one call this method will have to wait even though we are not creating any object)
16. What is the difference when String is gets created using literal or new() operator ?
When we create string with new() its created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap.
17. Does not overriding hashcode() method has any performance implication ?
This is a good question and open to all , as per my knowledge a poor hashcode function will result in frequent collision in HashMap which eventually increase time for adding an object into Hash Map.
18. What’s wrong using HashMap in multithreaded environment? When get() method go to infinite loop ?
Another good question. His answer was during concurrent access and re-sizing.
19. Give a simplest way to find out the time a method takes for execution without using any profiling tool?
Read the system time just before the method is invoked and immediately after method returns. Take the time difference, which will give you the time taken by a method for execution.
To put it in code…
long start = System.currentTimeMillis ();
method ();
long end = System.currentTimeMillis ();
System.out.println (“Time taken for execution is ” + (end – start));
Remember that if the time taken for execution is too small, it might show that it is taking zero milliseconds for execution. Try it on a method which is big enough, in the sense the one which is doing considerable amout of processing
.
How to choose design patterns
1) Flyweight: As discussed above, it’s a part of JDK and hence is used by every Java program.
2) Iterator: All collection classes provide the feature of an iterator to iterate their elements. Moreover, I have also seen custom classes in my projects where the feature of iterator is custom built for those classes.
3) DAO (Data Access Object) : This is also one of the common design pattern seen in many applications which use SQL commands to interact with the datasource/database. Usually a DAO class has methods which invoke the SQL queries for CRUD operations on the database tables thus avoiding the need to write and maintain SQL queries in multiple classes for the same database tables.
4) DTO (Data Transfer Object) : This design pattern represents the data which is actually stored in the database tables in terms of Java classes and is accepted as argument by various methods of the DAO classes. This design pattern usually translates to POJO classes corresponding to database tables and their columns.
5) Factory: If you have used DAO then it is very likely that you have also seen factory design pattern being used as a factory class is written for generating the DAO’s.
6) Abstract Factory: This design pattern represents a factory of factories. When you have many closely related factory classes, you will write an interface which all those factories implement and thus can be generically referenced. You will write a class which will have methods to return the appropriate factory depending upon the need.
7) MVC: This is also one of the famous design patterns and should not be missed in Java interview.
8) Front Controller: This design pattern is closely associated with MVC design pattern and is used to write the single point of contact for any request that comes from the users. The controller then dispatches the request to appropriate handler depending upon the arguments being passed along with the request.
9) Singleton: Though Singleton design pattern looks to be easy to understand and get away with any Java interview but one can be grilled like anything on this design pattern. There is so much to ask on Singleton that a candidate can be judged only by asking questions on Singleton. This is because the interviewer can associate Inheritance, Reflection, Serialization, Collections, Polymorphism, Cloning and other Java concepts with Singleton.
Still if you were to choose only five design patterns for an interview due to time constraints then I would advise the following list:
1) Singleton
2) DAO (Data Access Object)
3) DTO (Data Transfer Object)
4) MVC (Model View Controller)
5) Factory
A design pattern is a solution to a general software problem within a particular context.
Context : A recurring set of situations where the pattern applies.
Problem : A system of forces (goals and constraints) that occur repeatedly in this context.
Solution : A description of communicating objects and classes (collaboration) that can be applied to resolve those forces.
Design patterns capture solutions that have evolved over time as developers strive for greater flexibility in their software. Whereas class libraries are reusable source code, and components are reusable packaged objects, patterns are generic, reusable design descriptions that are customized to solve a specific problem. The study of design patterns provides a common vocabulary for communication and documentation, and it provides a framework for evolution and improvement of existing patterns.
2) Why is the study of patterns important?
As initial software designs are implemented and deployed, programmers often discover improvements which make the designs more adaptable to change. Design patterns capture solutions that have evolved over time as developers strive for greater flexibility in their software, and they document the solutions in a way which facilitates their reuse in other, possibly unrelated systems. Design patterns allow us to reuse the knowledge of experienced software designers.
Moreover, the study of design patterns provides a common vocabulary for communication and documentation, and it provides a framework for evolution and improvement of existing patterns. As an analogy, consider that during a discussion among programmers, the words “stack” and “tree” can be used freely without explanation. Software developers understand fundamental data structures such as a “stack” because these data structures are well-documented in textbooks and are taught in computer science courses. The study of design patterns will have a similar (but more profound) effect by allowing designers to say “composite pattern” or “observer pattern” in a particular design context, without having to describe all classes, relationships, and collaborations which make up the pattern. Patterns raise the level of abstraction when discussing and documenting software designs.
3) How do I document a design pattern?
A pattern description must address the following major points:
Pattern Name and Classification : A short, meaningful name for the pattern, usually only one or two words. Names provide a vocabulary for patterns, and they have implied semantics – choose names carefully. Following the GoF book, we can also group patterns into higher level classifications such as creational, structural, and behavioral patterns.
Problem : A general description of the problem context and the goals and constraints that occur repeatedly in that context. A concrete motivational scenario can be used to help describe the problem. The problem description should provide guidance to assist others in recognizing situations where the pattern can be applied.
Solution : The classes and/or objects that participate in the design pattern, their structure (e.g., in terms of a UML class diagram), their responsibilities, and their collaborations. The solution provides an abstract description that can be applied in many different situations. Sample Code in an object-oriented language can be used to illustrate a concrete realization of the pattern.
Consequences : A discussion of the results and tradeoffs of applying the pattern. Variations and language-dependent alternatives should also be addressed.
Known Uses : Examples of the pattern in real systems. Look for applications of the pattern in language libraries and frameworks, published system descriptions, text books, etc. Not every good solution represents a pattern. A general rule of thumb is that a candidate pattern (also called a “proto-pattern”) should be discovered in a minimum of three existing systems before it can rightfully be called a pattern.
The following quote by Robert Martin highlights the importance of providing pattern descriptions: “The revolutionary concept of the GoF book is not the fact that there are patterns; it is the way in which those patterns are documented. … Prior to the GoF book, the only good way to learn patterns was to discover them in design documentation, or (more probably) code.”
4) Where can I learn more about design patterns?
The best place to start is the seminal work by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (collectively known as the “Gang of Four” or simply “GoF”) entitled Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995).
Warning: This book is not light reading. From the Preface: “Don’t worry if you don’t understand this book completely on the first reading. We didn’t understand it all on the first writing.”
It is, however, a book which wears well over time, and it is definitely worth the effort required to work through it.
Beyond the GoF book, consider the list of references in the Design Patterns section of this bibliography on object technology, plus the following web links:
5) What is an example of a design pattern?
Following the lead of the “Gang of Four” (GoF), design pattern descriptions usually contain multiple sections including
Intent
Motivation
Applicability
Structure
Participants
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
A complete discussion of even a small pattern is beyond the scope of a simple FAQ entry, but it is possible to get the idea by examining an abbreviated discussion of one of the simplest and most easily understood patterns. Consider the Singleton pattern, whose intent reads as follows:
Intent: Ensure that a class has one instance, and provide a global point of access to it.
Almost every programmer has encountered this problem and formulated an approach for solving it in a general way – some solutions are better than others. The solution offered by the GoF would look something like the following when coded in Java.
public class Singleton {
private static Singleton instance = null;
public static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
protected Singleton() { ... }
// possibly another constructor form
public void someMethod() { ... }
//... other methods }
The programmer would access the single instance of this class by writing something similar to
1
Singleton.getInstance().someMethod()
or similar to
1
2
3
Singleton s = Singleton.getInstance();
s.method1(); ...
s.method2(); ...
For a more complete discussion of the Singleton pattern, see the chapter “Singleton” in the book Design Patterns: Elements of Reusable Object-Oriented Software by the “Gang of Four” (Addison-Wesley, 1995), or the chapter “Singleton” in the book Patterns in Java, Volume 1 by Mark Grand (John Wiley & Sons, 1998). For information about variations on the Singleton Pattern, see the chapter entitled “To Kill a Singleton” in the book Pattern Hatching: Design Patterns Applied by John Vlissides or the article “Implementing the Singleton Pattern in Java” by Rod Waldhoff.
6) Calendar is an abstract class. The getInstance() method tries to instantiate GregorianCalendar() i.e., parent instantiating a derived class. This looks Non-OO? Ex: Calendar a=Calendar.getInstance(); Can somebody explain why is it so?
The Calender class is an abstact class, true, however,the point you missed is that the getInstance() returns the ” Calendar using the default timezone and locale. ” , in your case, the GregorianCalender a class that IS a Calender (a Suzuki IS a car, a 747 IS a plane..the standard OO terminology. So what you get is a class that does some specialized work based on the default locale. Other methods
1
2
3
4
public static synchronized Calendar
getInstance(TimeZone zone,Locale aLocale)
public static synchronized Calendar
getInstance(TimeZone zone)
return Calenders for specific timezones and locales. The closest parallel is possibly the Factory Method design pattern.
7) What major patterns do the Java APIs utilize?
Design patterns are used and supported extensively throughout the Java APIs. Here are some examples:
The Model-View-Controller design pattern is used extensively throughout the Swing API.
The getInstance() method in java.util.Calendar is an example of a simple form of the Factory Method design pattern.
The classes java.lang.System and java.sql.DriverManager are examples of the Singleton pattern, although they are not implemented using the approach recommended in the GoF book but with static methods.
The Prototype pattern is supported in Java through the clone() method defined in class Object and the use of java.lang.Cloneable interface to grant permission for cloning.
The Java Swing classes support the Command pattern by providing an Action interface and an AbstractAction class.
The Java 1.1 event model is based on the observer pattern. In addition, the interface java.util.Observable and the class java.util.Observer provide support for this pattern.
The Adapter pattern is used extensively by the adapter classes in java.awt.event.
The Proxy pattern is used extensively in the implementation of Java’s Remote Method Invocation (RMI) and Interface Definition Language (IDL) features.
The structure of Component and Container classes in java.awt provide a good example of the Composite pattern.
The 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.
8)How can I make sure at most one instance of my class is ever created?
This is an instance where the Singleton design pattern would be used. You need to make the constructor private (so nobody can create an instance) and provide a static method to get the sole instance, where the first time the instance is retrieved it is created:
public class Mine {
private static Mine singleton; private Mine() { }
public static synchronized Mine getInstance() {
if (singleton == null) {
singleton = new Mine();
} return singleton;
}
// other stuff... }
9) When would I use the delegation pattern instead of inheritence to extend a class’s behavior?
Both delegation and inheritance are important concepts in object-oriented software design, but not everyone would label them as patterns. In particular, the seminal book on design patterns by the “Gang of Four” contains a discussion of inheritance and delegation, but the authors do not treat these topics as specific patterns. It is reasonable to think of them as design concepts which are more general than specific design patterns.
Inheritance is a relationship between two classes where one class, called a subclass in this context, inherits the attributes and operations of another class, called its superclass. Inheritance can be a powerful design/reuse technique, especially when it is used in the context of the Liskov Substitution Principle. (The article by Robert Martin at http://www.objectmentor.com/publications/lsp.pdf provides an excellent explanation of the ideas behind Barbara Liskov’s original paper on using inheritance correctly.) The primary advantages of inheritance are
it is directly supported by object-oriented languages, and
it provides the context for polymorphism in strongly-typed object-oriented languages such as C++ and Java.
But since the inheritance relationship is defined at compile-time, a class can’t change its superclass dynamically during program execution. Moreover, modifications to a superclass automatically propagate to the subclass, providing a two-edged sword for software maintenance and reuse. In summary, inheritance creates a strong, static coupling between a superclass and its subclasses.
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate. Delegation can also a powerful design/reuse technique. The primary advantage of delegation is run-time flexibility – the delegate can easily be changed at run-time. But unlike inheritance, delegation is not directly supported by most popular object-oriented languages, and it doesn’t facilitate dynamic polymorphism.
As a simple example, consider the relationship between a Rectangle class and a Window class. With inheritance, a Window class would inherit its rectangular properties from class Rectangle. With delegation, a Window object would maintain a reference or pointer to a Rectangle object, and calls to rectangle-like methods of the Window object would be delegated to corresponding methods of the Rectangle object.
Now let’s consider a slightly more complex example. Suppose employees can classified based on how they are paid; e.g., hourly or salaried. Using inheritance, we might design three classes: an Employee class which encapsulates the functionality common to all employees, and two subclasses HourlyEmployee and SalariedEmployee which encapsulates pay-specific details. While this design might be suitable for some applications, we would encounter problems in a scenario where a person changes, say from hourly to salaried. The class of an object and the inheritance relationship are both static, and objects can’t change their class easily (but see the State pattern for tips on how to fake it).
A more flexible design would involve delegation – an Employee object could delegate pay-related method calls to an object whose responsibilities focused solely on how the employee is paid. In fact, we might still use inheritance here in a slightly different manner by creating an abstract class (or interface) called PayClassification with two subclasses HourlyPayClassification and SalariedPayClassification which implement classification-specific computations. Using delegation as shown, it would be much easier to change the pay classification of an existing Employee object.
This second example illustrates an important point: In implementing delegation, we often want the capability to replace the delegate with another object, possibly of a different class. Therefore delegation will often use inheritance and polymorphism, with classes of potential delegates being subclasses of an abstract class which encapsulates general delegate responsibilities.
One final point. Sometimes, the choice between delegation and inheritance is driven by external factors such as programming language support for multiple inheritance or design constraints requiring polymorphism. Consider threads in Java. You can associate a class with a thread in one of two ways: either by extending (inheriting) directly from class Thread, or by implementing the Runnable interface and then delegating to a Thread object. Often the approach taken is based on the restriction in Java that a class can only extend one class (i.e., Java does not support multiple inheritance). If the class you want to associate with a thread already extends some other class in the design, then you would have to use delegation; otherwise, extending class Thread would usually be the simpler approach.
10) Which patterns were used by Sun in designing the Enterprise JavaBeans model?
Many design patterns were used in EJB, and some of them are clearly identifiable by their naming convention. Here are several:
Factory Method: Define a interface for creating classes, let a subclass (or a helper class) decide which class to instantiate.
This is used in EJB creation model. EJBHome defines an interface for creating the EJBObject implementations. They are actually created by a generated container class. See InitialContextFactory interface that returns an InitialContext based on a properties hashtable.
Singleton: Ensure a class has only one instance, and provide a global point of access to it.
There are many such classes. One example is javax.naming.NamingManager
Abstract Factory: Provide an interface for creating families of relegated or dependent objects without specifying their concrete classes.
We have interfaces called InitialContext, InitialContextFactory. InitialContextFactory has methods to get InitialContext.
Builder: Separate the construction of a complex factory from its representation so that the same construction process can create different representations.
InitialContextFactoryBuilder can create a InitialContextFactory.
Adapter: Convert the interface of a class into another interface clients expect.
In the EJB implementation model, we implement an EJB in a class that extends SessionBean or a EntityBean. We don’t directly implement the EJBObject/home interfaces. EJB container generates a class that adapts the EJBObject interface by forwarding the calls to the enterprise bean class and provides declarative transaction, persistence support.
Proxy: Provide a surrogate for other object to control access to it.
We have remote RMI-CORBA proxies for the EJB’s.
Memento: Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.
Certainly this pattern is used in activating/passivating the enterprise beans by the container/server.
11) What is an analysis pattern?
An analysis pattern is a software pattern not related to a language or implementation problem, but to a business domain, such as accounting or health care. For example, in health care, the patient visit activity would be subject to a number of patterns.
There is a good overview from an OOPSLA ’96 presentation at http://www.jeffsutherland.org/oopsla96/fowler.html
A good text would be: Martin Fowler’s Martin Analysis Patterns : Reusable Object Models, ISBN: 0201895420, published by Addison-Wesley.
In summary, analysis patterns are useful for discovering and capturing business processes.
12) What are the differences between analysis patterns and design patterns?
Analysis pattern are for domain architecture, and design pattern are for implementation mechanism for some aspect of the domain architecture. In brief, analysis pattern are more high level and more (end-user) functional oriented.
13) How does “Extreme Programming” (XP) fit with patterns?
Extreme Programming has a large emphasis on the concept of refactoring: Writing code once and only once.
Patterns, particularly the structural patterns mentioned by the Gang of Four, can give good pointers about how to acheive that goal.
(XP states when and where factoring should happen, patterns can show you how.)
14) What is the disadvantage of using the Singleton pattern? It is enticing to use this pattern for all the classes as it makes it easy to get the reference of the singleton object.
The intent of the Singleton pattern is to ensure a class has only one instance and to provide a global point of access to it. True, the second part about providing a global point of access is enticing, but the primary disadvantage of using the Singleton pattern for all classes is related to the first part of the intent; i.e., that it allows only one instance of the class. For most classes in an application, you will need to create multiple instances. What purpose would a Customer class serve if you could create only one Customer object?
15) How do you write a Thread-Safe Singleton?
I have written plenty of non-thread-safe Singletons but it wasn’t until recently when I tracked it down that I realized that thread-safety could be a big problem.
The problem is that in the typical Singleton implementation (at least the ones I’ve seen) there is the ability to create multiple versions of the single instance…I know, “But How?”.
Well, in the getInstance() call the instance is checked for null, and then immediately constructed if it is null, and then the instance is returned.
The problem is that the thread (Ta) making the call could swap-out immediately after checking for a null. A subsequent thread (Tb) could then make a call to get the instance and construct an instance of the Singleton. When the original thread (Ta) is then swapped back in, it would construct and return a completely separate object. BAD KITTY!
The following code snippet shows an example of a thread-safe Singleton.
package com.jgk.patterns.singleton;
public class JGKSingleton {
/* Here is the instance of the Singleton */
private static JGKSingleton instance_;
/* Need the following object to synchronize */
/* a block */
private static Object syncObject_;
/* Prevent direct access to the constructor
private JGKSingleton() { super(); }
public static JGKSingleton getInstance() {
/* in a non-thread-safe version of a Singleton */
/* the following line could be executed, and the */
/* thread could be immediately swapped out */
if (instance_ == null) {
synchronized(syncObject_) {
if (instance_ == null) {
instance_ = new JGKSingleton();
}
}
}
return instance_;
}
}
NOTE: The 2nd check for if (instance_ == null) is needed to avoid making another unnecessary construct.
Don’t let this byte you!
16) What is the Reactor pattern?
The new book “Pattern-oriented Software Architecture Volume 2″ ISBN 0471606952 has a chapter on the Reactor pattern. It falls under the general category of “Event Handling Patterns”. To quote the leading bit of the chapter,
“The Reactor architectural pattern allows event-driven applications to demultiplex and dispatch service requests that are delivered to an application from one or more clients”
It is used in a synchronous manner, so that if the callback you delegate the event to takes a while to complete you will run into problems with scalability.
17) What are Process Patterns?
Basically process patterns define a collection of best practices, techniques, methods for developing object-oriented software.
18) How and where did the concept of design patterns get started?
Work on patterns has been influenced by the works of Christopher Alexander who published on topics related to urban planning and building architecture in the late 1970s. The history of patterns for software design began in the late 1980s and reached an important milestone with the publishing of the first book fully dedicated to this subject by the “Gang of Four”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Design Patterns – Elements of Reusable Object-Oriented Software). In conjunction, the emergence of object-oriented software development fostered the work on other topics related to design patterns such as application frameworks, analysis patterns, language idioms, and so on.
19) Where can I find good examples of the Prototype pattern?
The prototype pattern is actually quite simple to implement in Java.
Recall that the idea of prototype is that you are passed an object and use that object as a template to create a new object. Because you might not know the implementation details of the object, you cannot create a new instance of the object and copy all of its data. (Some of the data may not be accessible via methods). So you ask the object itself to give you a copy of itself.
Java provides a simple interface named Cloneable that provides an implementation of the Prototype pattern. If you have an object that is Cloneable, you can call its clone() method to create a new instance of the object with the same values.
The trick here is that you must override the clone() method to increase its visibility, and just call super.clone(). This is because the implementation of clone(), defined in java.lang.object, is protected. For example:
public class CopyMe implements Cloneable {
public Object clone() {
return super.clone();
}
}
Note that Cloneable is an empty interface! It merely acts as a tag to state that you really want instance of the class to be cloned. If you don’t implement Cloneable, the super.clone() implementation will throw a CloneNotSupportedException.
The Object implementation of clone() performs a shallow copy of the object in question. That is, it copies the values of the fields in the object, but not any actual objects that may be pointed to. In other words, the new object will point to the same objects the old object pointed to.
As an example of using the cloning:
CopyMe thing = new Copyme();
CopyMe anotherThing = (Copyme)thing.clone();
This example is pretty trivial, but the real power comes when you don’t know what you’re actually cloning.
For example, suppose you define an interface that represents a customer:
public interface Customer extends Cloneable {
public Object clone();
// require making it public! public String getName();
public void setName(String name); ... }
You might have several different implementations of this interface, possibly storing data in a file, database, or using EJB Entity beans. If a shallow copy of the data is sufficient to represent a copy of the object, Java’s clone() method works great.
You might have a method that needs to make a copy of the data to store it in a Hashtable, for example:
public void storeCustomer(Customer customer) {
Customer copy = (Customer)customer.clone();
dataStore.put(copy);
}
Note that this method knows nothing about what type of customer we’re getting. This pattern will work for any actual type of Customer, no matter how the data is stored. For example:
FileBasedCustomer c1 = new FileBasedCustomer(...);
RDBMSBasedCustomer c2 = new RDBMSBasedCustomer(...);
EJBBasedCustomer c3 = new EJBBasedCustomer(...);
manager.storeCustomer(c1);
manager.storeCustomer(c2);
manager.storeCustomer(c3);
20) What are Anti-Patterns?
There isn’t really a “clean-cut” definition out there just yet, unlike Design Patterns. Basically, as Design Patterns (and more particularly, Process Patterns) try to codify a standard vocabulary for working solutions to problems that reappear frequently, Anti-Patterns represent an effort to define and classify reoccuring non-solutions, i.e., things that lots of projects do that fail to yield a solution, or actually prevent a project from working or being finished.
The most basic example I can think of is “The Hammer”, inspired by the old addage, “If your only tool is a hammer, everything looks like a nail” (or the variation, “If your only tool is a hammer, everything looks like your left thumb.” Hammer describes a regular, reoccuring problem in inexperienced engineers (particularly those who’ve only used one language for the bulk of their carreer so far), that of trying to force-feed all problems into the solutions they already know.
21) What patterns are particularly useful in building networked applications?
I suggest starting with Pattern-Oriented Software Architecture: Patterns for Concurrent and Networked Objects (POSA2). POSA2 specifically brings together 17 interrelated patterns addressing Service Access and Configuration, Event Handling, Synchronization, and Concurrency.
The patterns and many of the examples in POSA2 come primarily from the design and implementation of the ACE framework.
22) Are there any good Java-specific patterns books available?
The Java-specific patterns books are:
Java Design Patterns: A Tutorial
Patterns in Java, Volume 1
Patterns in Java, Volume 2
Concurrent Programming in Java , Second Edition: Design Principles and Patterns
SanFrancisco Design Patterns
As far as the good part of the question…. Doug Lea’s Concurrent Programming book is probably the best of the bunch. However, its patterns are specific to concurrent programming. Many people don’t like the quality of Mark Grand’s two Patterns in Java books. [They are rated 3 and 2 stars at Amazon, respectively]. The first printing of the Cooper tutorial book was riddled with errors. If you get that, be sure to get at least the second printing. [Look on last line of page before TOC.] The SanFrancisco book is definitely good, but I’m not sure how good if you aren’t using SF.
23) What are Collaboration Patterns?
Collaboration Patterns are repeatable techniques used by teams of people to help them work together (collaborate). Ellen Gottesdiener of EBG Consulting has created these patterns in order to help facilitate good teamwork. These patterns really have nothing to do with object-oriented development or Java, besides the fact that they can help with requirements gathering or CRC design sessions.
In a nutshell, Collaboration Patterns are techniques to help make meetings useful.
24) Is it correct from a design point of view to make an object both an Observer and Observable at the same time?
Yes, and this can be the preferred pattern in some cases.
For example, suppose you were writing a supply chain management system for a retail chain. Each store object in your system generates item-sold events; when the chain generates enough of these for a particular product, a buyer object generates a purchase order for more of the product. However, the buyer object has no particular interest in individual item sold events. Instead, the buyer (Observer) registers to receive out-of-stock events from the warehouse (Observable); the warehouse, as Observer, registers with the individual stores (Observables) to receive item-sold events. Thus, the warehouse is both Observer and Observable. (Please note that this is a synthetic example, and probably not the way to organize a supply chain.)
Another reason to use one or more central Observer-Observable object in between ultimate Observers and Observables is to fully decouple the two. In some cases, Observer and Observable may exist on different machines, and may rely on the central Observer-Observable to hide this complication.
A good source for more details is the Publisher-Subscriber section of Buschmann et al., Pattern-Oriented Software Architecture: A System of Patterns.
25) How can I maintain a single instance of an object in an applet?
In start(), instead of always creating a new object, return the existing one if it exists or create a new one if it doesn’t.
26) What is the best way to generate a universally unique object ID? Do I need to use an external resource like a file or database, or can I do it all in memory?
I need to generate unique id’s that will be used for node ‘ID’ attribute values within XML documents. This id must be unique system-wide. The generator must be available to a number of servlets that add various node structures to my XML docs as a service. What is the best way to tackle this? The ‘possible’ ways I can see:
Keep the maximum ID value in a flat-file where the service would read it upon start-up and increment it. Upon shutdown or failure, it would write the latest max id to the file.
Calculate the max id by searching the XML itself. This will be tougher since XML requires an alpha-numeric value (not strictly numeric).
Use a database (MySQL) with a two-field table where one field is the incremental counter.
There is an additional way to do that that doesn’t rely on an external file (or database) like the one you have presentred. If has been presented in the EJB Design Patterns book, written by Floyd Marinescu, and available in a pdf format for free from the given link.
The suggested solution is based on the UUID for EJB pattern, that comes out from this question:
How can universally unique primary keys can be generated in menory without requiring a database or a singleton?
Without enetring in the specifics (you can fully check out the pattern by reading the appropriate chapter), the solution is to generate a 32 digit key, encoded in hexadecimal composed as follows:
Unique down to the millisecond. Digits 1-8 are are the hex encoded lower 32 bits of the System.currentTimeMillis() call.
Unique across a cluster. Digits 9-16 are the encoded representation of the 32 bit integer of the underlying IP address.
Unique down to the object in a JVM. Digits 17-24 are the hex representation of the call to System.identityHashCode(), which is guaranteed to return distinct integers for distinct objects within a JVM.
Unique within an object within a millisecond. Finally digits 25-32 represent a random 32 bit integer generated on every method call using the cryptographically strong java.security.SecureRandom class.
27) Is there some kind of Design pattern which would make it possible to use the Same code base in EJB and non EJB context?
A good suggestion would be using Delegation
class PieceOfCode {
public Object myMethod() {}
}
class EJBImpl ... {
PieceOfCode poc = new PieceOfCode();
public Object myMethod() {
return poc.myMethod();
}
}
This should not be a violation of EJB specs, since EJBs can use simple java classes for their use. Think about Dependant Objects and so on.
28) What is session facade?
Session facade is one design pattern that is often used while developing enterprise applications.
It is implemented as a higher level component (i.e.: Session EJB), and it contains all the iteractions between low level components (i.e.: Entity EJB). It then provides a single interface for the functionality of an application or part of it, and it decouples lower level components simplifying the design.
Think of a bank situation, where you have someone that would like to transfer money from one account to another.
In this type of scenario, the client has to check that the user is authorized, get the status of the two accounts, check that there are enough money on the first one, and then call the transfer. The entire transfer has to be done in a single transaction otherwise is something goes south, the situation has to be restored.
As you can see, multiple server-side objects need to be accessed and possibly modified. Multiple fine-grained invocations of Entity (or even Session) Beans add the overhead of network calls, even multiple transaction. In other words, the risk is to have a solution that has a high network overhead, high coupling, poor reusability and mantainability.
The best solution is then to wrap all the calls inside a Session Bean, so the clients will have a single point to access (that is the session bean) that will take care of handling all the rest.
Obviously you need to be very careful when writing Session Facades, to avoid the abusing of it (often called “God-Bean”).
For a detailed description of this pattern, check this page: Core J2EE Patterns – Session Facade or get Floyd Marinescu’s EJB Design Patterns, in PDF format.
29) How is JDO different from VO ?
JDO is a persistence technology that competes against entity beans in enterprise application development. It allows you to create POJOs (plain old java objects) and persist them to the database – letting JDO take care of the storage.
Value objects, on the other hand, represent an abstract design pattern used in conjuction with entity beans, jdbc, and possibly even JDO to overcome commonly found isolation and transactional problems in enterprise apps. Value objects alone do not allow you to persist objects – they are simple data holders used to transfer data from the database to the client and back to the database.
Side note: I know that many books out there still refer to these data holders as value objects but the correct term is DTO: data transfer objects. Value objects refer to objects that hold a value. A good example of this java.lang.Integer object which holds an int.
30) How can I implement the MVC design pattern using JSP?
The MVC (Model View Controller) design pattern is a pattern/architecture that can be used by GUI’s. It seperates the application’s data, user interface and control logic into three separate entities. This ensures the system will be more maintainable in the future as changes to one component will not have an affect on the others.
The MVC pattern also conforms with the JSP Model 2 architecture.
The MVC pattern can be easily implemented in web applications using JSTL core, JSP, Servlets and JavaBeans.
JSTL makes it easy for HTML designers to use dynamic data in their pages without having to learn in depth java. The tags are in a recognizable HTML like format meaning a smaller learning curve than taking on the JSP specification after taking on the Java specification.
JSTL also makes it easy for web developers who are developing all aspects of the application in helping you keep the content separate from the display by keeping your JSP clean from any Java code. Since JSTL and its EL (expression Language) are really only suited for accessing data, it forces you to use a MVC pattern so long as you keep all JSP and Java syntax/code out of the JSP pages.
A common scenario might look like this, a user sends a request to a server. The request is handled by a Servlet (the controller) which will initialize any JavaBeans (the model) required to fulfill the user’s request. The Servlet (the controller) will then forward the request, which contains the JavaBeans (the model), to a JSP (the view) page which contains only HTML and JSTL syntax. The JSTL will format the data into a human readable format before being sent back to the user. Thus completing the full MVC process.
JSTL also has a tag library for XML processing, this could be used in an MVC environment as you could replace the JavaBeans with an XML document to describe your data, so long as the XML is still constructed in a controller such as a Servlet before being passed to the JSP (the view).
JSTL also has a tag library for database access, using this in your JSP (the view) pages would NOT comply to the MVC pattern as there will be no separation between the model and the view.
1. Give an example where you prefer abstract class over interface ?
This is common but yet tricky design interview question. both interface and abstract class follow "writing code for interface than implementation" design principle which adds flexibility in code, quite important to tackle with changing requirement. here are some pointers which help you to answer this question:
1. In Java you can only extend one class but implement multiple interface. So if you extend a class you lost your chance of extending another class.
2. Interface are used to represent adjective or behavior e.g. Runnable, Clonable, Serializable etc, so if you use an abstract class to represent behavior your class can not be Runnable and Clonable at same time because you can not extend two class in Java but if you use interface your class can have multiple behavior at same time.
3. On time critical application prefer abstract class is slightly faster than interface.
4. If there is a genuine common behavior across the inheritance hierarchy which can be coded better at one place than abstract class is preferred choice. Some time interface and abstract class can work together also where defining function in interface and default functionality on abstract class.
2. Design a Vending Machine which can accept different coins, deliver different products?
This is an open design question which you can use as exercise, try producing design document, code and Junit test rather just solving the problem and check how much time it take you to come to solution and produce require artifacts, Ideally this question should be solve in 3 hours, at least a working version.
3. You have a Smartphone class and will have derived classes like IPhone, AndroidPhone, WindowsMobile Phone can be even phone names with brand, how would you design this system of Classes.
This is another design pattern exercise where you need to apply your object oriented design skill to come with a design which is flexible enough to support future products and stable enough to support changes in existing model.
4. When do you overload a method in Java and when do you override it ?
Rather a simple question for experienced designer in Java. if you see different implementation of a class has different way of doing certain thing than overriding is the way to go while overloading is doing same thing but with different input. method signature varies in case of overloading but not in case of overriding in java.
5. Design ATM Machine ?
We all use ATM (Automated Teller Machine) , Just think how will you design an ATM ? for designing financial system one must requirement is that they should work as expected in all situation. so no matter whether its power outage ATM should maintain correct state (transactions), think about locking, transaction, error condition, boundary condition etc. even if you not able to come up exact design but if you be able to point out non functional requirement, raise some question , think about boundary condition will be good progress.
6. You are writing classes to provide Market Data and you know that you can switch to different vendors overtime like Reuters, wombat and may be even to direct exchange feed , how do you design your Market Data system.
This is very interesting design interview question and actually asked in one of big investment bank and rather common scenario if you have been writing code in Java. Key point is you will have a MarketData interface which will have methods required by client e.g. getBid(), getPrice(), getLevel() etc and MarketData should be composed with a MarketDataProvider by using dependency injection. So when you change your MarketData provider Client won't get affected because they access method form MarketData interface or class.
7. Why is access to non-static variables not allowed from static methods in Java
You can not access non-static data from static context in Java simply because non-static variables are associated with a particular instance of object while Static is not associated with any instance. You can also see my post why non static variable are not accessible in static context for more detailed discussion.
8. Design a Concurrent Rule pipeline in Java?
Concurrent programming or concurrent design is very hot now days to leverage power of ever increasing cores in advanced processor and Java being a multi-threaded language has benefit over others. Do design a concurrent system key point to note is thread-safety, immutability, local variables and avoid using static or instance variables. you just to think that one class can be executed by multiple thread a same time, So best approach is that every thread work on its own data, doesn't interfere on other data and have minimal synchronization preferred at start of pipeline. This question can lead from initial discussion to full coding of classes and interface but if you remember key points and issues around concurrency e.g. race condition, deadlock, memory interference, atomicity, ThreadLocal variables etc you can get around it.
1. What is design patterns ? Have you used any design pattern in your code ?
Design patterns are tried and tested way to solve particular design issues by various programmers in the world. Design patterns are extension of code reuse.
2. Can you name few design patterns used in standard JDK library?
Decorator design pattern which is used in various Java IO classes, Singleton pattern which is used in Runtime , Calendar and various other classes, Factory pattern which is used along with various Immutable classes likes Boolean e.g. Boolean.valueOf and Observer pattern which is used in Swing and many event listener frameworks.
3. What is Singleton design pattern in Java ? write code for thread-safe singleton in Java
Singleton pattern focus on sharing of expensive object in whole system. Only one instance of a particular class is maintained in whole application which is shared by all modules. Java.lang.Runtime is a classical example of Singleton design pattern. You can also see my post 10 questions on Singleton pattern in Java for more questions and discussion. From Java 5 onwards you can use enum to thread-safe singleton.
4. What is main benefit of using factory pattern ? Where do you use it?
Factory pattern’s main benefit is increased level of encapsulation while creating objects. If you use Factory to create object you can later replace original implementation of Products or classes with more advanced and high performance implementation without any change on client layer. See my post on Factory pattern for more detailed explanation and benefits.
5. What is observer design pattern in Java
Observer design pattern is based on communicating changes in state of object to observers so that they can take there action. Simple example is a weather system where change in weather must be reflected in Views to show to public. Here weather object is Subject while different views are Observers. Look on this article for complete example of Observer pattern in Java.
6. Give example of decorator design pattern in Java ? Does it operate on object level or class level ?
Decorator pattern enhances capability of individual object. Java IO uses decorator pattern extensively and classical example is Buffered classes like BufferedReader and BufferedWriter which enhances Reader and Writer objects to perform Buffer level reading and writing for improved performance. Read more on Decorator design pattern and Java
What are design patterns?
A pattern is a proven (and recurring) solution to a problem in a context. Each pattern describes a problem which occurs time and again in our environment, and describes its solution to this problem in such a way that we can use this solution any number of times. In simple words, there are a lot of common problems which a lot of developers have faced over time. These common problems ideally should have a common solution too. It is this solution when documented and used over and over becomes a design pattern.
Can we always apply the same solution to different problems at hand?
No. Design patterns would study the different problems at hand. To these problems then it would suggest different design patterns to be used. However, the type of code to be written in that design pattern is solely the discretion of the Project Manager who is handling that project.
What should be the level of detail/abstraction which should be provided by a design pattern?
Design patterns should present a higher abstraction level though it might include details of the solution. However, these details are lower abstractions and are called strategies. There may be more than one way to apply these strategies in implementing the patterns. The details of implementation are usually upto the developer who is coding at the ground level.
What are the most common problems which one faces during the application design phase that are solved by design patterns?
Some are:
1. Identifying components, internal structures of the components, and relationships between components.
2. Determining component granularity and appropriate interactions
3. Defining component interfaces.
How does one decide which Design pattern to use in our application?
We need to follow these steps:
1. We need to understand the problem at hand. Break it down to finer grained problems. Each design pattern is meant to solve certain kinds of problems. This would narrow down our search for design patterns.
2. Read the problem statement again along with the solution which the design pattern will provide. This may instigate to change a few patterns that we are to use.
3. Now figure out the interrelations between different patterns. Also decide what all patterns will remain stable in the application and what all need to change (with respect to Change Requests received from the clients).
What is Refactoring?
Learning different design patterns is not sufficient to becoming a good designer. We have to understand these patterns and use them where they have more benefits. Using too many patterns (more than required) would be over-engineering and using less design patterns than required would be under-engineering. In both these scenarios we use refactoring. Refactoring is a change made to the internal structure of the software to make it easier to understand and cheaper to modify, without changing its observable behaviour.
What are Antipatterns?
Though the use of patterns fulfils our objectives in the applications; there are also several instances where several applications did not fulfill their goals. The architects of these applications too need to document these wrong decisions. This helps others by preventing them from repeating these mistakes in their applications. Such documented mistakes are called antipatterns.
As we do development in tiers, how do we divide patterns in tiers?
The Sun Java Center has classified the patterns in three tiers. These are:
Presentation tier patterns for web-component tier,
Business tier patterns for business logic (EJB) tier, and
Integration tier patterns for connection to the databases.
What are the Presentation Tier Patterns?
The presentation tier patterns are:
Intercepting filter, Front Controller, View Helper, Composite View, Service-to-Worker, and Dispatcher View.
What are the Business Tier Patterns?
The business tier patterns are:
Business delegate, Value Object, Session Façade, Composite Entity, Value Object Assembler, Value List Handler, and Service Locator.
What are the Integration Tier Patterns?
Integration tier patterns are:
Data Access Object (DAO) and Service Activator
What is Intercepting Filter pattern?
Provides a solution for pre-processing and post-processing a request. It allows us to declaratively apply filters for intercepting requests and responses. For ex. Servlet filters.
What is Front Controller pattern?
It manages and handles requests through a centralized code. This could either be through a servlet or a JSP (through a Java Bean). This Controller takes over the common processing which happens on the presentation tier. The front controller manages content retrieval, security, view management and retrieval.
What is View Helper pattern?
There generally are two parts to any application – the presentation and the business logics. The “View” is responsible for the output-view formatting whereas “Helper” component is responsible for the business logic. Helper components do content retrieval, validation and adaptation. Helper components generally use Business delegate pattern to access business classes.
What is Composite View pattern?
This pattern is used for creating aggregate presentations (views) from atomic sub-components. This architecture enables says piecing together of elementary view components which makes the presentation flexible by allowing personalization and customization.
What is Service to Worker pattern?
This is used in larger applications wherein one class is used to process the requests while the other is used to process the view part. This differentiation is done for maintainability.
What is Dispatcher View pattern?
This is similar to Service to Worker pattern except that it is used for smaller applications. In this one class is used for both request and view processing.
What is Business Delegate pattern?
This pattern is used to reduce the coupling between the presentation and business-logic tier. It provides a proxy to the façade from where one could call the business classes or DAO class. This pattern can be used with Service Locator pattern for improving performance.
What is Value Object pattern?
Value Object is a serializable object which would contain lot of atomic values. These are normal java classes which may have different constructors (to fill in the value of different data) and getter methods to get access to these data. VOs are used as a course grained call which gets lots of data in one go (this reduces remote overhead). The VO is made serializable for it to be transferred between different tiers within a single remote method invocation
What is Session Façade pattern?
This pattern hides the complexity of business components and centralizes the workflow. It provides course-grained interfaces to the clients which reduces the remote method overhead. This pattern fits well with declarative transactions and security management.
What is Value Object Assembler pattern?
This pattern allows for composing a Value Object from different sources which could be EJBs, DAOs or Java objects.
What is Value List Handler pattern?
This pattern provides a sound solution for query execution and results processing.
What is Service Locator pattern?
It provides a solution for looking-up, creating and locating services and encapsulating their complexity. It provides a single point of control and it also improves performance.
What is Data Access Object pattern?
It provides a flexible and transparent access to the data, abstracts the data sources and hides the complexity of Data persistence layer. This pattern provides for loose coupling between business and data persistence layer.
What is EJB Command pattern?
Session Façade and EJB Command patterns are competitor patterns. It wraps business logic in command beans, decouples the client and business logic tier, and reduces the number of remote method invocations.
What is Version Number pattern?
This pattern is used for transaction and persistence and provides a solution for maintaining consistency and protects against concurrency. Every time a data is fetched from the database, it comes out with a version number which is saved in the database. Once any update is requested on the same row of the database, this version is checked. If the version is same, the update is allowed else not.
What all patterns are used to improve performance and scalability of the application?
Value Object, Session Façade, Business Delegate and Service Locator.
What design patterns could be used to manage security?
Single Access Point, Check point and Role patterns
Core Java Interview Questions Answers in Finance domain
1. What is immutable object? Can you write immutable object?
You need to make class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.
2. Does all property of immutable object needs to be final?
Not necessary as stated above you can achieve same functionality by making member as non final but private and not modifying them except in constructor.
3. What is the difference between creating String as new () and literal?
When we create string with new () it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap.
String s = new String("Test");
will put the object in String pool , it it does then why do one need String.intern() method which is used to put Strings into String pool explicitly. its only when you create String object as String literal e.g. String s = "Test" it put the String in pool.
4. How does substring () inside String works?
Another good question, I think answer is not sufficient but here it is “Substring creates new object out of string by taking a portion of original string”.
The substring() method is used to return a part (or substring) of the String used to invoke the method. The first argument represents the starting location (zero-based) of the substring. If the call has only one argument, the substring returned will include the characters to the end of the original String. If the call has two arguments, the substring returned will end with the character located in the nth position of the original String where n is the second argument. Unfortunately, the ending argument is not zero-based, so if the second argument is 7, the last character in the returned String will be in the original String’s 7 position, which is index 6. Let’s look at an example:
String x = "0123456789";
System.out.println( x.substring(5) ); // output is "56789"
System.out.println( x.substring(5, 8)); // output is "567"
The first example should be easy: start at index 5 and return the rest of the String. The second example should be read as follows: start at index 5 and return the characters up to and including the 8th position (index 7).
and @Anonymous pointed out some interesting fact:
omething important about String.substring() method, its implementation calls the following String(...) constructor :
// Package private constructor which shares value array for speed.
String(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}
That means the new String() object returned by substring(...) shares the same backing array (this.value) as the original string object.
Thus if your original string object is 1GB long, the substring object will always be 1GB long too!
You will probably want to trim the new array size to the substring range, this should do the job:
String veryLongString = readFromFile(file);
String tinyString = new String(veryLongString.substring(1,10));
The String(String) constructor is implemented that way:
public String(String original) {
...
if (originalValue.length > size) {
// The array representing the String is bigger than the new
// String itself. Perhaps this constructor is being called
// in order to trim the baggage, so make a copy of the array.
int off = original.offset;
v = Arrays.copyOfRange(originalValue, off, off+size);
}
...
}
5. Which two method you need to implement for key in hashMap ?
(equals and hashcode) read How HashMap works in Java for detailed explanation.
6. Where does these two method comes in picture during get operation?
See here How HashMap works in Java for detailed explanation.
7. How do you handle error condition while writing stored procedure or accessing stored procedure from java?
Open for all, my friend didn't know the answer so he didn't mind telling me.
8. What is difference between Executor.submit() and Executer.execute() method ?
There is a difference when looking at exception handling. If your tasks throws an exception and if it was submitted with execute this exception will go to the uncaught exception handler (when you don't have provided one explicitly, the default one will just print the stack trace to System.err). If you submitted the task with submit any thrown exception, checked or not, is then part of the task's return status. For a task that was submitted with submit and that terminates with an exception, the Future.get will rethrow this exception, wrapped in an ExecutionException.
9. What is the difference between factory and abstract factory pattern?
Open for all, he explains about factory pattern and how factory pattern saves maintenance time by encapsulating logic of object creation but didn't know exact answer
Abstract Factory provides one more level of abstraction. Consider different factories each extended from an Abstract Factory and responsible for creation of different hierarchies of objects based on the type of factory. E.g. AbstractFactory extended by AutomobileFactory, UserFactory, RoleFactory etc. Each individual factory would be responsible for creation of objects in that genre.
11. Can you write Critical section code for singleton?
check here 10 Interview questions on Singleton Pattern in Java
12. Can you write code for iterating over hashmap in Java 4 and Java 5 ?
Tricky one but he managed to write using while and for loop.
13. When do you override hashcode and equals() ?
Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap. check this for writing equals method correctly 5 tips on equals in Java
14. What will be the problem if you don't override hashcode() method ?
You will not be able to recover your object from hash Map if that is used as key in HashMap.
See here How HashMap works in Java for detailed explanation.
15. Is it better to synchronize critical section of getInstance() method or whole getInstance() method ?
Answer is critical section because if we lock whole method than every time some one call this method will have to wait even though we are not creating any object)
16. What is the difference when String is gets created using literal or new() operator ?
When we create string with new() its created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap.
17. Does not overriding hashcode() method has any performance implication ?
This is a good question and open to all , as per my knowledge a poor hashcode function will result in frequent collision in HashMap which eventually increase time for adding an object into Hash Map.
18. What’s wrong using HashMap in multithreaded environment? When get() method go to infinite loop ?
Another good question. His answer was during concurrent access and re-sizing.
19. Give a simplest way to find out the time a method takes for execution without using any profiling tool?
Read the system time just before the method is invoked and immediately after method returns. Take the time difference, which will give you the time taken by a method for execution.
To put it in code…
long start = System.currentTimeMillis ();
method ();
long end = System.currentTimeMillis ();
System.out.println (“Time taken for execution is ” + (end – start));
Remember that if the time taken for execution is too small, it might show that it is taking zero milliseconds for execution. Try it on a method which is big enough, in the sense the one which is doing considerable amout of processing
.
How to choose design patterns
1) Flyweight: As discussed above, it’s a part of JDK and hence is used by every Java program.
2) Iterator: All collection classes provide the feature of an iterator to iterate their elements. Moreover, I have also seen custom classes in my projects where the feature of iterator is custom built for those classes.
3) DAO (Data Access Object) : This is also one of the common design pattern seen in many applications which use SQL commands to interact with the datasource/database. Usually a DAO class has methods which invoke the SQL queries for CRUD operations on the database tables thus avoiding the need to write and maintain SQL queries in multiple classes for the same database tables.
4) DTO (Data Transfer Object) : This design pattern represents the data which is actually stored in the database tables in terms of Java classes and is accepted as argument by various methods of the DAO classes. This design pattern usually translates to POJO classes corresponding to database tables and their columns.
5) Factory: If you have used DAO then it is very likely that you have also seen factory design pattern being used as a factory class is written for generating the DAO’s.
6) Abstract Factory: This design pattern represents a factory of factories. When you have many closely related factory classes, you will write an interface which all those factories implement and thus can be generically referenced. You will write a class which will have methods to return the appropriate factory depending upon the need.
7) MVC: This is also one of the famous design patterns and should not be missed in Java interview.
8) Front Controller: This design pattern is closely associated with MVC design pattern and is used to write the single point of contact for any request that comes from the users. The controller then dispatches the request to appropriate handler depending upon the arguments being passed along with the request.
9) Singleton: Though Singleton design pattern looks to be easy to understand and get away with any Java interview but one can be grilled like anything on this design pattern. There is so much to ask on Singleton that a candidate can be judged only by asking questions on Singleton. This is because the interviewer can associate Inheritance, Reflection, Serialization, Collections, Polymorphism, Cloning and other Java concepts with Singleton.
Still if you were to choose only five design patterns for an interview due to time constraints then I would advise the following list:
1) Singleton
2) DAO (Data Access Object)
3) DTO (Data Transfer Object)
4) MVC (Model View Controller)
5) Factory