基金代码
//股票、国债、房地产代码
class Stock1
{
//卖股票
public void Sell()
{
System.out.println("股票1卖出");
}
//买股票
public void Buy()
{
System.out.println("股票1买入");
}
}
class Stock2//股票2
{//代码类似Stock1}
class Stock3//股票3
{//代码类似Stock1}
class NationalDebt1//国债1
{//代码类似Stock1}
class Realty1//房地产1
{//代码类似Stock1}
class Fund//基金类
{
Stock1 gu1;
Stock2 gu2;
Stock3 gu3;
NationalDebt1 nd1;
Realty1 rt1;
public Fund()
{
gu1 = new Stock1();
gu2 = new Stock2();
gu3 = new Stock3();
nd1 = new NationalDebt1();
rt1 = new Realty1();
}
public void BuyFund()
{
gu1.Buy();
gu2.Buy();
gu3.Buy();
nd1.Buy();
rt1.Buy();
}
public void SellFund()
{
gu1.Sell();
gu2.Sell();
gu3.Sell();
nd1.Sell();
rt1.Sell();
}
}
public class Program //客户端代码
{
public static void main(String[] args)
{
Fund jijin = new Fund();
jijin.BuyFund();
jijin.SellFund();
}
}
外观模式的结构:
基本代码:
四个子系统类:
public class SubSystemOne
{
public void MethodOne()
{
System.out.println("子系统方法一");
}
}
public class SubSystemTwo
{
public void MethodTwo()
{
System.out.println("子系统方法二");
}
}
public class SubSystemThree
{
public void MethodThree()
{
System.out.println("子系统方法三");
}
}
public class SubSystemFour
{
public void MethodFour()
{
System.out.println("子系统方法四");
}
}
外观类:
public class Facade
{
SubSystemOne one;
SubSystemTwo two;
SubSystemThree three;
SubSystemFour four;
public Facade()
{
one = new SubSystemOne();
two = new SubSystemTwo();
three = new SubSystemThree();
four = new SubSystemFour();
}
public void MethodA()
{
System.out.println("\n方法组A()----");
one.MethodOne();
two.MethodTwo();
four.MethodFour();
}
public void methodB()
{
System.out.println("\n方法组B()----");
two.MwthodTwo();
three.MethodThree();
}
}
客户端调用(由于Facade的作用,客户端可以根本不知道四个子系统类的存在):
public class Program
{
public static void main(String[] args)
{
Facade facade = new Facade();
facade.MethodA();
facade.MethodB();
}
}
08-22
08-22
08-22
08-22