MY mENU


Thursday 9 May 2013

Cascading Order

Multiple Styles Will Cascade into One  
Styles can be specified:
  • inside an HTML element
  • inside the head section of an HTML page
  • in an external CSS file
Even multiple external style sheets can be referenced inside a single HTML document.

Cascading order:

What style will be used when there is more than one style specified for an HTML element?

Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number 4 has the highest priority:
  1. Browser default
  2. External style sheet
  3. Internal style sheet (in the head section)
  4. Inline style (inside an HTML element)
So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style defined inside the tag, or in an external style sheet, or in a browser (a default value).

Design Patterns Intro

The Design Pattern is not a framework and is not directly deployed via code.

Design Pattern have two main usages:
  • Common language for developers: they provide developer a common langauage for certain problems. for example if a developer tells another developer that he is using a singleton, the another developer (Should) know exactly what is singleton.
  • Capture best practices: Design patterns capture solutions which have been applied to certain problems. By learning these patterns and the problem they are trying to solve a un experienced developer can learn a lot about software design.
Design pattern are based on the base principles of object orientated design.
  • Program to an interface not an implementation
  • Favor object composition over inheritance
Design Patterns can be divided into:
  • Creational Patterns
  • Structural Patterns
  • Behavioral Patterns
These patterns can divided into below shown:

Creational Patterns:
  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Singleton Pattern
  4. Builder Pattern
  5. Prototype Pattern
Structural Patterns:
  1. Adapter Pattern
  2. Bridge pattern
  3. Composite Pattern
  4. Decorator Pattern
  5. Facade Pattern
  6. Flyweight Pattern
  7. Proxy Pattern
Behavioral Patterns:
  1. Chain of responsibility Pattern
  2. Command Pattern
  3. Interpreter Pattern
  4. Iterator Pattern
  5. Mediator Pattern
  6. Momento Pattern
  7. Observer Pattern
  8. State Pattern
  9. Template Pattern
  10. Visitor Pattern

Factory Pattern

Factory of what? of classes. In simple words, if we have super class and n subclasses, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.
Let's take an example to understand this pattern.

Example: Let's suppose an application asks for entering the name and sex of a person. If the sex is male(M), it displays welcome message saying Hello Mr. and if the sex is Female (F), it displays message saying hello Ms.
The skeleton of the code can be given here.

public class Person{
    //name string
   public String name;
      private String gender;

      public String getName() {
        return name;
        }

     public String getGender(){
    return gender;
     }

}

This is a simple class person having methods for name and gender. Now we will have two sub-classes, Male and Female which will print the welcome message on screen.

public class Male extends Person{
     public Male(String fullName){
       System.out.println("Hello Mr."+fullName);
     }
}

Also the class Female

public class Female extends Person{
   public Female(String fullName){
       System.out.println("Hello Ms. "+fullName);
     }
}
Now, we have to create a client, or a salutationFactory which will return the welcome messgae depending on the data provided. 

public class SalutationFactory{
 public static void main(String args[]){
     SalutationFactory factory=new SalutationFactory();
     factory.getPerson(args[0],args[1]);
}
public Person getPerson(String name, String gender){
     if(gender.equals("M"))
        return new Male(name);
else if(gender.equals("F"))
       return new Female(name);
  else
      return null;
   }
}

This class accepts two arguments from the system at runtime and prints the names.

Running the program:

After compiling and running the code on my computer with the arguments Srinivas and M:

java Srinivas M

the result returned is : "Hello Mr. Srinivas".

when to use a Factory Pattern?

the factory patterns can be used in following cases:
  •  when a class does not know which class of objects it must create.
  • a class specifies its sub-classes to specify which objects to create.
  • In programmers language( very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.