Design Patterns : Fascade Pattern

Fascade, name being an analogy to an architectural fascade, intends set the tone for the entire functionality by providing a simplified yet effective interface wrapping away the complex, pooly designed collection of APIs.

Wrong Code

Consider following set of Classes.

public class User : IUser
{
        int _uInstance;
        private bool _IsRunning = false;

        public User(int UserID) => _uInstance = UserID;
        public void StartRunning()
        {
            Console.WriteLine($"User {_uInstance} Running");
            _IsRunning = true;
        }
        public void StopRunning(int UserID) => _IsRunning = false;
        
}

public class GPSTracking : IGPSTracking
{
        public void Start() => Console.WriteLine($"GPS started");
        public void Stop() { }
        public void InitiatizeGPSTracker() { }
        public void TroubleShoot() { }
}

public class MusicPlayer : IMusicPlayer
{
        public void Start(string AlbumName,string Song) => Console.WriteLine($"Music Player - Song {Song} , Album {AlbumName}");
        public void Stop() { }
        public void SearchAlbum(string Song) { }
        public bool Loop { get; set; }
        public void Next() { }
        public void Previous() { }
}

 

The Client Code could be

IUser userInstance = new User(1);
userInstance.StartRunning();

IGPSTracking gpsInstance = new GPSTracking();
gpsInstance.Start();

IMusicPlayer mplayerInstance = new MusicPlayer();
mplayerInstance.Start("MyAlbum","MySong");

As you can see, the Client class is invoking 3 APIs separately and the code look a mess.

Fascade In Action

Idea behind Fascade is to provide a simplified interface. Let’s introduce a Fascade Class now.

class FascadeClass
{
        IUser _uInstance;
        IGPSTracking _gInstance;
        IMusicPlayer _mInstance;

        string _Song;
        string _album;

        
        public FascadeClass(IUser uInstance,IGPSTracking gInstance, IMusicPlayer mInstance)
        {
            _uInstance = uInstance;
            _gInstance = gInstance;
            _mInstance = mInstance;

        }

        public void SetSong(string Album, string Song) { _Song = Song; _album = Album; }

        public void StartRunning()
        {
            _uInstance.StartRunning();
            _gInstance.Start();
            _mInstance.Start(_album, _Song);
        }

        public void StopRunning() { }
}

 

The Client Code would be now much cleaner as we have wrapped the complexity of IUser, IGPSTracking and IMusicPlayer within the Fascade Class.

 FascadeClass fInstance = new FascadeClass(new User(1), new GPSTracking(), new MusicPlayer());
fInstance.SetSong("MyAlbum", "MySong");
fInstance.StartRunning();

Code samples can be found here. Complete list of Patterns and Principles could be found here.

 

One thought on “Design Patterns : Fascade Pattern

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s