Facade refers to the front of a building, facing a street or open space.
The Facade pattern is a structural design pattern that provides a simplified and unified interface to a complex system
For example, you have client side where, you need to start AudioPlayback,
for that you need Decoder, Player, Resampler, Mixer, VolumeController etc.
1 Way is that client creates all these objects and maintain the state of these, which will create 2 main problem:
- Client needs to learn a lot of things about state machine.
- Increases the dependency of various components in client side.
So, What is the alternative ?
May be encapsulate these items in a Facade.
We can expose a Facade, which internally know what is the state design, and expose only simple API to client
// If there are multiple components, don't let client depend on them
class Decoder {
public:
void decode() {
cout <<"decoding" <<endl;
}
};
class Player {
public:
void play() {
cout <<"playing" <<endl;
}
};
class Resampler {
public:
void resample() {
cout <<" resampling " <<endl;
}
};
class Mixer {
public:
void mix() {
cout <<"mixing " <<endl;
}
};
class PlayerFacade {
Decoder decoder;
Player player;
Resampler resampler;
Mixer mixer;
public:
void play() {
decoder.decode();
player.play();
resampler.resample();
mixer.mix();
}
};
int main() {
// 1 way is client makes all objects and class the
// respective methods, otherwise use facade.
PlayerFacade pf;
pf.play();
}
Follow more posts @ https://jdecodes.wordpress.com
My all design pattern articles :
- https://jdecodes.wordpress.com/2024/07/13/builder-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/command-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/iterator-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/mediator-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/state-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/memento-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/observer-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/strategy-pattern/
- https://jdecodes.wordpress.com/2024/07/20/visitor-design-pattern/
- https://jdecodes.wordpress.com/2024/07/20/adapter-desing-pattern/
- https://jdecodes.wordpress.com/2024/07/20/bridge-design-pattern/
- https://jdecodes.wordpress.com/2024/07/22/composite-desing-pattern/
- https://jdecodes.wordpress.com/2024/07/22/facade-design-pattern/
- https://jdecodes.wordpress.com/2024/07/22/decorater-design-pattern/
Leave a comment