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