美文网首页
外观模式

外观模式

作者: keith666 | 来源:发表于2016-05-21 13:32 被阅读18次

Intent

  • Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
  • Wrap a complicated subsystem with a simpler interface.

Structure

facade by keith
  • 代码:
  • public class Facade {
        public static void main(String[] args) {
            Customer customer = new Customer("king");
            ServerFacade facade = new ServerFacade(new Server());
            facade.doServer(customer);
        }
    }
    class ServerFacade {
        Server server;
    
        public ServerFacade(Server server) {
            this.server = server;
        }
    
        public void doServer(Customer customer) {
            server.doCheck(customer);
            server.doService(customer);
            server.afterService(customer);
        }
    }
    class Customer {
        String name;
    
        public Customer(String name) {
            this.name = name;
        }
    }
    class Server {
        public void doCheck(Customer customer) {
            System.out.println(customer.name + " is checking");
        }
    
        public void doService(Customer customer) {
            System.out.println(customer.name + " is on service");
        }
    
        public void afterService(Customer customer) {
            System.out.println(customer.name + " is done server");
        }
    }
    
    1. Output
    king is checking
    king is on service
    king is done server
    

    Refenrence

    1. Design Patterns
    2. 设计模式

    相关文章

    网友评论

        本文标题:外观模式

        本文链接:https://www.haomeiwen.com/subject/fcforttx.html