ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Design Pattern - Facade(Java)

2019-03-23 17:56:29  阅读:285  来源: 互联网

标签:Java chimomo Pattern public facade Facade class Chimomo


分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

Definition

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.

Participants

    The classes and/or objects participating in this pattern are:

  • Facade (MortgageApplication)
    • Knows which subsystem classes are responsible for a request.
    • Delegates client requests to appropriate subsystem objects.
  • Subsystem classes (Bank, Credit, Loan)
    • Implement subsystem functionality.
    • Handle work assigned by the Facade object.
    • Have no knowledge of the facade and keep no reference to it.

Sample Code in Java


This structural code demonstrates the Facade pattern which provides a simplified and uniform interface to a large subsystem of classes.

/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.sample;

/**
 * The 'Facade' class.
 *
 * @author Chimomo
 */
class Facade {

    // The sub system one.
    private SubSystemOne one;

    // The sub system two.
    private SubSystemTwo two;

    // The sub system three.
    private SubSystemThree three;

    // The sub system four.
    private SubSystemFour four;

    /**
     * Initializes a new instance of the "Facade" class.
     */
    public Facade() {
        this.one = new SubSystemOne();
        this.two = new SubSystemTwo();
        this.three = new SubSystemThree();
        this.four = new SubSystemFour();
    }

    /**
     * The method a.
     */
    public void methodA() {
        System.out.println("MethodA() ------ ");
        this.one.methodOne();
        this.two.methodTwo();
        this.four.methodFour();
    }

    /**
     * The method b.
     */
    public void methodB() {
        System.out.println("MethodB() ------ ");
        this.two.methodTwo();
        this.three.methodThree();
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.sample;

/**
 * Startup class for Structural Facade Design Pattern.
 *
 * @author Chimomo
 */
class Program {

    /**
     * Entry point into console application.
     *
     * @param args
     */
    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.methodA();
        facade.methodB();
    }

}

/*
Output:
MethodA() ------
 SubSystemOne Method
 SubSystemTwo Method
 SubSystemFour Method
MethodB() ------
 SubSystemTwo Method
 SubSystemThree Method
 */
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.sample;

/**
 * The 'Subsystem ClassD' class.
 *
 * @author Chimomo
 */
class SubSystemFour {

    /**
     * The method four.
     */
    public void methodFour() {
        System.out.println(" SubSystemFour Method");
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.sample;

/**
 * The 'Subsystem ClassA' class.
 *
 * @author Chimomo
 */
class SubSystemOne {

    /**
     * The method one.
     */
    public void methodOne() {
        System.out.println(" SubSystemOne Method");
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.sample;

/**
 * The 'Subsystem ClassC' class.
 *
 * @author Chimomo
 */
class SubSystemThree {

    /**
     * The method three.
     */
    public void methodThree() {
        System.out.println(" SubSystemThree Method");
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.sample;

/**
 * The 'Subsystem ClassB' class.
 *
 * @author Chimomo
 */
class SubSystemTwo {

    /**
     * The method two.
     */
    public void methodTwo() {
        System.out.println(" SubSystemTwo Method");
    }

}

This real-world code demonstrates the Facade pattern as a Mortgage Application object which provides a simplified interface to a large subsystem of classes measuring the credit worthiness of an applicant.

/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.realworld;

/**
 * The 'Subsystem ClassA' class.
 *
 * @author Chimomo
 */
class Bank {

    /**
     * The has sufficient savings.
     *
     * @param c
     * @param amount
     * @return
     */
    public boolean hasSufficientSavings(Customer c, int amount) {
        System.out.println("Check bank for " + c.getName());
        return true;
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.realworld;

/**
 * The 'Subsystem ClassB' class.
 *
 * @author Chimomo
 */
class Credit {

    /**
     * The has good credit.
     *
     * @param c
     * @return
     */
    public boolean hasGoodCredit(Customer c) {
        System.out.println("Check credit for " + c.getName());
        return true;
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.realworld;

/**
 * The Customer class.
 *
 * @author Chimomo
 */
class Customer {

    // The name.
    private String name;

    /**
     * Initializes a new instance of the "Customer" class.
     *
     * @param name
     */
    public Customer(String name) {
        this.name = name;
    }

    /**
     * Get name.
     */
    public String getName() {
        return this.name;
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.realworld;

/**
 * The 'Subsystem ClassC' class.
 *
 * @author Chimomo
 */
class Loan {

    /**
     * The has no bad loans.
     *
     * @param c
     * @return
     */
    public boolean hasNoBadLoans(Customer c) {
        System.out.println("Check loans for " + c.getName());
        return true;
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.realworld;

/**
 * The 'Facade' class.
 *
 * @author Chimomo
 */
class Mortgage {

    // The bank.
    private Bank bank = new Bank();

    // The credit.
    private Credit credit = new Credit();

    // The loan.
    private Loan loan = new Loan();

    /**
     * The is eligible.
     *
     * @param cust
     * @param amount
     * @return
     */
    public boolean isEligible(Customer cust, int amount) {
        System.out.println(String.format("%s applies for %d loan", cust.getName(), amount));
        boolean eligible = true;

        // Check credit worthiness of applicant.
        if (!this.bank.hasSufficientSavings(cust, amount)) {
            eligible = false;
        } else if (!this.loan.hasNoBadLoans(cust)) {
            eligible = false;
        } else if (!this.credit.hasGoodCredit(cust)) {
            eligible = false;
        }

        return eligible;
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.facade.realworld;

/**
 * Startup class for Real-World Facade Design Pattern.
 *
 * @author Chimomo
 */
class Program {

    /**
     * Entry point into console application.
     *
     * @param args
     */
    public static void main(String[] args) {

        // Facade.
        Mortgage mortgage = new Mortgage();

        // Evaluate mortgage eligibility for customer.
        Customer customer = new Customer("Ann McKinsey");
        boolean eligible = mortgage.isEligible(customer, 125000);
        System.out.println(customer.getName() + " has been " + (eligible ? "Approved" : "Rejected"));
    }

}

/*
Output:
Ann McKinsey applies for 125000 loan
Check bank for Ann McKinsey
Check loans for Ann McKinsey
Check credit for Ann McKinsey
Ann McKinsey has been Approved
 */

标签:Java,chimomo,Pattern,public,facade,Facade,class,Chimomo
来源: https://blog.csdn.net/chimomo/article/details/80974411

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有