zoukankan      html  css  js  c++  java
  • 2016-7-5 Design Patterns : Facade

    问题:

    class ComputerShop {
        public boolean isHaveCPU = true;
        public boolean isHaveHardDisk = true;
        public boolean isHaveMemory = true;
    }
    
    class Customer {
        private static Customer customer = null;
        private static ComputerShop shop = null;
    
        public static Customer getCustomer() {
            if (customer == null) {
                customer = new Customer();
                shop = new ComputerShop();
            }
    
            return customer;
        }
    
        public ComputerShop getComputerShop() {
            return shop;
        }
    }
    
    class GetCPU {
        public void buy() {
            ComputerShop shop = Customer.getCustomer().getComputerShop();
            if (shop.isHaveCPU) {
                System.out.println("buy cpu");
            }
        }
    }
    
    class GetHardDisk {
        public void buy() {
            ComputerShop shop = Customer.getCustomer().getComputerShop();
            if (shop.isHaveHardDisk) {
                System.out.println("buy harddisk");
            }
        }
    }
    
    class GetMemory {
        public void buy() {
            ComputerShop shop = Customer.getCustomer().getComputerShop();
            if (shop.isHaveMemory) {
                System.out.println("buy memory");
            }
        }
    }
    
    public class Problem {
        public static void main(String[] args) {
    
            new GetCPU().buy();
            new GetHardDisk().buy();
            new GetMemory().buy();
        }
    }

    有何问题:

    get各个零件需要与customer内部的模块进行多次交互

    一旦computershop中出现品种变化(CPU下架了),那么main(调用)中getcpu也要进行修改了

    解决:

    class Facade {
        public void onestop() {
            new GetCPU().buy();
            new GetHardDisk().buy();
            new GetMemory().buy();
        }
    }
    
    public class Resolve {
        public static void main(String[] args) {
            new Facade().onestop();
        }
    }

    增加一个Facade类即可.

  • 相关阅读:
    HDU4003 Find Metal Mineral
    POJ1125 Stockbroker Grapevine
    HDU4028The time of a day
    弱校ACM奋斗史
    POJ1236 Network of Schools
    HDU4004 The Frog's Games
    HDU4001 To Miss Our Children Time
    POJ2186 Popular Cows
    POJ1094 Sorting It All Out
    hadoop2.7.1单机和伪集群的搭建0
  • 原文地址:https://www.cnblogs.com/juzi-123/p/5644149.html
Copyright © 2011-2022 走看看