Jul 23, 2010

Factory Method Pattern


Factory Method Pattern
An important facet of system design is the manner in which objects are created. One of the most widely used creational patterns is the Factory Method Pattern.
Creational patterns describe object-creation mechanisms that enable greater levels of reuse in evolving systems
Let’s consider that, the client is an object that requires an instance of another object (the product) for some purpose. Rather than creating the product instance directly, the client delegates this responsibility to the factory. Once invoked, the factory creates a new instance of the product, passing it back to the client. Put simply, the client uses the factory to create an instance of the product.
CLIENT ---uses----> FACTORY (Creator) ----create---->PRODUCT
The Factory completely abstracts the creation and initialization of the Product from the Client, which helps client to focus on its discrete role. As the product implementation changes in future, the client remains unchanged.
“Factory Method Pattern defines an interface for creating an object but let’s subclass decide which class to instantiate”
Most implementations of the Factory method pattern use two abstract classes, Factory and Product.
Consider the following example for Factory Method Pattern.
Let consider AbstractProduct.java class as abstract product class which return the cost of the product. This abstract product class is extended and two concrete classes are created i.e.  ProductSubClass_One.java and ProductSubClass_Sec.java
 







ProductSubClass_One.java-This class extends AbstractProduct class and provide first concrete implementation of getProductCost() method.


 






ProductSubClass_Sec.java-This class extends AbstractProduct class and provide second concrete implementation of getProductCost() method.







Next consider the Factory (Creator) class AbstractCreator.java is an abstract class which contains one method createProduct(String)  method for creating the Product.








Now we will create a concrete class of the Factory (creator) i.e. ConcreteCreator.java which override the createProduct (String) method to return object of the concrete Product Sub Class based on the type.











Till now we have created Factory and Product related classes, now we will create a Client class which in turn uses Factory (creator) class to obtain a Product object.

 





Here in this class we are creating a Factory (creator) object and using this factory object to obtain the product based on “type” which passed as argument in createProduct (String) method.
When we run the Client.java class we will get object of ProductSubClass_One class which gives following output.






Factory method pattern should be used, 

  • When the object creation depends upon the user data or some event; 
  • When object which is getting created is abstracted from the user;
  • When the type of object created is to be decided at runtime.

Jul 17, 2010

Amazon Interview Questions

Q1.

Given an array of size n+1 which contains all the numbers from 1 to n. Find the number which is repeated in O(n) time. How do you proceed with the same with floating numbers from 0 to 1 instead of 1 to n.

Ans:

The number appearing two times is (sum of all the numbers in the array) - (sum of the numbers from 1 to n). For floating numbers multiply it with 100 and proceed.

Q2.

How will you find the first k smallest elements in a given unsorted array in O(n) at the worst case?

Ans:

This can be solved in O(n) time. You can find out the position of kth element in an array using the "median of medians" algorithm.

Median of Medians Algorithm

Once we have the kth element, we can just iterate through the array and print elements that are smaller than the kth element to get the k smallest elements.

Jul 9, 2010

Effective SOA Governance


Now a day it is of paramount importance that an enterprise that is strategizing around SOA needs an efficient governance mechanism. SOA governance is more than just providing governance for SOA efforts; it is how IT governance should operate within an enterprise that has adopted SOA as its primary approach to enterprise architecture.
Steps for effective SOA governance
1.       Governance should be included from very beginning of SOA initiative.
·         Because of the nature of discrete, well-defined services within a SOA, governance is not exclusively a runtime or design-time activity. SOA governance happens all the time—design time, run time, and change time—and architects understand this.

2.       Effective SOA starts with governance but a governance solution need to be sold to business to get necessary funding. Approach executive management with the justification for SOA governance. Business should understand the benefit of SOA governance for getting support. E.g.  Hartford case study. There was firm commitment from Leadership team of Hartford to put governance in place.
Business team need to understand the benefit of codifying best practices, processes and rules into reusable services based on
·         Better control of business solution
·         Enabling business and IT teams to collaborate based on business requirements.
Business team needs to provide support for SOA governance
·         Cross organizational oversight
·         Centralize management of adoption of SOA across organization
·         Encourage collaboration across business units with different perspectives.

3.       Create a comprehensive plan to create high value and right business service with executive support. Roadmap is needed to ensure success.

·         Begin with understanding of the corporate goals and objectives of the business. Establish a governance process before your organization begins building business services.
·         Choosing projects that integrate and automate business processes that have the most widespread, visible, and positive impact across the organization enables IT to both qualitatively and quantifiably demonstrate the value of well-governed SOA to the business.
·         Develop a process to catalog information about each service- definitions, control, and change management.
·         Plan to ensure services are safe to use i.e. security

4.       Establish process for organizational change-managing change is as important as creating SOA services.

·         Put processes in place to keep track of which business services are developed and when/how they are changed
·         Avoid chaos by creating systematic approach to notification and approval for changes.
·         Business need to agree on business rules to ensure stability and trust.
·         Create business services in a predictable manner based on corporate governance requirements.

5.       Balancing risk with oversight to find proper balance for SOA governance.
·         How much risk can you afford to take?
·         Which business need high level of oversight at corporate and IT level? Some business process require high level of oversight other not. There should always be room for innovation. Future innovation should not be constrained.
·         Build flexibility into your governance model.

6.       Governing Service Lifecycle – ongoing process
Governance process needs to be ongoing. Step to manage service over their lifecycle
·         Decompose parts of business processes
·         Identify the service owner.
·         Identify the source of funding.
·         Govern services from an end to end perspective, so they are not viewed as isolated elements.
·         Provide support to ongoing services.
·         Ensure service meet the corporate objectives. As corporate objective change services change
In the end some important points
·         SOA demands a business focus to be effective
·         SOA demands that you codify business processes into reusable services
·         Trust is the heart of SOA governance – predictability is the foundation of trust
Business and IT are jointly responsible for ensuring that software reflects best business and IT practices

Jun 26, 2010

Singleton Design Pattern

The Singleton creation pattern is a common programming idiom.The Singleton is a useful Design Pattern for allowing only one instance of your class to be created, but common mistakes can inadvertently allow more than one instance to be created.

The classic example is:



















This is a good solution and 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  In the general case, however, it is not thread-safe. This implementation is fine for single threaded program but when multiple threads are introduced then getInstance() method must be protected.

Consider two threads calling the getInstance() method concurrently and the following sequence of events.
  1. Thread A calls the getInstance() method and determines that instance is null at //1.
  2. Thread A enters the if block at //1, but is preempted by thread B before executing the line at //2.
  3. Thread B calls the getInstance() method and determines that instance is null at //1.
  4. Thread B enters the if block at //1 and creates a new Singleton object and assigns the variable instance to this new object at //2.
  5. Thread B returns the Singleton object reference at //3.
  6. Thread B is preempted by thread A.
  7. Thread A starts where it left off and executes line //2 which results in creation of another Singleton object.
  8. Thread A returns this second object at //3.
The above classic example creates two Singleton objects when it should create one. This problem can be corrected by synchronization of getInstance() method, which would allow only one thread to execute at a time in getInstance() method.

 












    The synchronized code mentioned above works correctly for a multithreaded access to getInstance() method. But on carefully looking at the code, you can easily make out that instead of synchronizing whole of getInstance() method, synchronization is required only for the first invocation of the method. Subsequent invocations to getInstance() method does not require synchronization because the first invocation is the only invocation that executes the code at line //2. Synchronization has performance cost associated with it, so in order to make above program more efficient we have following third approach.



















    SyncSingleton_2 class also exhibits the same problem as was there with the Classic approach.

    Consider two threads calling the getInstance() method concurrently and the following sequence of events.
    1. Thread A calls the getInstance() method and determines that instance is null at //1.
    2. Thread A enters the if block at //1, but is preempted by thread B before entering the Synchronized block..
    3. Thread B calls the getInstance() method and determines that instance is null at //1.
    4. Thread B enters the if block at //1 and creates a new Singleton object and assigns the variable instance to this new object at //2.
    5. Thread B returns the Singleton object reference at //3.
    6. Thread B is preempted by thread A.
    7. Thread A starts where it left off and executes line //2 which results in creation of another Singleton object.
    8. Thread A returns this second object at //3.

    To fix the above problem there is solution which require that a second check should be made before creating the object. This method is called Double Checked locking.

















    The concept behind double-checked locking is that the second check at //2 makes it impossible for two different SyncSingleton_3 objects to be created.
    The concept behind Double Check Locking makes it perfect, but this is not the case.The problem with double-checked locking is that there is no guarantee it will work.This is because of Java Platform memory model which allow out-of order writes. We won't be covering that here.

    The bottom line is that double-checked locking, in whatever form, should not be used because you cannot guarantee that it will work. So there are only two options left which are as follows.
    1. First is to synchronize getInstance() method, which we have already covered above.
    2. Second is to let go synchronization and use static instead, which we will cover now.















    In the above scenario StaticSingleton object is not created until call is made to static getInstance() method. This is a good alternative is you donot wish to use synchronisation.The Java specs guarantees that the static initializer will be executed only once, at class load time. There could be an argument that, this would create an object if someone refers to the class or at class loading time even if it is not used and this becomes a valid argument when the object is heavy.

    An embedded static final class can defer this as shown below













     
    If someone happens to refer to StaticSingleton.class , the singleton object would not be created unless explicit call to getInstance() method is not done.At that time, SingletonHolder is referred to; its class loads; and its static member instance is instantiated.

    There is one thing clear after above discussion on Singleton, that any implementation should be matched carefully to the application at hand.

    Jun 17, 2010

    Configuration of Oracle BPM enterprise 10.3.1.0.0 (Standalone) with MSSQL Server 2005


    Step 1: Click on Add button


    Step 2:
    Step 3:



    Step 4:

    1. Enter the host name were MSSQL server is running  Host: localhost
    2. Enter the Port on which MSSQL server is listening/running Port : 2108
    3. Create a login in MSSQL server obpmdir and provide dbcreator role
    a.       Enter User : obpmdir
    b.      Enter Password : ********
    c.       Enter Confirm Password : *********
          
    1. Enter the database name for Directory: Database : OBPMDir


     Step 5:



     Step 6:



    Step 7: Repeat steps performed for creating directory i.e. step 4, for creating engine




    Step 8:






    Step 9: Make sure MSSQL server is up and running





    Step 10: Directory configuration added under Directories section