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:

  1. Client needs to learn a lot of things about state machine.
  2. 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 :

It’s Jdecoder

I am trying to decode the concepts into simple words and documenting items i know or currently learning.

Let’s connect

Design a site like this with WordPress.com
Get started