A quick guide on the Facade design pattern

Discussion in 'Programming General' started by Prism, Nov 19, 2014.

A quick guide on the Facade design pattern
  1. Unread #1 - Nov 19, 2014 at 11:22 PM
  2. Prism
    Joined:
    Nov 19, 2014
    Posts:
    6
    Referrals:
    0
    Sythe Gold:
    0

    Prism Newcomer

    A quick guide on the Facade design pattern

    What is it?

    The Facade design pattern is a commonly used pattern which pretty much every programming language API uses for general usage. In this design pattern, you use a single class to hide the details of a complex object, or even a set of classes. For example, java.net.InetAddress, its purpose is to connect to a specific URL and retrieve information about the host, etc. however it hides all of the complex detail (ex. using DNS to get the IP address of a host).

    Simple example from Wikipedia:

    Code:
    /* 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 You {
        public static void main(String[] args) {
            ComputerFacade computer = new ComputerFacade();
            computer.start();
        }
    }
    If you have any questions, feel free to ask me! :)
     
  3. Unread #2 - Nov 20, 2014 at 3:01 AM
  4. 70i
    Joined:
    Jan 11, 2014
    Posts:
    462
    Referrals:
    0
    Sythe Gold:
    174

    70i Forum Addict
    Banned

    A quick guide on the Facade design pattern

    boooo
     
  5. Unread #3 - Nov 20, 2014 at 2:50 PM
  6. Prism
    Joined:
    Nov 19, 2014
    Posts:
    6
    Referrals:
    0
    Sythe Gold:
    0

    Prism Newcomer

    A quick guide on the Facade design pattern

    Whats wrong?
     
< where to start programing | >

Users viewing this thread
1 guest


 
 
Adblock breaks this site