• 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类即可.

  • 相关阅读:
    java 爬虫 爬取豆瓣 请不要害羞 图片
    Intellij idea 基本配置
    Linux 基本操作
    Java 快速排序
    Codeforces 986D Perfect Encoding FFT
    Codeforces 749E Gosha is hunting 二分+DP
    BZOJ5305 [Haoi2018]苹果树
    Codeforces 666E Forensic Examination SAM+权值线段树
    BZOJ3712[PA2014]Fiolki 建图+倍增lca
    Codeforces 576D Flights for Regular Customers 矩阵快速幂+DP
  • 原文地址:https://www.cnblogs.com/juzi-123/p/5644149.html
Copyright © 2020-2023  润新知