Design patterns : Bridge Design Pattern

Bridge pattern decouples the abstraction from its implementation so that both can vary independently. It is quite similar to the adapter pattern. An adapter pattern makes two unrelated, existing classes work together, when the two participants were not thought to be aware of each other during design. A bridge pattern separates concerns and is chosen at the design level before the creation of participating classes.

There is an abstraction that the client sees. There is an implementor which provides the interface for actual implementation. A refined abstraction which provides extension to the abstraction’s functionalities. And a ConcreteImplementor which is an implementation of the implementor.

PHP Code :

abstract class BridgeBook{  private $bbAuthor;  private $bbTitle;  private $bbImp;  function __construct($author_in, $title_in, $choice_in)  {    $this->bbAuthor = $author_in;    $this->bbTitle  = $title_in;    if ('STARS' == $choice_in)    {      $this->bbImp = new BridgeBookStarsImp();    } else    {      $this->bbImp = new BridgeBookCapsImp();    }  }  function showAuthor()  {    return $this->bbImp->showAuthor($this->bbAuthor);  }  function showTitle()  {    return $this->bbImp->showTitle($this->bbTitle);  }}

class BridgeBookAuthorTitle extends BridgeBook{  function showAuthorTitle()  {    return $this->showAuthor() . "'s " . $this->showTitle();  }}

class BridgeBookTitleAuthor extends BridgeBook{  function showTitleAuthor()  {    return $this->showTitle() . ' by ' . $this->showAuthor();  }}

abstract class BridgeBookImp{  abstract function showAuthor($author);  abstract function showTitle($title);}

class BridgeBookCapsImp extends BridgeBookImp{  function showAuthor($author_in)  {    return strtoupper($author_in);  }  function showTitle($title_in)  {    return strtoupper($title_in);  }}

class BridgeBookStarsImp extends BridgeBookImp{  function showAuthor($author_in)  {    return Str_replace(" ","*",$author_in);  }  function showTitle($title_in)  {    return Str_replace(" ","*",$title_in);  }}

echo('BEGIN'."\n");

echo('test 1 - author title with caps');$book = new BridgeBookAuthorTitle('Larry Truett','PHP for Cats','CAPS');echo($book->showAuthorTitle());echo("\n");

echo('test 2 - author title with stars');$book = new BridgeBookAuthorTitle('Larry Truett','PHP for Cats','STARS');echo($book->showAuthorTitle());echo("\n");

echo('test 3 - title author with caps');$book = new BridgeBookTitleAuthor('Larry Truett','PHP for Cats','CAPS');echo($book->showTitleAuthor());echo("\n");

echo('test 4 - title author with stars');$book = new BridgeBookTitleAuthor('Larry Truett','PHP for Cats','STARS');echo($book->showTitleAuthor());echo("\n");

echo('END'."\n");

Output : BEGINtest 1 - author title with capsLARRY TRUETT's PHP FOR CATStest 2 - author title with starsLarry*Truett's PHP*for*Catstest 3 - title author with capsPHP FOR CATS by LARRY TRUETTtest 4 - title author with starsPHP*for*Cats by Larry*TruettEND

Java Code :

/** "Implementor" */interface DrawingAPI {  public void drawCircle(double x, double y, double radius);}

/** "ConcreteImplementor" 1/2 */class DrawingAPI1 implements DrawingAPI {  public void drawCircle(double x, double y, double radius)   {    System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);  }}

/** "ConcreteImplementor" 2/2 */class DrawingAPI2 implements DrawingAPI {  public void drawCircle(double x, double y, double radius)   {     System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);  }}

/** "Abstraction" */interface Shape {  public void draw();                             // low-level  public void resizeByPercentage(double pct);     // high-level}

/** "Refined Abstraction" */class CircleShape implements Shape {  private double x, y, radius;  private DrawingAPI drawingAPI;  public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI)   {    this.x = x;      this.y = y;      this.radius = radius;     this.drawingAPI = drawingAPI;  }

  // low-level i.e. Implementation specific  public void draw()   {    drawingAPI.drawCircle(x, y, radius);  }     // high-level i.e. Abstraction specific  public void resizeByPercentage(double pct)   {    radius *= pct;  }}

/** "Client" */public class Bridge{  public static void main(String[] args)   {    Shape[] shapes = new Shape[2];    shapes[0] = new CircleShape(1, 2, 3, new DrawingAPI1());    shapes[1] = new CircleShape(5, 7, 11, new DrawingAPI2());

    for (Shape shape : shapes)     {      shape.resizeByPercentage(2.5);      shape.draw();    }  }}

Output :

API1.circle at 1.000000:2.000000 radius 7.500000API2.circle at 5.000000:7.000000 radius 27.500000

Design patterns : Adapter Design Pattern

Adapter design pattern converts the interface of a class into another interface that the client expects. It lets the classes work together that otherwise could not because of incompatible interfaces. It wraps the existing class in a compatible interface acceptible to the the client.

The actors here are the client – which makes use of an object which implements the target interface, the target is the point of extension of the client module, adaptee – object of different module or library and the adapter is an implementation of the target which forwards real work to the adaptee and also hides him from the client.

PHP Code :

class SimpleBook{  private $author;  private $title;  function __construct($author_in, $title_in)  {    $this->author = $author_in;    $this->title  = $title_in;  }  function getAuthor()  {    return $this->author;  }  function getTitle()  {    return $this->title;  }}

class BookAdapter{  private $book;  function __construct(SimpleBook $book_in)  {    $this->book = $book_in;  }  function getAuthorAndTitle()  {    return $this->book->getTitle().' by '.$this->book->getAuthor();  }}

// client

echo "BEGIN\n";

$book = new SimpleBook("PHP Author", "PHP Book on Design patterns");$bookAdapter = new BookAdapter($book);echo "Author and Title: ".$bookAdapter->getAuthorAndTitle()."\n";

echo "END\n";

Output:

BEGINAuthor and Title: PHP Book on Design patterns by PHP AuthorEND

JAVA Code:

class LegacyLine{  public void draw(int x1, int y1, int x2, int y2)  {    System.out.println("line from (" + x1 + ',' + y1 + ") to (" + x2 + ','  + y2 + ')');  }}

class LegacyRectangle{  public void draw(int x, int y, int w, int h)  {    System.out.println("rectangle at (" + x + ',' + y + ") with width " + w  + " and height " + h);  }}

interface Shape{  void draw(int x1, int y1, int x2, int y2);}

class Line implements Shape{  private LegacyLine adaptee = new LegacyLine();  public void draw(int x1, int y1, int x2, int y2)  {    adaptee.draw(x1, y1, x2, y2);  }}

class Rectangle implements Shape{  private LegacyRectangle adaptee = new LegacyRectangle();  public void draw(int x1, int y1, int x2, int y2)  {    adaptee.draw(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),   Math.abs(y2 - y1));  }}

public class AdapterDemo{  public static void main(String[] args)  {    Shape[] shapes =     {      new Line(), new Rectangle()    };    // A begin and end point from a graphical editor    int x1 = 10, y1 = 20;    int x2 = 30, y2 = 60;    for (int i = 0; i < shapes.length; ++i)      shapes[i].draw(x1, y1, x2, y2);  }}

Output : 

line from (10,20) to (30,60)rectangle at (10,20) with width 20 and height 40

Design Patterns : Prototype pattern

The prototype pattern specifies the kind of objects to create using a prototypical instance, and creates new objects by copying the prototype. A clone method is available in the class which can be used to create new objects of the same kind.

The pattern consists of a prototype – an interface of the cloneable classes. A ConcretePrototype implements the clone operations and a client actually clones the prototype instance to use the clones as new objects.

Simple examples

PHP

abstract class BookPrototype{  protected $title;  protected $topic;  abstract function __clone();  function getTitle()  {    return $this->title;  }  function setTitle($titleIn)  {    $this->title = $titleIn;  }  function getTopic()  {    return $this->topic;  }}

class PHPBookPrototype extends BookPrototype{  function __construct()  {    $this->topic = 'PHP';  }  function __clone()  {  }}

class SQLBookPrototype extends BookPrototype{  function __construct()  {    $this->topic = 'SQL';  }  function __clone()  {  }}

echo("BEGIN \n");

$phpProto = new PHPBookPrototype();$sqlProto = new SQLBookPrototype();

$book1 = clone $sqlProto;$book1->setTitle('SQL Book prototype');echo('Book 1 topic: '.$book1->getTopic()."\n");echo('Book 1 title: '.$book1->getTitle()."\n");

$book2 = clone $phpProto;$book2->setTitle('PHP Book prototype');echo('Book 2 topic: '.$book2->getTopic()."\n");echo('Book 2 title: '.$book2->getTitle()."\n");

$book3 = clone $sqlProto;$book3->setTitle('SQL Book prototype II');echo('Book 3 topic: '.$book3->getTopic()."\n");echo('Book 3 title: '.$book3->getTitle()."\n");

echo("END\n");

Output:

BEGIN Book 1 topic: SQLBook 1 title: SQL Book prototypeBook 2 topic: PHPBook 2 title: PHP Book prototypeBook 3 topic: SQLBook 3 title: SQL Book prototype IIEND

Java :

public class proto {  interface Xyz   {    Xyz cloan();  }

  static class Tom implements Xyz   {    public Xyz cloan()        {      return new Tom();    }    public String toString()     {      return "tom prototype";    }  }

  static class Dick implements Xyz   {    public Xyz    cloan()        {      return new Dick();    }    public String toString()     {      return "dick prototype";    }  }

  static class Harry implements Xyz   {    public Xyz    cloan()        {      return new Harry();    }    public String toString()     {      return "harry prototype";    }  }

  static class Factory   {    private static java.util.Map prototypes = new java.util.HashMap();    static     {      prototypes.put( "tom",   new Tom() );      prototypes.put( "dick",  new Dick() );      prototypes.put( "harry", new Harry() );    }    public static Xyz makeObject( String s )     {      return ((Xyz)prototypes.get(s)).cloan();    }  }

  public static void main( String[] args )   {    for (int i=0; i < args.length; i++)     {      System.out.println( Factory.makeObject( args[i] ) + "  " );    }  }}

Output : $ java proto tom dick harry harry tomtom prototype  dick prototype  harry prototype  harry prototype  tom prototype 

Design patterns : Factory Method

Factory method defines an interface for creating an object, but lets the subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.

Factory method makes the design more customizable and only a little more complicated. Other design patterns require new classes whereas factory method requires only a new operation.

Factory method is quite similar to the Abstract Factory. In fact every relevant method of Abstract Factory is a Factory Method. Factory methods could be moved out of their classes in subsequent refactoring and evolve in an external Abstract Factory.

The participating actors are the “Product” – the abstraction of the created object, a “concrete product” – an implementation of the product. “Creator” is the base class which declares a default Factory Method and “ConcreteCreator” is a subclass of the creator which overrides the Factory Method to return a Concrete Product.

Lets check some code

PHP : lets check some code similar to those we wrote for Abstract Factory

abstract class AbstractFactoryMethod{  abstract function makeBook($param);}

class OReillyFactoryMethod extends AbstractFactoryMethod{  private $context = "OReilly";  function makeBook($param)  {    $book = NULL;    switch($param)    {      case "PHP":        $book = new OReillyPHPBook;      break;      case "MySQL":        $book = new OReillyMySQLBook;      break;      default:      $book = new OReillyPHPBook;      break;    }    return $book;  }}

class SAMSFactoryMethod extends AbstractFactoryMethod{  private $context = "SAMS";  function makeBook($param)  {    $book = NULL;    switch($param)    {      case "PHP":        $book = new SamsPHPBook;      break;      case "MySQL":        $book = new SamsMySQLBook;      break;      default:      $book = new SamsMySQLBook;      break;    }    return $book;  }}

abstract class AbstractBook{  abstract function getAuthor();  abstract function getTitle();}

class OReillyPHPBook extends AbstractBook{  private $author;  private $title;  function __construct()  {    $this->author = "Author - OReilly php";    $this->title = "Title - OReilly php";  }

  function getAuthor() {return $this->author;}  function getTitle() {return $this->title;}}

class SamsPHPBook extends AbstractBook{  private $author;  private $title;  function __construct()  {    $this->author = "Author - SAMS php";    $this->title = "Title - SAMS php";  }

  function getAuthor() {return $this->author;}  function getTitle() {return $this->title;}}

class OReillyMySQLBook extends AbstractBook{  private $author;  private $title;  function __construct()  {    $this->author = "Author - OReilly MySQL";    $this->title = "Title - OReilly MySQL";  }

  function getAuthor() {return $this->author;}  function getTitle() {return $this->title;}}

class SamsMySQLBook extends AbstractBook{  private $author;  private $title;  function __construct()  {    $this->author = "Author - SAMS MySQL";    $this->title = "Title - SAMS MySQL";  }

  function getAuthor() {return $this->author;}  function getTitle() {return $this->title;}}

function testFactoryMethod($factoryMethodInstance){  $phpBook = $factoryMethodInstance->makeBook("PHP");  echo "php author : ".$phpBook->getAuthor()."\n";  echo "php title : ".$phpBook->getTitle()."\n";

  $mysqlBook = $factoryMethodInstance->makeBook("MySQL");  echo "mysql author : ".$mysqlBook->getAuthor()."\n";  echo "mysql title : ".$mysqlBook->getTitle()."\n";}

echo("Begin\n");echo("Testing OReillyFactoryMethod\n");$bookMethodInstance = new OReillyFactoryMethod;testFactoryMethod($bookMethodInstance);echo("----\n");

echo("Testing SamsFactoryMethod\n");$bookMethodInstance = new SamsFactoryMethod;testFactoryMethod($bookMethodInstance);echo("----\n");

Output : 

BeginTesting OReillyFactoryMethodphp author : Author - OReilly phpphp title : Title - OReilly phpmysql author : Author - OReilly MySQLmysql title : Title - OReilly MySQL----Testing SamsFactoryMethodphp author : Author - SAMS phpphp title : Title - SAMS phpmysql author : Author - SAMS MySQLmysql title : Title - SAMS MySQL----

And some example in java

abstract class Pizza {  public abstract int getPrice(); // count the cents}

class HamAndMushroomPizza extends Pizza {  public int getPrice()   {    return 850;  }}

class DeluxePizza extends Pizza {  public int getPrice()   {    return 1050;  }}

class HawaiianPizza extends Pizza {  public int getPrice()   {    return 1150;  }}

class PizzaFactory {  public enum PizzaType   {    HamMushroom,      Deluxe,      Hawaiian  }

  public static Pizza createPizza(PizzaType pizzaType)   {    switch (pizzaType)     {      case HamMushroom:        return new HamAndMushroomPizza();      case Deluxe:        return new DeluxePizza();      case Hawaiian:        return new HawaiianPizza();    }    throw new IllegalArgumentException("The pizza type " + pizzaType + " is not recognized.");  }}

class myPizza{  // Create all available pizzas and print their prices  public static void main (String args[])   {    for (PizzaFactory.PizzaType pizzaType : PizzaFactory.PizzaType.values())     {      System.out.println("Price of " + pizzaType + " is " + PizzaFactory.createPizza(pizzaType).getPrice());    }  }}

Output : 

$ java myPizza  Price of HamMushroom is 850 Price of Deluxe is 1050 Price of Hawaiian is 1150

Design patterns : Builder

The purpose of a builder is to separate the construction process of a complex object from its representation so that the same construction process can be used to create different representations.

The participating actors are a “director” which interprets the information and invokes the “builder” to get the object built. The builder creates parts of a complex object each time it is called and maintains the state of the object. Eventually when the product is complete, the client can retrieve the product from the “builder”.

Lets check out some code:

In PHP

abstract class AbstractPageBuilder //abstract builder{  abstract function getPage();}

abstract class AbstractPageDirector //abstract director{  abstract function __construct(AbstractPageBuilder $builder_in);  abstract function buildPage();  abstract function getPage();}

class HTMLPage //product {  private $page = NULL;  private $page_title = NULL;  private $page_heading = NULL;  private $page_text = NULL;  function __construct()   {  }  function showPage()   {    return $this->page;  }  function setTitle($title_in)   {    $this->page_title = $title_in;  }  function setHeading($heading_in)   {    $this->page_heading = $heading_in;  }  function setText($text_in)   {    $this->page_text .= $text_in;  }  function formatPage()   {    $this->page  = "\n";    $this->page .= "".$this->page_title."\n";    $this->page .= "\n";    $this->page .= "

".$this->page_heading."

\n"; $this->page .= $this->page_text; $this->page .= "\n\n"; $this->page .= ""; }} class HTMLPageBuilder extends AbstractPageBuilder //concrete builder{ private $page = NULL; function __construct() { $this->page = new HTMLPage(); } function setTitle($title_in) { $this->page->setTitle($title_in); } function setHeading($heading_in) { $this->page->setHeading($heading_in); } function setText($text_in) { $this->page->setText($text_in); } function formatPage() { $this->page->formatPage(); } function getPage() { return $this->page; }} class HTMLPageDirector extends AbstractPageDirector //concrete director{ private $builder = NULL; public function __construct(AbstractPageBuilder $builder_in) { $this->builder = $builder_in; } public function buildPage() { $this->builder->setTitle('Testing HTMLPage Title'); $this->builder->setHeading('Testing HTMLPage Heading'); $this->builder->setText('Body1 Testing, testing, testing!'); $this->builder->setText('Body2 Testing, testing, testing, or!'); $this->builder->setText('Body3 Testing, testing, testing, more!'); $this->builder->formatPage(); } public function getPage() { return $this->builder->getPage(); }} echo("BEGIN\n"); $pageBuilder = new HTMLPageBuilder();$pageDirector = new HTMLPageDirector($pageBuilder);$pageDirector->buildPage();$page = $pageDirector->GetPage();echo $page->showPage();echo "\nEND\n"; Output: BEGIN Testing HTMLPage Title

Testing HTMLPage Heading

Body1 Testing, testing, testing!Body2 Testing, testing, testing, or!Body3 Testing, testing, testing, more! END

The classic pizza example in java

/* "Product" */class Pizza {  private String dough = "";  private String sauce = "";  private String topping = "";

  public void setDough(String dough)     { this.dough = dough; }  public void setSauce(String sauce)     { this.sauce = sauce; }  public void setTopping(String topping) { this.topping = topping; }

  public void showPizza()  {    System.out.println("PIZZA........");    System.out.println("Dough : "+this.dough);    System.out.println("Sauce : "+this.sauce);    System.out.println("Topping : "+this.topping);    System.out.println("----------");  }}

/* "Abstract Builder" */abstract class PizzaBuilder {  protected Pizza pizza;

  public Pizza getPizza() { return pizza; }  public void createNewPizzaProduct()   {     pizza = new Pizza();   }

  public abstract void buildDough();  public abstract void buildSauce();  public abstract void buildTopping();

}

/* "ConcreteBuilder" */class HawaiianPizzaBuilder extends PizzaBuilder {  public void buildDough()   { pizza.setDough("cross"); }  public void buildSauce()   { pizza.setSauce("mild"); }  public void buildTopping() { pizza.setTopping("ham+pineapple"); }}

/* "ConcreteBuilder" */class SpicyPizzaBuilder extends PizzaBuilder {  public void buildDough()   { pizza.setDough("pan baked"); }  public void buildSauce()   { pizza.setSauce("hot"); }  public void buildTopping() { pizza.setTopping("pepperoni+salami"); }}

/* "Director" */class Waiter {  private PizzaBuilder pizzaBuilder;

  public void setPizzaBuilder(PizzaBuilder pb) { pizzaBuilder = pb; }  public Pizza getPizza() { return pizzaBuilder.getPizza(); }

  public void constructPizza()   {    pizzaBuilder.createNewPizzaProduct();    pizzaBuilder.buildDough();    pizzaBuilder.buildSauce();    pizzaBuilder.buildTopping();  }}

/* A customer ordering a pizza. */public class BuilderExample {  public static void main(String[] args)   {    Waiter waiter = new Waiter();    PizzaBuilder hawaiian_pizzabuilder = new HawaiianPizzaBuilder();    PizzaBuilder spicy_pizzabuilder = new SpicyPizzaBuilder();

    waiter.setPizzaBuilder( hawaiian_pizzabuilder );    waiter.constructPizza();

    Pizza pizza = waiter.getPizza();    pizza.showPizza();  }}

Output:

PIZZA........Dough : crossSauce : mildTopping : ham+pineapple----------

Design patterns : Abstract Factory

The abstract factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It encapsulates the possibility of creation of a suite of “products” which otherwise would have required a sequence of “if .. then .. else” statements. The abstract factory has the responsibility for providing creation services for the entire family of objects. Clients never create the objects directly – they ask the factory to do that for them.

This mechanism makes changing the entiry family of objects easy – because the specific class of factory object appears only once in the application – where it was instantiated. The application can replace the entire family of objects by simply instantiating a different instance of the abstract factory. It also provides for lazy creation of objects.

The participating actors here are the client, the abstract factory, the abstract product, the concrete factory and the concrete product. The abstract factory defines which objects the concrete factory will need to be able to create. The concrete factory must create the correct objects for its context, ensuring that all objects created by the concrete factory are able to work correctly for a given circumstance.

Lets take an example to explain the situation. The AbstractBookFactory specifies that two classes – the AbstractPHPBook and the AbstractMySQLBook will need to be created by the concrete factory. The concreate factory OReillyBookFactory extends the AbstractBookFactory and can create the objects for OReillyPHPBook and OReillyMySQLBook which are correct classes for the context of OReilly.

abstract class AbstractBookFactory{  abstract function makePHPBook();  abstract function makeMySQLBook();}

//for objects in context of OReillyclass OReillyBookFactory extends AbstractBookFactory{  private $context = "OReilly";  function makePHPBook()  {    return new OReillyPHPBook;  }  function makeMySQLBook()  {    return new OReillyMySQLBook;  }}

//for objects in context of Samsclass SamsBookFactory extends AbstractBookFactory{  private $context = "Sams";  function makePHPBook()  {    return new SamsPHPBook;  }  function makeMySQLBook()  {    return new SamsMySQLBook;  }}

//Classes for booksabstract class AbstractBook{  abstract function getAuthor();  abstract function getTitle();}

abstract class AbstractPHPBook{  private $subject = "PHP";}

abstract class AbstractMySQLBook{  private $subject = "MySQL";}

class OReillyPHPBook extends AbstractPHPBook{  private $author;  private $title;

  function __construct()  {    $this->author = "OReilly : php author 1";    $this->title = "OReilly : title for php";  }

  function getAuthor()  {    return $this->author;  }

  function getTitle()  {    return $this->title;  }}

class SamsPHPBook extends AbstractPHPBook{  private $author;  private $title;

  function __construct()  {    $this->author = "Sams : php author";    $this->title = "Sams : title for php";  }

  function getAuthor()  {    return $this->author;  }

  function getTitle()  {    return $this->title;  }}

class OReillyMySQLBook extends AbstractMySQLBook{  private $author;  private $title;

  function __construct()  {    $this->author = "OReilly : MySQL author";    $this->title = "OReilly : title for MySQL";  }

  function getAuthor()  {    return $this->author;  }

  function getTitle()  {    return $this->title;  }}

class SamsMySQLBook extends AbstractMySQLBook{  private $author;  private $title;

  function __construct()  {    $this->author = "Sams : MySQL author";    $this->title = "Sams : title for MySQL";  }

  function getAuthor()  {    return $this->author;  }

  function getTitle()  {    return $this->title;  }}

//testing 

echo("Begin\n");echo("Testing OReillyBookFactory\n");$bookFactoryInstance = new OReillyBookFactory;testConcreteFactory($bookFactoryInstance);echo("----\n");

echo("Testing SamsBookFactory\n");$bookFactoryInstance = new SamsBookFactory;testConcreteFactory($bookFactoryInstance);echo("----\n");

function testConcreteFactory($bookFactoryInstance){  $phpBook = $bookFactoryInstance->makePHPBook();  echo "php author : ".$phpBook->getAuthor()."\n";  echo "php title : ".$phpBook->getTitle()."\n";

  $mysqlBook = $bookFactoryInstance->makeMySQLBook();  echo "mysql author : ".$mysqlBook->getAuthor()."\n";  echo "mysql title : ".$mysqlBook->getTitle()."\n";}

//Output

BeginTesting OReillyBookFactoryphp author : OReilly : php author 1php title : OReilly : title for phpmysql author : OReilly : MySQL authormysql title : OReilly : title for MySQL----Testing SamsBookFactoryphp author : Sams : php authorphp title : Sams : title for phpmysql author : Sams : MySQL authormysql title : Sams : title for MySQL----

Another example using java

interface GUIFactory //Abstract Factory {  public Button createButton();}

class WinFactory implements GUIFactory //concrete Factory{  public Button createButton()   {    return new WinButton();  }}

class OSXFactory implements GUIFactory //Concrete Factory{  public Button createButton()   {    return new OSXButton();  }}

interface Button //Abstract Product {  public void paint();}

class WinButton implements Button //concrete Product{  public void paint()   {    System.out.println("I'm a WinButton");  }}

class OSXButton implements Button //concrete Product{  public void paint()   {    System.out.println("I'm an OSXButton");  }}

class Application //execution free of product type {  public Application(GUIFactory factory)  {    Button button = factory.createButton();    button.paint();  }}

public class ApplicationRunner {  public static void main(String[] args)   {    if(args.length != 1)    {      System.out.println("usage : java ApplicationRunner ");      System.exit(1);    }    new Application(createOsSpecificFactory(args[0]));  }

  public static GUIFactory createOsSpecificFactory(String ostype)   {    if (ostype.equals("0"))    {      return new WinFactory();    }     else     {      return new OSXFactory();    }  }}

Output------

$ java ApplicationRunner 0I'm a WinButton$ java ApplicationRunner 1I'm an OSXButton

Restore grub 2 after windows installation

Here is the step by step guide to recover Grub 2 (with ubuntu 9.10) after windows install. The steps are different than for recovering Grub 1 (as explained in this post:

You will need a LIVE cd if you are going to recover an Ubuntu Box. Boot the system with Live CD (I assume you are using Ubuntu Live CD). Press Alt+F2 and enter gnome-terminal command. And continue typing :

$sudo fdisk -l

This will show your partition table:

Device Boot Start End Blocks Id System
/dev/sda1 * 1 12748 102398278+ 7 HPFS/NTFS
/dev/sda2 12749 60800 385977690 f W95 Ext’d (LBA)
/dev/sda5 12749 13003 2048256 82 Linux swap / Solaris
/dev/sda6 13004 16827 30716248+ 83 Linux
/dev/sda7 16828 25272 67834431 83 Linux
/dev/sda8 25273 32125 55046302+ 7 HPFS/NTFS
/dev/sda9 32126 60800 230331906 7 HPFS/NTFS

Now we need to mount Linux (sda6 and sda7 here). sda6 is the / partition and sda7 is the /home partition. So mount them accordingly. If you have any other partitions (specially boot partition) dont forget to mount them.

$sudo mount /dev/sda6 /mnt
$sudo mount /dev/sda7 /mnt/home
$sudo mount –bind /dev /mnt/dev
$sudo mount –bind /proc /mnt/proc
$sudo mount –bind /sys /mnt/sys

Now chroot into the enviroment we made :

$sudo chroot /mnt

You may want to edit /etc/default/grub file to fit your system (timeout options etc)

$vi /etc/default/grub

Once you are thru install grub2 by using

$grub-install /dev/sda

If you get errors with that code use:

$grub-install –recheck /dev/sda

Now you can exit the chroot, umount the system and reboot your box :

$exit
$sudo umount /mnt/home
$sudo umount /mnt/sys
$sudo umount /mnt/dev
$sudo umount /mnt/proc
$sudo umount /mnt
$sudo reboot

Design patterns : Singleton pattern

Singleton design pattern is required when you want to allow only one instance of the class to be created. Database connections and filesystems are examples where singleton classes might be required. You can also use a singleton class to store variables which need global access – thereby limiting the scope of those variables and keeping the global space variable free.

With singleton design pattern, following points need to be ensured

  • Ensure that only a single instance of the class is created.
  • Provide a global point of access to the object.
  • The creation of the object should be thread safe to avoid multiple instances in a multi-threaded environment.

An illustration in java (from wikipedia)

public class Singleton {  // Private constructor prevents instantiation from other classes  private Singleton() {}

  /**   * SingletonHolder is loaded on the first execution of Singleton.getInstance()    * or the first access to SingletonHolder.INSTANCE, not before.  */  private static class SingletonHolder   {     private static final Singleton INSTANCE = new Singleton();  }

  public static Singleton getInstance()   {    return SingletonHolder.INSTANCE;  }}

Another illustration in php

class singleton{  private static $_instance;

  //private constructor to prevent instantiation from other classes  private function __construct()  {  }

  private function __destruct()  {  }

  // only first call to getInstance creates and returns an instance.   // next call returns the already created instance.  public static function getInstance()  {    if(self::$_instance === null)    {      self::$_instance = new self();    }    return self::$_instance;  }

}

How to get your passport made in simple easy steps.

Prelude : If you have the time, the patience and the ability to coerce and bribe people efficiently, you can save money by going directly to the passport office and interacting with the peon there. It seems that with good interaction skills, the passport can be had in a weeks time with a bribe of only 1000.

Preparation : If you lack the time, then, be prepared to spend around 4K. The list of documents required would be your
=> ID proof – pan card, Driving licence.
=> Residence proof – electricity bill, voter id card.
=> Age proof – school leaving certificate.
=> References – people who could vouch for you – someone who stays close by.

Step 1 : Look out for ads in newspapers for people who make passports. You can also ask people who hang around ATMs (offereing credit cards or policies). They generally work in association with the passport offices to “easily” get your form filled and application passed. When you are filling the application, remember to write your name and sign. Give all documents – they should not need the original documents (because they have some setup with the passport office). And pay them their “form submission fee” – which is passport fee + agent fee = around 2K. You can bargain. There are also agencies who specialize in making of passport and pan-card.

Step 2 : Followup with the “agent” to get the receipt of submission of application for passport. The actual process of creation of passport starts only after the application is submitted. If your agent has good connections and has greased the palms nicely with the money you have given, then you would get the receipt very fast.

Step 3 : Once the application receipt is in your hand – have another list of documents ready and wait –
=> ID proof
=> Residence proof – which shows residence at the location for more than a year.
=> Age proof
=> Residence proof of past addresses – at least two location where you have stayed earlier.

Step 4 : In 7-15 days time you would get an official from Intelligence department who would come to your house for verification. He would ask for all sorts of documents and keep you running around the house. The point he basically wants to make is that unless you grease his palms, he wont sign and forward the documents further. If you can show him all the documents he asks for, you will have an edge over him and may be able to bargain with him for the bribe money. Ofcourse he would start the bargain at 1K, but if your negotiation skills are good, you can bring him down to 500. Another point he will make is the police department (to whom he will be forwarding your app) can be bargained with – but do not trust him. The less you pay in the process of making him happy – the better.

Step 5 : Again wait for the police official who will come in a week’s time. He will carry a file with him with maybe around 100 applications. He will start asking questions about where you have stayed earlier and the time of your stay. If you happen to carry residence proof of past addresses – you can have an edge over him. Else you have to just play along. If he threatens you that your past locations cannot be verified, dont worry – he is just trying to make you nervous – so he could extract more money in the bribe. Ask him gently about “how much would be needed to get the verification done ?” and he might ask for 1000 – again. If you try to bargain – he would say that he could make your application go around police stations for six months and your case will be lost. Dont worry – just bargain gently. If you tell him that the previous intelligence guy asked for the same amount – he would say that the guy had only a single sign to do – and he is supposed to get 3-4 signatures to do. Just play along and if possible, bring him down to 500. Pay him and wait.

Step 6 : Keep on checking the status of your passport online at http://passport.gov.in/. You will get a notification that your passport is on way. Keep a tab on the local post office and your snail-mail box.

Step 7 : A guy from the post may come to deliver the passport – within 15-20 days time. He would ask for “chai-pani”. Dont offer him tea. Simply hand him 20-50 rs and get your passport.

The point to note here is that at every stage a hand has to be greased. Even if the details given by you are wrong, you would still get your passport if the hands are greased properly. Only the amount of grease will vary. With the 6th pay commission in force now – you might have hoped that the officials would be less greedy. But it has only lead to increase in the greed of the government officials and now they charge more for than before.

Forking Vs. Threading

What is Fork/Forking?

Fork is nothing but a new process that looks exactly like the old or the parent process but still it is a different process with different process ID and having it’s own memory. Parent process creates a separate address space for child. Both parent and child process possess the same code segment, but execute independently from each other.

The simplest example of forking is when you run a command on shell in unix/linux. Each time a user issues a command, the shell forks a child process and the task is done.

When a fork system call is issued, a copy of all the pages corresponding to the parent process is created, loaded into a separate memory location by the OS for the child process, but in certain cases, this is not needed. Like in ‘exec’ system calls, there is not need to copy the parent process pages, as execv replaces the address space of the parent process itself.

Few things to note about forking are:

  • The child process will be having it’s own unique process ID.
  • The child process shall have it’s own copy of parent’s file descriptor.
  • File locks set by parent process shall not be inherited by child process.
  • Any semaphores that are open in the parent process shall also be open in the child process.
  • Child process shall have it’s own copy of message queue descriptors of the parents.
  • Child will have it’s own address space and memory.

Fork is universally accepted than thread because of the following reasons:

  • Development is much easier on fork based implementations.
  • Fork based code a more maintainable.
  • Forking is much safer and more secure because each forked process runs in its own virtual address space. If one process crashes or has a buffer overrun, it does not affect any other process at all.
  • Threads code is much harder to debug than fork.
  • Fork are more portable than threads.
  • Forking is faster than threading on single cpu as there are no locking over-heads or context switching.

Some of the applications in which forking is used are: telnetd(freebsd), vsftpd, proftpd, Apache13, Apache2, thttpd, PostgreSQL.

Pitfalls in Fork:

  • In fork, every new process should have it’s own memory/address space, hence a longer startup and stopping time.
  • If you fork, you have two independent processes which need to talk to each other in some way. This inter-process communication is really costly.
  • When the parent exits before the forked child, you will get a ghost process. That is all much easier with a thread. You can end, suspend and resume threads from the parent easily. And if your parent exits suddenly the thread will be ended automatically.
  • In-sufficient storage space could lead the fork system to fail.

What are Threads/Threading?

Threads are Light Weight Processes (LWPs). Traditionally, a thread is just a CPU (and some other minimal state) state with the process containing the remains (data, stack, I/O, signals). Threads require less overhead than “forking” or spawning a new process because the system does not initialize a new system virtual memory space and environment for the process. While most effective on a multiprocessor system where the process flow can be scheduled to run on another processor thus gaining speed through parallel or distributed processing, gains are also found on uniprocessor systems which exploit latency in I/O and other system functions which may halt process execution.

Threads in the same process share:
== Process instructions
== Most data
== open files (descriptors)
== signals and signal handlers
== current working directory
== User and group id

Each thread has a unique:
== Thread ID
== set of registers, stack pointer
== stack for local variables, return addresses
== signal mask
== priority
== Return value: errno

Few things to note about threading are:

  • Thread are most effective on multi-processor or multi-core systems.
  • For thread – only one process/thread table and one scheduler is needed.
  • All threads within a process share the same address space.
  • A thread does not maintain a list of created threads, nor does it know the thread that created it.
  • Threads reduce overhead by sharing fundamental parts.
  • Threads are more effective in memory management because they uses the same memory block of the parent instead of creating new.

Pitfalls in threads:

  • Race conditions: The big loss with threads is that there is no natural protection from having multiple threads working on the same data at the same time without knowing that others are messing with it. This is called race condition. While the code may appear on the screen in the order you wish the code to execute, threads are scheduled by the operating system and are executed at random. It cannot be assumed that threads are executed in the order they are created. They may also execute at different speeds. When threads are executing (racing to complete) they may give unexpected results (race condition). Mutexes and joins must be utilized to achieve a predictable execution order and outcome.
  • Thread safe code: The threaded routines must call functions which are “thread safe”. This means that there are no static or global variables which other threads may clobber or read assuming single threaded operation. If static or global variables are used then mutexes must be applied or the functions must be re-written to avoid the use of these variables. In C, local variables are dynamically allocated on the stack. Therefore, any function that does not use static data or other shared resources is thread-safe. Thread-unsafe functions may be used by only one thread at a time in a program and the uniqueness of the thread must be ensured. Many non-reentrant functions return a pointer to static data. This can be avoided by returning dynamically allocated data or using caller-provided storage. An example of a non-thread safe function is strtok which is also not re-entrant. The “thread safe” version is the re-entrant version strtok_r.

Advantages in threads:

  • Threads share the same memory space hence sharing data between them is really faster means inter-process communication (IPC) is real fast.
  • If properly designed and implemented threads give you more speed because there aint any process level context switching in a multi threaded application.
  • .Threads are really fast to start and terminate

Some of the applications in which threading is used are: MySQL, Firebird, Apache2, MySQL 323

FAQ’s:

1. Which should i use in my application ?

Ans: That depends on a lot of factors. Forking is more heavy-weight than threading, and have a higher startup and shutdown cost. Interprocess communication (IPC) is also harder and slower than interthread communication. Actually threads really win the race when it comes to inter communication. Conversely, whereas if a thread crashes, it takes down all of the other threads in the process, and if a thread has a buffer overrun, it opens up a security hole in all of the threads.

which would share the same address space with the parent process and they only needed a reduced context switch, which would make the context switch more efficient.

2. Which one is better, threading or forking ?

Ans: That is something which totally depends on what you are looking for. Still to answer, In a contemporary Linux (2.6.x) there is not much difference in performance between a context switch of a process/forking compared to a thread (only the MMU stuff is additional for the thread). There is the issue with the shared address space, which means that a faulty pointer in a thread can corrupt memory of the parent process or another thread within the same address space.

3. What kinds of things should be threaded or multitasked?

Ans: If you are a programmer and would like to take advantage of multithreading, the natural question is what parts of the program should/ should not be threaded. Here are a few rules of thumb (if you say “yes” to these, have fun!):

  • Are there groups of lengthy operations that don’t necessarily depend on other processing (like painting a window, printing a document, responding to a mouse-click, calculating a spreadsheet column, signal handling, etc.)?
  • Will there be few locks on data (the amount of shared data is identifiable and “small”)?
  • Are you prepared to worry about locking (mutually excluding data regions from other threads), deadlocks (a condition where two COEs have locked data that other is trying to get) and race conditions (a nasty, intractable problem where data is not locked properly and gets corrupted through threaded reads & writes)?
  • Could the task be broken into various “responsibilities”? E.g. Could one thread handle the signals, another handle GUI stuff, etc.?

Conclusions:

1. Whether you have to use threading or forking, totally depends on the requirement of your application.
2. Threads more powerful than events, but power is not something which is always needed.
3. Threads are much harder to program than forking, so only for experts.
4. Use threads mostly for performance-critical applications.

source : http://www.geekride.com/index.php/2010/01/fork-forking-vs-thread-threading-linux-kernel/

Follow

Get every new post delivered to your Inbox.