Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Facade Structural Design Pattern

hides the complexities of the system by a class that provide a simple interface to a complex subsystem which contains lots of moving parts.

Sections

Definitions

tutorialspoint
  • is a structural pattern.
  • this pattern adds an interface to existing system to hide its complexities.
  • and provides an interface to the client using which the client can access the system.
  • Wikipedia
  • a structural pattern
  • provides a simplified interface to a library, a framework, or any other complex set of classes.

  • Facade describe how to solve recurring design problems
  • to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

  • What Problem Facade Solve

    • Imagine that you must make your code work with a broad set of objects that belong to a sophisticated library or framework.
    • Ordinarily, you’d need to initialize all of those objects, keep track of dependencies, execute methods in the correct order, and so on.

    As a result, the business logic of your classes would become tightly coupled to the implementation details of 3rd-party classes, making it hard to comprehend and maintain.


    Solution

    • A facade is a class that provides a simple interface to a complex subsystem which contains lots of moving parts.

    • A facade might provide limited functionality in comparison to working with the subsystem directly.

    • However, it includes only those features that clients really care about.

    • Having a facade is handy when you need to integrate your app with a sophisticated library that has dozens of features, but you just need a tiny bit of its functionality.

    Examples

    Morning Facade Example

    source: GOF scottt2 design-patterns-in-dart facade

    all classes needed to be implemented

    class Grinder {
      String _type;
    
      Grinder(this._type);
      void grind() => print("Grinding $_type!");
    }
    
    class Maker {
      String _type;
    
      Maker(this._type);
      void fill() => print("Filling the $_type maker!");
      void retrieve() => print("Retrieving the $_type!");
      void start() => print("Starting the $_type maker!");
    }
    
    class Imbiber {
      String _beverage;
    
      Imbiber(this._beverage);
      void drink() => print("Mmmmm...drinking $_beverage!");
    }

    facade class

    class MorningFacade {
      final _coffeeDrinker = Imbiber("coffee");
      final _coffeeGrinder = Grinder("coffee beans");
      final _coffeeMaker = Maker("coffee");
    
      void prepareCoffee() {
        print("\r\nPreparing the coffee...");
        _coffeeGrinder.grind();
        _coffeeMaker
          ..fill()
          ..start();
        print("Coffee is brewing!\r\n");
      }
    
      void drinkCoffee() {
        print("\r\nMust...have...coffee...");
        _coffeeMaker.retrieve();
        _coffeeDrinker.drink();
        print("This is damn fine coffee!");
      }
    }

    main() to test

    void main() {
      var typicalMorning = MorningFacade();
    
      print("Wake up! Grab a brush and put on a little makeup...");
      print("\r\nStumble to the kitchen...");
    
      typicalMorning.prepareCoffee();
      print("Oh my...that smells good...");
    
      typicalMorning.drinkCoffee();
      print("\r\nI'm ready to attack the day!");
    }
    

    file converter example

    Shape Maker

    Sources