外观模式
一、股民炒股代码
//股票1
class Stock1{
//卖股票
public void Sell() {
System.out.print("股票1卖出");
}
//买股票
public void Buy() {
System.out.print("股票1买入");
}
}
class Stock2{
//卖股票
public void Sell() {
System.out.print("股票2卖出");
}
//买股票
public void Buy() {
System.out.print("股票2买入");
}
}
class Stock3{
//卖股票
public void Sell() {
System.out.print("股票3卖出");
}
//买股票
public void Buy() {
System.out.print("股票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买入");
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Stock1 gu1=new Stock1();
Stock2 gu2=new Stock2();
NationalDebt1 nd1=new NationalDebt1();
Realty1 rt1=new Realty1();
gu1.Buy();
gu1.Sell();
gu2.Buy();
gu2.Sell();
nd1.Buy();
nd1.Sell();
rt1.Buy();
rt1.Sell();
}
}
客户需要了解股票、国债、房产等情况,需要参与这些项目的具体买和卖。耦合性很高。
二、投资基金代码
如果我们现在增加基金类
//股票1
class Stock1{
//卖股票
public void Sell() {
System.out.print("股票1卖出");
}
//买股票
public void Buy() {
System.out.print("股票1买入");
}
}
class Stock2{
//卖股票
public void Sell() {
System.out.print("股票2卖出");
}
//买股票
public void Buy() {
System.out.print("股票2买入");
}
}
class Stock3{
//卖股票
public void Sell() {
System.out.print("股票3卖出");
}
//买股票
public void Buy() {
System.out.print("股票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 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 Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fund jijin=new Fund();//基金购买
jijin.BuyFund();
//基金赎回
jijin.SellFund();
}
}
三、外观模式
外观模式,为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
//四个子系统的类
class SubSystemOne{
public void MethodOne() {
System.out.println("子系统方法一");
}
}
class SubSystemTwo{
public void MethodTwo() {
System.out.println("子系统方法二");
}
}
class SubSystemThree{
public void MethodThree() {
System.out.println("子系统方法三");
}
}
class SubSystemFour{
public void MethodFour() {
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("\n方法组A()----");
one.MethodOne();
two.MethodTwo();
four.MethodFour();
}
public void MethodB() {
System.out.println("\n方法组B()----");
two.MethodTwo();
three.MethodThree();
}
}
//由于Facade的作用,客户端可以根本不知三个子系统类的存在
public class Main{
public static void main(String[] args){
Facade facade=new Facade();
facade.MethodA();
facade.MethodB();
}
}
四、何时使用外观模式
首先,在设计初期阶段,应该要有意识的将不同的两个层分离,比如经其的三层架构,就需要考虑在数据访问层和业务逻辑层、业务逻辑层和表示层的层与层之间建立外观Facade,这样可以为复杂的子系统提供一个简单的接口,使得耦合度大大降低。
其次,在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,大多数的模式使用时也都会产生很多很小的类,这本是好事,但也给外部调用它们的用户程序带来了使用上的困难,增加外观Facade可以提供一个简单的接口,减少它们之间的依赖。
第三,在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,但因为它包含非常重要的功能,新的需求开发必须要依赖于它。此时用外观模式Facade也是非常合适的·。你可以为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰的简单接口,让新系统与Facade对象交互,Facade与遗留代码交互所有复杂的工作。