Thursday, March 17, 2016

Design Pattern - 4 Factory Pattern.

The Factory Design Pattern -  Defines and interface for creating an object, but lets subclasses decide which class to instantiate. Factory pattern lets a class defer instantiation to subclasses.

This pattern gives a way to encapsulate the instantiations of concret types.

Example: Consider a product Pizza. There are different types of pizza.CheesePizza, VegPizza, ChickenPizza. These are subtypes of the product Pizza. Taking Pizza as a class, CheesePizza, VegPizza and ChickenPizza are the sub classes of Pizza.

Now, let's take the case of a PizzaStore where people order pizza. The main objective of a PizzaStore is to cater to customers who order pizzas. People place order and PizzaStore irrespective of whatever pizza was ordered by the customer, need to cater to the customer.

Thus, PizzaStore uses the product Pizza. PizzaStore has operations such as orderPizza. The process of instantiating which type of pizza can be isolated from the PizzaStore.

class VegPizza extends Pizza{

}

abstract class PizzaStore {
   factoryMethod();
    orderPizza(){ }
}

The factory method can be implemented by classes inherting PizzaStore, such as NewYorkStylePizzaStore or CaliforniaStylePizzaStore, thereby creating NewYorkStyleCheesePizza ot CaliforniaStyleCheesePizza.

Design Principle: Depend upon abstractions. Do not depend upon concrete classes.

Abstract Factory Pattern: Provides an interface for creating families of related or dependant objects without specifying their concrete classes.

Design Principles so far:
1) Encapsulate what varies
2) Favor composition over inheritance.
3) Program to interfaces, not implementations.
4) Strive for loosely coupled designs between objects that interact.
5) Classes should be open for extension, but closed for modification.

6) Depend on abstractions. Do not depend on concrete classes.

Tuesday, March 15, 2016

Design Pattern - 3 - Decorator Pattern



Decorator Pattern - Attaches additional responsibilities to an object dyanmically.
Decorators  provide a flexible alternative to subclassing for extending functionality.

Example: Take the case of veg. pizza being ordered. Customer wants to add extra cheese as topping.
Here the veg. pizza is the basic class and cheese is the decorator class. Cheese class decorates the veg. pizza.
Similarly, if user decides to add some extra jalepenos. Then, jalepeno is a decorator class.

Code:
class VegPizza extends Pizza {
  // description of VegPizza 
   String topping; 
   addTopping(){
       topping = topping+"veg";
   }   
}

abstract class Toppings extends Pizza {
 
}

class CheeseTopping extends Toppings {
   Pizza pizza;
   CheeseTopping(Pizza pizza){
      this.pizza= pizza;
   }
    addTopping(){
       pizza.topping=pizza.topping + "cheese";       
   }
}

class JalepenoTopping extends Toppings {
   Pizza pizza;
   JalepenoTopping(Pizza pizza){
       this.pizza= pizza;   
   }
   addTopping(){
            pizza.topping=pizza.topping + "Jalepeno";       
   }
}

class MainClass{
  public static void main(String args[]){
   Pizza vegPizza = new VegPizza();
   CheeseTopping topping1= new CheeseTopping(vegPizza);
   JalepenoTopping topping2 = new JalepenoTopping(vegPizza);
   System.out.print("Toppings added:+"vegPizza.toppings);   
 }
}

Design Principle: Classes should be open for extension, but closed for modification.

Design Principles so far:
1) Encapsulate what varies
2) Favor composition over inheritance.
3) Program to interfaces, not implementations.
4) Strive for loosely coupled designs between objects that interact.
5) Classes should be open for extension, but closed for modification.

The downside of the decorator pattern is - a lot of small classes(decorator) can get added up making it difficult to understand the design.
However, the benefit lies in the fact the the client or the base class not being aware that it is being decorated upon.

Tuesday, March 8, 2016

Design Pattern - 2 Observer Pattern

The Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated successfully.

Observer pattern has a Subject to which Observers listen to. It is similar to a subscriber-enewspaper relationship. Whenever a newspaper (subject) is published, the subscribers(observers) are notified.
Also similar like a button in a page whose listeners perform different operations based on the button action.

Design principle: Strive for loosely coupled designs between objects that interact.

Observable is a class in java.util. package where the observer pattern is implemented.

Typically, the Subject interface or Observable class has methods to add an observer, remove an observer and notifyObservers().The subject maintains a list of observers.The subject is unaware when a new observer is added.
public SubjectImpl implements Subject {
  private List observerList ;
  public void addObserver(Observer observer){ observerList.add(observer); }
  public void removeObserver(Observer observer { // code to remove from observerlist; }
  public void notifyObservers() {  // Iterate over list of observerList and notify the data change }
}

The Observer interface or class implementing observer interface will register or add itself to the  Subject. The subject reference is maintained in the observer class.

public ObserverImpl implements Observer{
 private Subject subject;
 public ObserverImpl(Subject subjectarg){
   this.subject = subjectarg;
   subject.addObserver(this);
 }

}

The order of notification to observers CANNOT be guranteed by subject.
Swing uses heavy usage of Observer pattern.

Design principles so far:
Design Principle 1: Identify the parts of the application that can vary and separate them from what stays the same. Encapsulate what varies.

Design Principle 2: Program to an interface, not an implementation.

Design Principle 3: Favor composition over inheritance.


Design principle 4: Strive for loosely coupled designs between objects that interact. 





Thursday, March 3, 2016

Design Pattern - 1 - Strategy Pattern

Design Principle 1: Identify the parts of the application that can vary and separate them from what stays the same. Encapsulate what varies.

Design Principle 2: Program to an interface, not an implementation.

Design Principle 3: Favor composition over inheritance.

Strategy Pattern: The strategy pattern defines a family of algorithms, encapsulates each one and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Courtesy: Head First Design Patterns

Wednesday, December 29, 2010

Array To List & Vice Versa

Converting an array to a List type
Object[] array = new Object[]{"12","23","34"};
java.util.List list = Arrays.asList(array);

Object can be a String array or Integer array. However, they cannot be a primitive array.
To convert a primitive array such as int to a List, the int array must be converted to an Object array such as Integer array.

Converting a List to an array.
List list = new ArrayList();
list.add("AB");
list.add("CD");
list.add("EF");
String[] sl = (String[]) list.toArray(new String[list.size()]);

To find the minimum in a integer array,
int min = (int) Collections.min(Arrays.asList(lenArr));
where lenArr is an Object[] array such as an Integer array.

To find the minimum in a integer array,
int max = (int) Collections.max(Arrays.asList(lenArr));

Wednesday, November 10, 2010

Setting JVM Size in Eclipse

One quick way to resolve the Java OutOfMemory error in Eclipse...
1) Open 'Debug Configurations' in Eclipse. (Run --> Debug Configurations)
2) In the left pane, click on 'Java Application'.
3) The list of java application ( with the main() method) are listed here.
4) Select the required Java Application.
5) In the right pane, click on the '(x)=Arguments'. Under VM Arguments, type
-Xmx1g - This will increase the heap size to 1 GB
-Xmx256m - This will increase the heap size to 256 MB.
6) Click Apply and Ok.

Wednesday, September 15, 2010

DB2 Basic Commands

1) To import data from a csv file to a table in database:
import from C:\testfile.csv of del insert into workemployee
2) To export data to a csv file from a table in database:
export to C:\names.csv of del select * from names
3) To connect to db2 database
connect to database_name user user_id using pwd
4) To set the schema for accessing the tables under a schema
set current schema schema_name
5) To list all tables under the schema or in general to list all tables. Two ways of doing the same.
- list tables for schema schema_name
- list tables