外观模式:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,
这个接口使得这一子系统更加容易使用。
股民炒股
package 设计模式;
import java.util.HashMap;
public class Text {
public static void main(String[] args) {
Stock1 g1=new Stock1();
Stock2 g2=new Stock2();
Stock3 g3=new Stock3();
NationalDebt1 nd1=new NationalDebt1();
Realty1 rt1=new Realty1();
g1.Buy(); g2.Buy(); g3.Buy();
nd1.Buy(); rt1.Buy();
g1.Sell();g2.Sell();g3.Sell();
nd1.Sell();rt1.Sell();
}
}
class Stock1
{
public void Sell()
{
System.out.println("股票1卖出");
}
public void Buy()
{
System.out.println("股票1买入");
}
}
class Stock2
{
public void Sell()
{
System.out.println("股票2卖出");
}
public void Buy()
{
System.out.println("股票2买入");
}
}class Stock3
{
public void Sell()
{
System.out.println("股票3卖出");
}
public void Buy()
{
System.out.println("股票3买入");
}
}
class NationalDebt1
{
public void Sell()
{
System.out.println("国债1卖出");
}
public void Buy()
{
System.out.println("国债1买入");
}
}
class Realty1
{
public void Sell()
{
System.out.println("房地产1卖出");
}
public void Buy()
{
System.out.println("房地产1买入");
}
}
投资基金
package 设计模式;
import java.util.HashMap;
public class Text {
public static void main(String[] args) {
Fund jj=new Fund();
jj.BuyFund();
jj.SellFund();
}
}
class Stock1
{
public void Sell()
{
System.out.println("股票1卖出");
}
public void Buy()
{
System.out.println("股票1买入");
}
}
class Stock2
{
public void Sell()
{
System.out.println("股票2卖出");
}
public void Buy()
{
System.out.println("股票2买入");
}
}class Stock3
{
public void Sell()
{
System.out.println("股票3卖出");
}
public void Buy()
{
System.out.println("股票3买入");
}
}
class NationalDebt1
{
public void Sell()
{
System.out.println("国债1卖出");
}
public void Buy()
{
System.out.println("国债1买入");
}
}
class Realty1
{
public void Sell()
{
System.out.println("房地产1卖出");
}
public void Buy()
{
System.out.println("房地产1买入");
}
}
class Fund
{
Stock1 g1;
Stock2 g2;
Stock3 g3;
NationalDebt1 nd1;
Realty1 rt1;
public Fund()
{
g1=new Stock1();
g2=new Stock2();
g3=new Stock3();
nd1=new NationalDebt1();
rt1=new Realty1();
}
public void BuyFund()
{
g1.Buy();
g2.Buy();
g3.Buy();
nd1.Buy();
rt1.Buy();
}
public void SellFund()
{
g1.Sell();
g2.Sell();
g3.Sell();
nd1.Sell();
rt1.Sell();
}
}
用户不需要了解股票,甚至可以对股票一无所知,买了基金过了一段时间后在数回来就可以数钱了。
参与股票的具体买卖都有基金公司完成。客户端代码也非常简洁明了。
外观模式
package 设计模式;
import java.util.HashMap;
public class Text {
public static void main(String[] args) {
Facade f=new Facade();
f.MethodA();
f.MethodB();
}
}
class SubSystemOne
{
public void MethodeOne()
{
System.out.println("子系统方法一");
}
}
class SubSystemTwo
{
public void MethodeTwo()
{
System.out.println("子系统方法二");
}
}
class SubSystemThree
{
public void MethodeThree()
{
System.out.println("子系统方法三");
}
}
class SubSystemFour
{
public void MethodeFour()
{
System.out.println("子系统方法四");
}
}
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("方法组A()");
one.MethodeOne();
two.MethodeTwo();
four.MethodeFour();
}
public void MethodB()
{
System.out.println("方法组B()");
two.MethodeTwo();
three.MethodeThree();
}
}
外观模式优点
1、屏蔽了外部客户端和系统内部模块的交互
2、Facade的功能可以被多个客户端调用,可以实现复用(功能的共享)
3、对使用Facade的人员来说,Facade大大节省了学习成本