Tuesday, May 8, 2012

CSS FLOAT Demystified

courtesy:http://www.w3.org/TR/2011/REC-CSS2-20110607/visuren.html#floats

9.7 Relationships between 'display', 'position', and 'float'

The three properties that affect box generation and layout — 'display', 'position', and 'float' — interact as follows:
  1. If 'display' has the value 'none', then 'position' and 'float' do not apply. In this case, the element generates no box.
  2. Otherwise, if 'position' has the value 'absolute' or 'fixed', the box is absolutely positioned, the computed value of 'float' is 'none', and display is set according to the table below. The position of the box will be determined by the 'top', 'right', 'bottom' and 'left' properties and the box's containing block.
  3. Otherwise, if 'float' has a value other than 'none', the box is floated and 'display' is set according to the table below.
  4. Otherwise, if the element is the root element, 'display' is set according to the table below, except that it is undefined in CSS 2.1 whether a specified value of 'list-item' becomes a computed value of 'block' or 'list-item'.
  5. Otherwise, the remaining 'display' property values apply as specified.
Specified value Computed value
inline-table table
inline, table-row-group, table-column, table-column-group, table-header-group, table-footer-group, table-row, table-cell, table-caption, inline-block block
others same as specified

Observer Pattern

Observer Pattern

Motivation

We can not talk about Object Oriented Programming without considering the state of the objects. After all object oriented programming is about objects and their interaction. The cases when certain objects need to be informed about the changes occured in other objects are frequent. To have a good design means to decouple as much as possible and to reduce the dependencies. The Observer Design Pattern can be used whenever a subject has to be observed by one or more observers.
Let's assume we have a stock system which provides data for several types of client. We want to have a client implemented as a web based application but in near future we need to add clients for mobile devices, Palm or Pocket PC, or to have a system to notify the users with sms alerts. Now it's simple to see what we need from the observer pattern: we need to separate the subject(stocks server) from it's observers(client applications) in such a way that adding new observer will be transparent for the server.

Intent

  • Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Observer Design Pattern Implementation - UML Class Diagram

Implementation
The participants classes in this pattern are:
  • Observable - interface or abstract class defining the operations for attaching and de-attaching observers to the client. In the GOF book this class/interface is known as Subject.
  • ConcreteObservable - concrete Observable class. It maintain the state of the object and when a change in the state occurs it notifies the attached Observers.
  • Observer - interface or abstract class defining the operations to be used to notify this object.
  • ConcreteObserverA, ConcreteObserver2 - concrete Observer implementations.
The flow is simple: the main framework instantiate the ConcreteObservable object. Then it instantiate and attaches the concrete observers to it using the methods defined in the Observable interface. Each time the state of the subject it's changing it notifies all the attached Observers using the methods defined in the Observer interface. When a new Observer is added to the application, all we need to do is to instantiate it in the main framework and to add attach it to the Observable object. The classes already created will remain unchanged.


Applicability & Examples

The observer pattern is used when:
  • the change of a state in one object must be reflected in another object without keeping the objects tight coupled.
  • the framework we are writing needs to be enhanced in future with new observers with minimal changes.
Some Classical Examples:
  • Model View Controller Pattern - The observer pattern is used in the model view controller (MVC) architectural pattern. In MVC the this pattern is used to decouple the model from the view. View represents the Observer and the model is the Observable object.
  • Event management - This is one of the domains where the Observer patterns is extensively used. Swing and .Net are extensively using the Observer pattern for implementing the events mechanism.


Example - News Agency

Lets' take the example of a news agency. A news agency gather news news and publish them to different subscribers. We need to create a framework for and agency to be able to inform immediately, when event occurs, its subscribers about the event. The subscribers can receive the news in different ways: Emails, SMS, ... The solution need to be extensively enough to support new types of subscribers(maybe new communication technologies will appear).
 Observer Design Pattern Newspublisher Example - UML Class Diagram
Obviously, the agency is represented by an Observable(Subject) class named NewsPublisher. This one is created as an abstract class because the agency want to create several types of Observable objects: in the beginning only for business news, but after some time sport and political new will be published. The concrete class is BusinessNewsPublisher.
The observer logic is implemented in NewsPublisher. It keeps a list of all it subscribers and it informs them about the latest news. The subscribers are represented by some observers (SMSSubscriber, EmailSubscriber). Both the observers mentioned above are inherited from the Subscriber. The subscriber is the abstract class which is known to the publisher. The publisher doesn't know about concrete observers, it knows only about their abstraction.
In the main class a publisher(Observable) is built and a few subscribers(Observers). The subscribers are subscribed to the publisher and they can be unsubscribed. In this architecture new types of subscribers can be easily added(instant messaging, ...) and new types of publishers(Weather News, Sport News, ...).

Specific Implementation Problems


Many subjects to Many observers

It's not a common situation but there are cases when a there are many observers that need to observe more than one subject. In this case the observer need to be notified not only about the change, but also which is the subject with the state changed. This can be realized very simple by adding to the subjects reference in the update notification method. The subject will pass a reference to itself(this) to the when notify the observer.

Who triggers the update?

The communication between the subject and its observers is done through the notify method declared in observer interface. But who it cat be triggered from either subject or observer object. Usually the notify method is triggered by the subject when it's state is changed. But sometimes when the updates are frequent the consecutive changes in the subject will determine many unnecessary refresh operations in the observer. In order to make this process more efficient the observer can be made responsible for starting the notify operation when it consider necessary.

Making sure Subject state is self-consistent before notification

The subject state should be consistent when the notify operation is triggered. If changes are made in the subject state after the observer is notified, it will will be refreshed with an old state. This seems hard to achieve but in practice this can be easily done when Subject subclass operations call inherited operations. In the following example, the observer is notified when the subject is in an inconsistent state:
class Observable{
 ...
 int state = 0;
 int additionalState = 0;
 public updateState(int increment)
 {
  state = state + increment;
  notifyObservers();
 }
 ...
}

class ConcreteObservable extends Observable{
 ...
 public updateState(int increment){
  super.updateState(increment); // the observers are notified
  additionalState = additionalState + increment; // the state is changed after the notifiers are updated
 }
 ...
}
This pitfall can be avoided using template methods in the abstract subject superclass for calling the notify operations. Then subject subclass will implement the operations(s) of the template:
class Observable{
 ...
 int state = 0;
 int additionalState = 0;
 public void final updateState(int increment)
 {
  doUpdateState(increment);
  notifyObservers();
 }
 public void doUpdateState(int increment)
 {
  state = state + increment;  
 }
 ...
}

class ConcreteObservable extends Observable{
 ...
 public doUpdateState(int increment){
  super.doUpdateState(increment); // the observers are notified
  additionalState = additionalState + increment; // the state is changed after the notifiers are updated
 }
 ...
}
The Operations defined in the subject base class which triggers notify operation should be documented.

Push and pull communication methods

There are 2 methods of passing the data from the subject to the observer when the state is being changed in the subject side:
  • Push model - The subjects send detailed information about the change to the observer whether it uses it or not. Because the subject needs to send the detailed information to the observer this might be inefficient when a large amount of data needs to be sent and it is not used. Another aproach would be to send only the information required by the observer. In this case the subject should be able to distinguish between different types of observers and to know the required data of each of them, meaning that the subject layer is more coupled to observer layer.
  • Pull model - The subject just notifies the observers when a change in his state appears and it's the responsibility of each observer to pull the required data from the subject. This can be inefficient because the communication is done in 2 steps and problems might appear in multithreading environments.

  • Specifying points of interests

    The efficiency can be improved by specifying which are the events on which each observer is interested. This can be realized by adding a new class defining an aspect. When an observer is registering it will provide the aspects in which it is interested:
    class Subject{
     ...
     void attach(Observer observer, Aspect interest);
     ...
    }

    Encapsulating complex update semantics

    When we have several subjects and observers the relations between them we'll become more complex. First of all are have a many to many relation, more difficult to manage directly. Second of all the relation between subjects and observers can contain some logic. Maybe we want to have an observer notified only when all the subjects will change their states. In this case we should introduce another object responsible (called ChangeManager) for the following actions:
    • to maintain the many to many relations between the subjects and their observers.
    • to encapsulate the logic of notify the observers.
    • to receive the notifications from subjects and delegate them to the observers(based on the logic it encapsulate)
    Basically the Change Manager is an observer because if gets notified of the changes of the subject and in the same time is an subject because it notify the observers. The ChangeManager is an implemenation of the Mediator pattern.

    The Observer pattern is usually used in combination with other design patterns:

    • Factory pattern - It's very likely to use the factory pattern to create the Observers so no changes will be required even in the main framework. The new observers can be added directly in the configuration files.
    • Template Method - The observer pattern can be used in conjunction with the Template Method Pattern to make sure that Subject state is self-consistent before notification
    • Mediator Pattern - The mediator pattern can be used when we have cases of complex cases of many subjects an many observers

    Memento : Design Pattern

    Memento Pattern

    Motivation

    It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.

    Intent

    • The intent of this pattern is to capture the internal state of an object without violating encapsulation and thus providing a mean for restoring the object into initial state when needed.

    Implementation

    The figure below shows a UML class diagram for the Memento Pattern:
    Memento Pattern Implementation - UML Class Diagram
    • Memento
      • Stores internal state of the Originator object. The state can include any number of state variables.
      • The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.
    • Originator
      • Creates a memento object capturing the originators internal state.
      • Use the memento object to restore its previous state.
    • Caretaker
      • Responsible for keeping the memento.
      • The memento is opaque to the caretaker, and the caretaker must not operate on it.
    A Caretaker would like to perform an operation on the Originator while having the possibility to rollback. The caretaker calls the createMemento() method on the originator asking the originator to pass it a memento object. At this point the originator creates a memento object saving its internal state and passes the memento to the caretaker. The caretaker maintains the memento object and performs the operation. In case of the need to undo the operation, the caretaker calls the setMemento() method on the originator passing the maintained memento object. The originator would accept the memento, using it to restore its previous state.

    Applicability & Examples

    The memento pattern is used when a snapshot of an object's state must be captured so that it can be restored to that state later and in situations where explicitly passing the state of the object would violate encapsulation.

    Example - Simple Calculator with Undo Operation.

    This simple example is a calculator that finds the result of addition of two numbers, with the additional option to undo last operation and restore previous result.
    Memento Pattern Alternative Implementation - UML Class Diagram

    Specific problems and implementation


    Database Transactions

    Transactions are operations on the database that occur in an atomic, consistent, durable, and isolated fashion. A transaction can contain multiple operations on the database; each operation can succeed or fail, however a transaction guarantees that if all operations succeed, the transaction would commit and would be final. And if any operation fails, then the transaction would fail and all operations would rollback and leave the database as if nothing has happened.
    This mechanism of rolling back uses the memento design pattern. Consider an object representing a database table, a transaction manager object which is responsible of performing transactions must perform operations on the table object while having the ability to undo the operation if it fails, or if any operation on any other table object fails. To be able to rollback, the transaction manager object would ask the table object for a memento before performing an operation and thus in case of failure, the memento object would be used to restore the table to its previous state.

    Consequences

    Memento protects encapsulation and avoids exposing originator's internal state and implementation. It also simplifies originator code such that the originator does not need to keep track of its previous state since this is the responsibility of the CareTaker.
    Using the memento pattern can be expensive depending on the amount of state information that has to be stored inside the memento object. In addition the caretaker must contain additional logic to be able to manage mementos.

    Related Patterns

    Command Pattern - Commands can use mementos to maintain state for undoable operations.

    Known Uses

    Undo and restore operations in most software.
    Database transactions discussed earlier.

    Double-checked locking and the Singleton pattern

    http://www.ibm.com/developerworks/java/library/j-dcl/index.html

     

    Double-checked locking and the Singleton pattern

    A comprehensive look at this broken programming idiom
    Peter Haggar (haggar@us.ibm.com), Senior Software Engineer, IBM
    Summary:  All programming languages have their share of idioms. Many are useful to know and use, and programmers spend valuable time creating, learning, and implementing them. The problem is that some idioms are later proven not to be all that they were purported, or to simply not work as described. The Java programming language contains several useful programming idioms. It also contains some that further study has shown should not be used. Double-checked locking is one such idiom that should never be used. In this article, Peter Haggar examines the roots of the double-checked locking idiom, why it was developed, and why it doesn't work.
    Tags for this article:  and, design, double-checked, java, java技术, locking, pattern, singleton, the
    Date:  01 May 2002
    Level:  Intermediate

    Comments:   15 (View | Add comment - Sign in)
    Average rating 5 stars based on 547 votes Average rating (547 votes)
    Rate this article
    Editor's note: This article refers to the Java Memory Model before it was revised for Java 5.0; statements about memory ordering may no longer be correct. However, the double-checked locking idiom is still broken under the new memory model. For more information on the memory model in Java 5.0, see "Java theory and practice: Fixing the Java Memory Model" Part 1 and Part 2.
    The Singleton creation pattern is a common programming idiom. When used with multiple threads, you must use some type of synchronization. In an effort to create more efficient code, Java programmers created the double-checked locking idiom to be used with the Singleton creation pattern to limit how much code is synchronized. However, due to some little-known details of the Java memory model, this double-checked locking idiom is not guaranteed to work. Instead of failing consistently, it will fail sporadically. In addition, the reasons for its failure are not obvious and involve intimate details of the Java memory model. These facts make a code failure due to double-checked locking very difficult to track down. In the remainder of this article, we'll examine the double-checked locking idiom in detail to understand just where it breaks down.
    Singleton creation idiom
    To understand where the double-checked locking idiom originated, you must understand the common singleton creation idiom, which is illustrated in Listing 1:

    Listing 1. Singleton creation idiom
                    
    import java.util.*;
    class Singleton
    {
      private static Singleton instance;
      private Vector v;
      private boolean inUse;
    
      private Singleton()
      {
        v = new Vector();
        v.addElement(new Object());
        inUse = true;
      }
    
      public static Singleton getInstance()
      {
        if (instance == null)          //1
          instance = new Singleton();  //2
        return instance;               //3
      }
    }
    

    The design of this class ensures that only one Singleton object is ever created. The constructor is declared private and the getInstance() method creates only one object. This implementation is fine for a single-threaded program. However, when multiple threads are introduced, you must protect the getInstance() method through synchronization. If the getInstance() method is not protected, it is possible to return two different instances of the Singleton object. Consider two threads calling the getInstance() method concurrently and the following sequence of events:
    1. Thread 1 calls the getInstance() method and determines that instance is null at //1.

    2. Thread 1 enters the if block, but is preempted by thread 2 before executing the line at //2.

    3. Thread 2 calls the getInstance() method and determines that instance is null at //1.

    4. Thread 2 enters the if block and creates a new Singleton object and assigns the variable instance to this new object at //2.

    5. Thread 2 returns the Singleton object reference at //3.

    6. Thread 2 is preempted by thread 1.

    7. Thread 1 starts where it left off and executes line //2 which results in another Singleton object being created.

    8. Thread 1 returns this object at //3.
    The result is that the getInstance() method created two Singleton objects when it was supposed to create only one. This problem is corrected by synchronizing the getInstance() method to allow only one thread to execute the code at a time, as shown in Listing 2:

    Listing 2. Thread-safe getInstance() method
                    
    public static synchronized Singleton getInstance()
    {
      if (instance == null)          //1
        instance = new Singleton();  //2
      return instance;               //3
    }
    

    The code in Listing 2 works fine for multithreaded access to the getInstance() method. However, when you analyze it you realize that synchronization is required only for the first invocation of the method. Subsequent invocations do not require synchronization because the first invocation is the only invocation that executes the code at //2, which is the only line that requires synchronization. All other invocations determine that instance is non-null and return it. Multiple threads can safely execute concurrently on all invocations except the first. However, because the method is synchronized, you pay the cost of synchronization for every invocation of the method, even though it is only required on the first invocation.
    In an effort to make this method more efficient, an idiom called double-checked locking was created. The idea is to avoid the costly synchronization for all invocations of the method except the first. The cost of synchronization differs from JVM to JVM. In the early days, the cost could be quite high. As more advanced JVMs have emerged, the cost of synchronization has decreased, but there is still a performance penalty for entering and leaving a synchronized method or block. Regardless of the advancements in JVM technology, programmers never want to waste processing time unnecessarily.
    Because only line //2 in Listing 2 requires synchronization, we could just wrap it in a synchronized block, as shown in Listing 3:

    Listing 3. The getInstance() method
                    
    public static Singleton getInstance()
    {
      if (instance == null)
      {
        synchronized(Singleton.class) {
          instance = new Singleton();
        }
      }
      return instance;
    }
    

    The code in Listing 3 exhibits the same problem as demonstrated with multiple threads and Listing 1. Two threads can get inside of the if statement concurrently when instance is null. Then, one thread enters the synchronized block to initialize instance, while the other is blocked. When the first thread exits the synchronized block, the waiting thread enters and creates another Singleton object. Note that when the second thread enters the synchronized block, it does not check to see if instance is non-null.
    Double-checked locking
    To fix the problem in Listing 3, we need a second check of instance. Thus, the name "double-checked locking." Applying the double-checked locking idiom to Listing 3 results in Listing 4.

    Listing 4. Double-checked locking example
                    
    public static Singleton getInstance()
    {
      if (instance == null)
      {
        synchronized(Singleton.class) {  //1
          if (instance == null)          //2
            instance = new Singleton();  //3
        }
      }
      return instance;
    }
    

    The theory behind double-checked locking is that the second check at //2 makes it impossible for two different Singleton objects to be created as occurred in Listing 3. Consider the following sequence of events:
    1. Thread 1 enters the getInstance() method.

    2. Thread 1 enters the synchronized block at //1 because instance is null.

    3. Thread 1 is preempted by thread 2.

    4. Thread 2 enters the getInstance() method.

    5. Thread 2 attempts to acquire the lock at //1 because instance is still null. However, because thread 1 holds the lock, thread 2 blocks at //1.

    6. Thread 2 is preempted by thread 1.

    7. Thread 1 executes and because instance is still null at //2, creates a Singleton object and assigns its reference to instance.

    8. Thread 1 exits the synchronized block and returns instance from the getInstance() method.

    9. Thread 1 is preempted by thread 2.

    10. Thread 2 acquires the lock at //1 and checks to see if instance is null.

    11. Because instance is non-null, a second Singleton object is not created and the one created by thread 1 is returned.
    The theory behind double-checked locking is perfect. Unfortunately, reality is entirely different. The problem with double-checked locking is that there is no guarantee it will work on single or multi-processor machines.
    The issue of the failure of double-checked locking is not due to implementation bugs in JVMs but to the current Java platform memory model. The memory model allows what is known as "out-of-order writes" and is a prime reason why this idiom fails.
    Out-of-order writes
    To illustrate the problem, you need to re-examine line //3 from Listing 4 above. This line of code creates a Singleton object and initializes the variable instance to refer to this object. The problem with this line of code is that the variable instance can become non-null before the body of the Singleton constructor executes.
    Huh? That statement might be contradictory to everything you thought possible, but it is, in fact, the case. Before explaining how this happens, accept this fact for a moment while examining how this breaks the double-checked locking idiom. Consider the following sequence of events with the code in Listing 4:
    1. Thread 1 enters the getInstance() method.

    2. Thread 1 enters the synchronized block at //1 because instance is null.

    3. Thread 1 proceeds to //3 and makes instance non-null, but before the constructor executes.

    4. Thread 1 is preempted by thread 2.

    5. Thread 2 checks to see if instance is null. Because it is not, thread 2 returns the instance reference to a fully constructed, but partially initialized, Singleton object.

    6. Thread 2 is preempted by thread 1.

    7. Thread 1 completes the initialization of the Singleton object by running its constructor and returns a reference to it.
    This sequence of events results in a period of time where thread 2 returned an object whose constructor had not executed.
    To show how this occurs, consider the following pseudo code for the line: instance =new Singleton();
    mem = allocate();             //Allocate memory for Singleton object.
    instance = mem;               //Note that instance is now non-null, but
                                  //has not been initialized.
    ctorSingleton(instance);      //Invoke constructor for Singleton passing
                                  //instance.
    

    This pseudo code is not only possible, but is exactly what happens on some JIT compilers. The order of execution is perceived to be out of order, but is allowed to happen given the current memory model. The fact that JIT compilers do just this makes the issues of double-checked locking more than simply an academic exercise.
    To demonstrate this, consider the code in Listing 5. It contains a stripped-down version of the getInstance() method. I've removed the "double-checkedness" to ease our review of the assembly code produced (Listing 6). We are interested only in seeing how the line instance=new Singleton(); is compiled by the JIT compiler. In addition, I've provided a simple constructor to make it clear when the constructor is run in the assembly code.

    Listing 5. Singleton class to demonstrate out-of-order writes
                    
    class Singleton
    {
      private static Singleton instance;
      private boolean inUse;
      private int val;  
    
      private Singleton()
      {
        inUse = true;
        val = 5;
      }
      public static Singleton getInstance()
      {
        if (instance == null)
          instance = new Singleton();
        return instance;
      }
    }
    

    Listing 6 contains the assembly code produced by the Sun JDK 1.2.1 JIT compiler for the body of the getInstance() method from Listing 5.

    Listing 6. Assembly code produced from code in Listing 5
                    
    ;asm code generated for getInstance
    054D20B0   mov         eax,[049388C8]      ;load instance ref
    054D20B5   test        eax,eax             ;test for null
    054D20B7   jne         054D20D7
    054D20B9   mov         eax,14C0988h
    054D20BE   call        503EF8F0            ;allocate memory
    054D20C3   mov         [049388C8],eax      ;store pointer in 
                                               ;instance ref. instance  
                                               ;non-null and ctor
                                               ;has not run
    054D20C8   mov         ecx,dword ptr [eax] 
    054D20CA   mov         dword ptr [ecx],1   ;inline ctor - inUse=true;
    054D20D0   mov         dword ptr [ecx+4],5 ;inline ctor - val=5;
    054D20D7   mov         ebx,dword ptr ds:[49388C8h]
    054D20DD   jmp         054D20B0
    

    Note: To reference the lines of assembly code in the following explanation, I refer to the last two values of the instruction address because they all begin with 054D20. For example, B5 refers to test eax,eax.
    The assembly code is produced by running a test program that calls the getInstance() method in an infinite loop. While the program runs, run the Microsoft Visual C++ debugger and attach it to the Java process representing the test program. Then, break the execution and find the assembly code representing the infinite loop.
    The first two lines of assembly code at B0 and B5 load the instance reference from memory location 049388C8 into eax and test for null. This corresponds to the first line of the getInstance() method in Listing 5. The first time this method is called, instance is null and the code proceeds to B9 . The code at BE allocates the memory from the heap for the Singleton object and stores a pointer to that memory in eax. The next line, C3, takes the pointer in eax and stores it back into the instance reference at memory location 049388C8. As a result, instance is now non-null and refers to a valid Singleton object. However, the constructor for this object has not run yet, which is precisely the situation that breaks double-checked locking. Then at line C8, the instance pointer is dereferenced and stored in ecx. Lines CA and D0 represent the inline constructor storing the values true and 5 into the Singleton object. If this code is interrupted by another thread after executing line C3 but before completing the constructor, double-checked locking fails.
    Not all JIT compilers generate the code as above. Some generate code such that instance becomes non-null only after the constructor executes. Both the IBM SDK for Java technology, version 1.3 and the Sun JDK 1.3 produce code such as this. However, this does not mean you should use double-checked locking in these instances. There are other reasons it could fail. In addition, you do not always know which JVMs your code will run on, and the JIT compiler could always change to generate code that breaks this idiom.
    Double-checked locking: Take two
    Given that the current double-checked locking code does not work, I've put together another version of the code, shown in Listing 7, to try to prevent the out-of-order write problem you just saw.

    Listing 7. Attempting to solve the out-of-order write problem
                    
    public static Singleton getInstance()
    {
      if (instance == null)
      {
        synchronized(Singleton.class) {      //1
          Singleton inst = instance;         //2
          if (inst == null)
          {
            synchronized(Singleton.class) {  //3
              inst = new Singleton();        //4
            }
            instance = inst;                 //5
          }
        }
      }
      return instance;
    }
    

    Looking at the code in Listing 7 you should realize that things are getting a little ridiculous. Remember, double-checked locking was created as a way to avoid synchronizing the simple three-line getInstance() method. The code in Listing 7 has gotten out of hand. In addition, the code does not fix the problem. Careful examination reveals why.
    This code is trying to avoid the out-of-order write problem. It tries to do this by introducing the local variable inst and a second synchronized block. The theory works as follows:
    1. Thread 1 enters the getInstance() method.

    2. Because instance is null, thread 1 enters the first synchronized block at //1.

    3. The local variable inst gets the value of instance, which is null at //2.

    4. Because inst is null, thread 1 enters the second synchronized block at //3.

    5. Thread 1 then begins to execute the code at //4, making inst non-null but before the constructor for Singleton executes. (This is the out-of-order write problem we just saw.)

    6. Thread 1 is preempted by Thread 2.

    7. Thread 2 enters the getInstance() method.

    8. Because instance is null, thread 2 attempts to enter the first synchronized block at //1. Because thread 1 currently holds this lock, thread 2 blocks.

    9. Thread 1 then completes its execution of //4.

    10. Thread 1 then assigns a fully constructed Singleton object to the variable instance at //5 and exits both synchronized blocks.

    11. Thread 1 returns instance.

    12. Thread 2 then executes and assigns instance to inst at //2.

    13. Thread 2 sees that instance is non-null, and returns it.
    The key line here is //5. This line is supposed to ensure that instance will only ever be null or refer to a fully constructed Singleton object. The problem occurs where theory and reality run orthogonal to one another.
    The code in Listing 7 doesn't work because of the current definition of the memory model. The Java Language Specification (JLS) demands that code within a synchronized block not be moved out of a synchronized block. However, it does not say that code not in a synchronized block cannot be moved into a synchronized block.
    A JIT compiler would see an optimization opportunity here. This optimization would remove the code at //4 and the code at //5, combine it and generate the code shown in Listing 8:

    Listing 8. Optimized code from Listing 7
                    
    public static Singleton getInstance()
    {
      if (instance == null)
      {
        synchronized(Singleton.class) {      //1
          Singleton inst = instance;         //2
          if (inst == null)
          {
            synchronized(Singleton.class) {  //3
              //inst = new Singleton();      //4
              instance = new Singleton();               
            }
            //instance = inst;               //5
          }
        }
      }
      return instance;
    }
    

    If this optimization takes place, you have the same out-of-order write problem we discussed earlier.
    volatile anyone?
    Another idea is to use the keyword volatile for the variables inst and instance. According to the JLS (see Resources), variables declared volatile are supposed to be sequentially consistent, and therefore, not reordered. But two problems occur with trying to use volatile to fix the problem with double-checked locking:
    • The problem here is not with sequential consistency. Code is being moved, not reordered.

    • Many JVMs do not implement volatile correctly regarding sequential consistency anyway.
    The second point is worth expanding upon. Consider the code in Listing 9:

    Listing 9. Sequential consistency with volatile
                    
    class test
    {
      private volatile boolean stop = false;
      private volatile int num = 0;
    
      public void foo()
      {
        num = 100;    //This can happen second
        stop = true;  //This can happen first
        //...
      }
    
      public void bar()
      {
        if (stop)
          num += num;  //num can == 0!
      }
      //...
    }
    

    According to the JLS, because stop and num are declared volatile, they should be sequentially consistent. This means that if stop is ever true, num must have been set to 100. However, because many JVMs do not implement the sequential consistency feature of volatile, you cannot count on this behavior. Therefore, if thread 1 called foo and thread 2 called bar concurrently, thread 1 might set stop to true before num is set to 100. This could lead thread 2 to see stop as true, but num still set to 0. There are additional problems with volatile and the atomicity of 64-bit variables, but this is beyond the scope of this article. See Resources for more information on this topic.
    The solution
    The bottom line is that double-checked locking, in whatever form, should not be used because you cannot guarantee that it will work on any JVM implementation. JSR-133 is addressing issues regarding the memory model, however, double-checked locking will not be supported by the new memory model. Therefore, you have two options:
    • Accept the synchronization of a getInstance() method as shown in Listing 2.

    • Forgo synchronization and use a static field.
    Option 2 is shown in Listing 10:

    Listing 10. Singleton implementation with static field
                    
    class Singleton
    {
      private Vector v;
      private boolean inUse;
      private static Singleton instance = new Singleton();
    
      private Singleton()
      {
        v = new Vector();
        inUse = true;
        //...
      }
    
      public static Singleton getInstance()
      {
        return instance;
      }
    }
    

    The code in Listing 10 does not use synchronization and ensures that the Singleton object is not created until a call is made to the static getInstance() method. This is a good alternative if your objective is to eliminate synchronization.
    String is not immutable
    You might wonder about the String class given the issue of out-of-order writes and a reference becoming non-null prior to the constructor executing. Consider the following code:
    private String str;
    //...
    str = new String("hello");
    

    The String class is supposed to be immutable. However, given the out-of-order write problem we discussed previously, could that cause a problem here? The answer is it could. Consider two threads with access to the String str. One thread could see the str reference refer to a String object in which the constructor has not run. In fact, Listing 11 contains code that shows this occurring. Note that this code breaks only with older JVMs that I tested. Both the IBM 1.3 and Sun 1.3 JVMs produce immutable Strings as expected.

    Listing 11. Example of a Mutable String
                    
    class StringCreator extends Thread
    {
      MutableString ms;
      public StringCreator(MutableString muts)
      {
        ms = muts;
      }
      public void run()
      {
        while(true)
          ms.str = new String("hello");          //1
      }
    }
    class StringReader extends Thread
    {
      MutableString ms;
      public StringReader(MutableString muts)
      {
        ms = muts;
      }
      public void run()
      {
        while(true)
        {
          if (!(ms.str.equals("hello")))         //2
          {
            System.out.println("String is not immutable!");
            break;
          }
        }
      }
    }
    class MutableString
    {
      public String str;                         //3
      public static void main(String args[])
      {
        MutableString ms = new MutableString();  //4
        new StringCreator(ms).start();           //5
        new StringReader(ms).start();            //6
      }
    }
    

    This code creates a MutableString class at //4 that contains a String reference shared by two threads at //3. Two objects are created, StringCreator and StringReader, on two separate threads at lines //5 and //6, passing a reference to the MutableString object. The StringCreator class enters an infinite loop and creates String objects with the value "hello" at //1. The StringReader also enters an infinite loop and checks to see if the current String object has the value "hello" at //2. If it doesn't, the StringReader thread prints out a message and stops. If the String class is immutable, you should never see any output from this program. The only way for StringReader to see the str reference to be anything other than a String object with "hello" as its value is if the problem of out-of-order writes occurs.
    Running this code on old JVMs like Sun JDK 1.2.1 results in the out-of-order write problem, and thus, a non-immutable String.
    Summary
    In an effort to avoid costly synchronization in singletons, programmers, quite ingeniously, invented the double-checked locking idiom. Unfortunately, it was not until this idiom was in fairly wide use that it became apparent that it is not a safe programming construct due to the current memory model. Work is underway to redefine areas of the memory model that are weak. However, even under the newly proposed memory model, double-checked locking will not work. The best solution to this problem is to accept synchronization or use a static field.

    Adapter Pattern : Nice article

    • http://www.oodesign.com/adapter-pattern.html
    •  Convert the interface of a class into another interface clients expect.
    • Adapter lets classes work together, that could not otherwise because of incompatible interfaces.
     
     Adapter  Pattern Implementation - UML Class Diagram
    The classes/objects participating in adapter pattern:

    • Target - defines the domain-specific interface that Client uses.
    • Adapter - adapts the interface Adaptee to the Target interface.
    • Adaptee - defines an existing interface that needs adapting.
    • Client - collaborates with objects conforming to the Target interface.

    Objects Adapters - Based on Delegation

    Objects Adapters are the classical example of the adapter pattern. It uses composition, the Adaptee delegates the calls to Adaptee (opossed to class adapters which extends the Adaptee). This behaviour gives us a few advantages over the class adapters(however the class adapters can be implemented in languages allowing multiple inheritance). The main advantage is that the Adapter adapts not only the Adpatee but all its subclasses. All it's subclasses with one "small" restriction: all the subclasses which don't add new methods, because the used mechanism is delegation. So for any new method the Adapter must be changed or extended to expose the new methods as well. The main disadvantage is that it requires to write all the code for delegating all the necessary requests tot the Adaptee.

    Class Adapters - Based on (Multiple) Inheritance

    Adapter  Pattern Implementation - UML Class Diagram
    Class adapters can be implemented in languages supporting multiple inheritance(Java, C# or PHP does not support multiple inheritance). Thus, such adapters can not be easy implemented in Java, C# or VB.NET. Class adapter uses inheritance instead of composition. It means that instead of delegating the calls to the Adaptee, it subclasses it. In conclusion it must subclass both the Target and the Adaptee. There are advantages and disadvantages:
    • It adapts the specific Adaptee class. The class it extends. If that one is subclassed it can not be adapted by the existing adapter.
    • It doesn't require all the code required for delegation, which must be written for an Object Adapter.
    If the Target is represented by an interface instead of a class then we can talk about "class" adapters, because we can implement as many interfaces as we want.

    Adapter Pattern and Strategy Pattern

    Adapter Pattern and Strategy Pattern - there are many cases when the adapter can play the role of the Strategy Pattern. If we have several modules implementing the same functionality and we wrote adapters for them, the adapters are implementing the same interface. We can simply replace the adapters objects at run time because they implements the same interface.

    Friday, May 4, 2012

    nodejs : Anatomy of http.request

    http.request() method will create createRequest Object.This will inturn call Agent's addRequestMethod.
    This will see if there is any already created socket for host:port combination.If the no of sockets created for this combination is < than the set configuration max limit value, a socket is created using net.createconnection.After the socket is returned createRequestObject's onSocket method is called which will create a function and add it to the event queue to be executed as part of next tick.This created function will set up socket's ondata.

    sockets's ondata function creates a parser and this parser parses the data and takes necessary actions.This parser creates a IncomingMessage object and populates the response.on complete it will emit response event on createRequest Object. This will call the callback we passed while initiating the request(http.request)

    clientResponse is a http.IncomingMessage object, an eventEmitter that gets built and populated by the parser whose execute method is called when socket data is available,
       
        the callback we are passing gets called after the headers are processed, at which time we will add listeners for the events 'data' & 'end'.
           
        Parser while parsing the message body will emit 'data' event on clientResponse.
        Parser will emit 'end' event on messageComplete

    Thursday, May 3, 2012

    Javascript ::: Enumerating all properties of an object

    https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Objects#Defining_getters_and_setters

     

    Enumerating all properties of an object

    Starting with ECMAScript 5, there are three native ways to list/traverse object properties:
    • for...in loops
      This method traverses all enumerable properties of an object and its prototype chain
    • Object.keys(o)
      This method returns an array with all the own (not in the prototype chain) enumerable properties names ("keys") of an object o.
    • Object.getOwnPropertyNames(o)
      This method returns an array containing all own properties names (enumerable or not) of an object o.
    In ECMAScript 5, there is no native way to list all properties of an object. However, this can be achieved with the following function:
    1. function listAllProperties(o){       
    2.     var objectToInspect;       
    3.     var result = [];  
    4.       
    5.     for(objectToInspect = o; objectToInspect !== null; objectToInspect = Object.getPrototypeOf(objectToInspect)){    
    6.         result = result.concat(Object.getOwnPropertyNames(objectToInspect));    
    7.     }  
    8.       
    9.     return result;   
    10. }  
    This can be useful to reveal "hidden" properties (properties in the prototype chain which are not accessible through the object, because another property has the same name earlier in the prototype chain). Listing accessible properties only can easily be done by removing duplicates in the array.