首页 > 代码库 > facade模式

facade模式

GuoZai.java

package edu.facade2;

public class GuoZai {
    public void mai() {
        System.out.println("买股票");
    }
}

GuPiao.java

package edu.facade2;

public class GuPiao {
    public void cao() {
        System.out.println("炒股票");
    }
}

QiHuo.java

package edu.facade2;

public class QiHuo {
    public void gou() {
        System.out.println("购期货");
    }
}

JinJi.java

package edu.facade2;

public class JiJin {
    private GuoZai guoZai;
    private GuPiao guPiao;
    private QiHuo qiHuo;

    public GuoZai getGuoZai() {
        return guoZai;
    }

    public void setGuoZai(GuoZai guoZai) {
        this.guoZai = guoZai;
    }

    public GuPiao getGuPiao() {
        return guPiao;
    }

    public void setGuPiao(GuPiao guPiao) {
        this.guPiao = guPiao;
    }

    public QiHuo getQiHuo() {
        return qiHuo;
    }

    public void setQiHuo(QiHuo qiHuo) {
        this.qiHuo = qiHuo;
    }

    public void Mai() {
        guoZai.mai();
        qiHuo.gou();
        guPiao.cao();
    }
}

关系图

applicationContext2.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    <bean id="guoZai" class="edu.facade2.GuoZai" />
    <bean id="guPiao" class="edu.facade2.GuPiao" />
    <bean id="qiHuo" class="edu.facade2.QiHuo" />
    <bean id="jiJin" class="edu.facade2.JiJin">
        <property name="guoZai" ref="guoZai"/>
        <property name="guPiao" ref="guPiao"/>
        <property name="qiHuo" ref="qiHuo"/>
    </bean>
</beans>
ClassTest2
package cn.itcast.dao.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.facade2.JiJin;

public class ClassTest2 {
    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext2.xml");
        JiJin jiJin = (JiJin) ctx.getBean("jiJin");
        jiJin.Mai();
    }
}