Thursday, May 18, 2017

Bridge Design Pattern

The Bridge pattern is designed to separate a class's interface from its implementation so you can vary or replace the implementation without changing the client code (see the Bridge pattern Wikipedia entry).

Lets see what that means in a simpler way.

In following example polymorphism is used inorder to give Person Class different implementations.
class Person{
public:
Person(){}
virtual void introduce() = 0;
};
class John : public Person{
public:
John(){}
void introduce(){
//print I'm John
}
};
class Ann : public Person{
public:
Ann(){}
void introduce(){
//print I'm Ann
}
};
view raw BridgeDP_1.cpp hosted with ❤ by GitHub

---
There are lot of people by same name. Say we want to introduce a new property home town.
If we follow this pattern code would be as follows.

class Person{
public:
Person(){}
virtual void introduce() = 0;
};
class John1 : public Person{
public:
John(){}
void introduce(){
//print I'm John and I'm from Newyork
}
};
class John2 : public Person{
public:
John(){}
void introduce(){
//print I'm John and I'm from Los angeles
}
};
class Ann1 : public Person{
public:
Ann(){}
void introduce(){
//print I'm Ann and I'm from Newyork
}
};
class Ann2 : public Person{
public:
Ann(){}
void introduce(){
//print I'm Ann and I'm from Los angeles
}
};
view raw BridgeDP_2.cpp hosted with ❤ by GitHub
---
That is code is hard to maintain and reusability is low. We can use Bridge design pattern to improve that code by seperating implementation of home town.
---
class Person{
public:
Person(){}
virtual void introduce(Town t) = 0;
};
class Town{
public:
Town(){}
virtual void name() = 0;
};
class Newyork : public Town {
void name(){
//print NewYork
}
};
class LA : public Town {
void name(){
//print Los angeles
}
};
class John1 : public Person{
public:
John(){}
void introduce(Town t){
//print I'm John and I'm from
t.name();
}
};
class Ann1 : public Person{
public:
Ann(){}
void introduce(Town t){
//print I'm Ann and I'm from
t.name();
}
};
view raw BridgeDP_3.cpp hosted with ❤ by GitHub
---

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...