Java设计模式之Facade模式

网上很少有Facade模式的例子,传的最多的就是那个jdbc的例子,不过这个例子我看着憋的慌,为什么?看不懂,我觉得文章不全,不知道怎么的就出来了prep 和 MySql两个类,从头到尾都没看到定义,但是很多网站都有这个例子,难道大家就没考虑一下这个例子对不对就转发吗?这不是太不负责任了吗?
     我还是自己写一个例子,因为网上有很多介绍facade模式的文章谈起医院里,挂号,看病,拿药,然后找一个Facade做完这些事情,那么我们就做一个类。首先是三个类,挂号:Registry 看病:Doctor 拿药:Pharmacy 然后定义一个Facade 完成这三个类里的方法。
package structure.facade;

public interface Patient {
  public String getName();
}
package structure.facade;
public interface Disposer {
  public void dispose(Patient patient);
}
package structure.facade;
public class Registry implements Disposer{
 public void dispose(Patient patient) {
  // TODO Auto-generated method stub
  System.out.println("I am registering...."+patient.getName());
 }
}
package structure.facade;
public class Doctor implements Disposer {
 public void dispose(Patient patient) {
  // TODO Auto-generated method stub
  System.out.println("I am diagnosing...."+patient.getName());
 }
 
}
package structure.facade;
public class Pharmacy implements Disposer {
 public void dispose(Patient patient) {
  // TODO Auto-generated method stub
  System.out.println("I am giving medicine..."+patient.getName());
 }
}
package structure.facade;
public class Facade {
 private Patient patient;
    public Facade(Patient patient){this.patient=patient;}
    public void dispose(){
     Registry registry=new Registry();
     Doctor doctor=new Doctor();
     Pharmacy ph=new Pharmacy();
     
     registry.dispose(patient);
     doctor.dispose(patient);
     ph.dispose(patient);
    }
}
package structure.facade;
public class ConcretePatient implements Patient {
 private String name;
 public ConcretePatient(String name){this.name=name;}
 public String getName() {
  // TODO Auto-generated method stub
  return name;
 }
}
package structure.facade;
public class Demo {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Patient patient =new ConcretePatient("Bush");
  Facade f=new Facade(patient);
  f.dispose();
 }
}

你可能感兴趣的:(java,设计模式,mysql,jdbc,F#)