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.
Lets see what that means in a simpler way.
In following example polymorphism is used inorder to give Person Class different implementations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}; |
---
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}; |
---
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.
---
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}; |
---