1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

OOPs and Design Pattern interview questions

  • What is Observer pattern ? Explain with Code Example ? view answer
  • "is-a" relationship describes inheritance relationship between objects view answer
  • has-a relationship, which is also called object composition view answer
  • The four main concepts are involved in OOP view answer
  • Example Java Code For Association & Aggregation & Composition view answer
  • What are the common things need to consider while Creating and Destroying Objects view answer
  • what is the difference between encapsulation and data hiding? explain with Code example? view answer
  • Dependency In Java view answer
  • Is Singleton class is serialized ? view answer
  • What is Singleton ? and Double-checked locking ? view answer
  • What is Factory Method pattern? view answer
  • What is Adapter pattern ? view answer
  • What is Proxy and Decorator patterns ? With Java Example ? view answer
  • Brief Introduction to OOP Concepts view answer
  • What is Abstract Factory Pattern ? With Code Example ? view answer

!!! OOPs and Design Pattern interview questions !!!

What is Observer pattern ? Explain with Code Example ?



Observer is a very common pattern. You typically use this pattern when you're implementing
an application with a Model/View/Controller architecture. The Model/View part of this design
is intended to decouple the presentation of data from the data itself.
Consider, for example, a case where data is kept in a database and can be displayed in
multiple formats, as a table or a graph. The Observer pattern suggests that the display
classes register themselves with the class responsible for maintaining the data, so they can
be notified when the data changes, and so they can update their displays.
The Java API uses this pattern in the event model of its AWT/Swing classes. It also provides
direct support so this pattern can be implemented for other purposes.


The Java API provides an Observable class that can be subclassed by objects that want to
be observed. Among the methods Observable provides are:
* addObserver(Observer o) is called by Observable objects to register
themselves.
* setChanged() marks the Observable object as having changed.
* hasChanged() tests if the Observable object has changed.
* notifyObservers() notifies all observers if the Observable object has changed,
according to hasChanged().
To go along with this, an Observer interface is provided, containing a single method that is
called by an Observable object when it changes (providing the Observer has registered
itself with the Observable class, of course):
public void update(Observable o, Object arg)The following example demonstrates how an Observer pattern can be used to notify a
display class for a sensor such as temperature has detected a change:


import java.util.*;

//class which implements Observable class is the class which is observed( observable).
class Sensor extends Observable {
    private int temp = 68;
void takeReading(){
    double d;
    d =Math.random();
    System.out.println(d);
if(d>0.75){
    temp++;
    setChanged();
}
System.out.print("[Temp: " + temp + "]");
}
public int getReading(){
return temp;
}
}


import java.util.Observable;
import java.util.Observer;
// This is observer class
public class Display implements Observer {
public void update(Observable o, Object arg){
System.out.print("New Temp: " + ((Sensor) o).getReading());
}
public static void main(String []ac){
Sensor sensor = new Sensor();
Display display = new Display();
// register observer with observable
sensor.addObserver(display);//

for(int i=0; i < 5; i++){
sensor.takeReading();
// notifyObservers() will call update method of observer.
sensor.notifyObservers();
System.out.println();
}
}
}



The information you are posting should be related to java and ORACLE technology. Not political.