Thursday, 4 January 2018

Design Patterns | Facade pattern

The facade pattern is a wrapper of many other interfaces in a result to produce a simpler interface.

Facade hides the complexities of the system and provides an interface to the client from where the client can access the system easily. A facade is an object that provides a simplified interface to a larger body of code, such as a class library.






Example
This is an abstract example of how a client ("you") interacts with a facade (the "computer") to a complex system (internal computer parts, like CPU and HardDrive).

/* Complex parts */
class CPU {
      public void freeze() {
            ...
      }
      public void jump(long position) {
            ...
      }
      public void execute() {
            ...
      }
}

class Memory {
      public void load(long position, byte[] data) {
            ...
      }
}

class HardDrive {
      public byte[] read(long lba, int size) {
            ...
      }
}

/* Facade */

class ComputerFacade {
      private CPU processor;
      private Memory ram;
      private HardDrive hd;

      public ComputerFacade() {
            this.processor = new CPU();
            this.ram = new Memory();
            this.hd = new HardDrive();
      }

      public void start() {
            processor.freeze();
            ram.load(BOOT_ADDRESS, hd.read(BOOT_SECTOR, SECTOR_SIZE));
            processor.jump(BOOT_ADDRESS);
            processor.execute();
      }
}

/* Client */

class Facade {
      public static void main(String[] args) {
            ComputerFacade computer = new ComputerFacade();
            computer.start();
      }
}

Points to remember about fa├žade design pattern
1. Wrap a poorly designed collection of APIs with a single well-designed API.
2. Reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
3. Facade design pattern is more like a helper for client applications; it doesn’t hide subsystem interfaces from the client. Whether to use Facade or not is completely dependent on client code.
4. Facade design pattern can be applied at any point of development, usually when the number of interfaces grows and the system gets complex.
5. Subsystem interfaces are not aware of Facade and they shouldn’t have any reference of the Facade interface.

We can use Factory pattern with Facade to provide the better interface to client systems.

Sources

7 comments:

  1. Your potential customers are actually counting on local and cellular search engines and directories to locate - and visit - search engine optimization support.
    professional logo design services

    ReplyDelete
  2. https://hemphousenc.com/ Cool stuff you have got and you keep update all of us.

    ReplyDelete
  3. http://cuttingedgewindowtinting.co/ This content is written very well. Your use of formatting when making your points makes your observations very clear and easy to understand. Thank you.

    ReplyDelete
  4. landscaping We are a professional detailing services company serving Harford and Baltimore counties in northern Maryland. Call (443) 756-3000 for all your detailing needs

    ReplyDelete
  5. CBD hand sanitizer We are Tennessee’s first and only CBD/Hemp Dispensary owned and managed by a Pharmacist and staffed by Medical Professionals certified in Cannabinoid Pharmacotherapy. We offer an environment that is warm and inviting.

    ReplyDelete
  6. https://www.emergencyelectriciangreensboro.com/blog/24-hours-electrician-best-electrician-of-greensboro At Emergency Electrician Greensboro, we understand how much you rely on electrical energy to power your everyday life. Fortunately, we are experts regarding safeguarding against its inherent risks and are all ready to do the reliable labor had to make sure your home's power systems work for decades to come.

    ReplyDelete
  7. Fayetteville NC Once Upon A Child® buys and sells gently used kids' clothing, shoes, toys, and baby gear. We pay you cash on the spot and provide a convenient way to recycle the items your children have outgrown, giving those items a second life. Every day you’ll find an ever-changing selection of top name brands at up to 70% off regular retail prices. Get paid and save for being eco-friendly!

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...