我認為wiki寫的蠻清楚的,這邊直接貼上說明
A facade is an object that provides a simplified interface to a larger body of code, such as a
class library. A facade can:
- make a software library easier to use, understand and test, since the facade has convenient methods for common tasks;
- make the library more readable, for the same reason;
- reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
- wrap a poorly designed collection of APIs with a single well-designed API (as per task needs).
The Facade design pattern is often used when a system is very complex or difficult to understand because the system has a large number of interdependent classes or its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client. It typically involves a single wrapper class which contains a set of members required by client. These members access the system on behalf of the facade client and hide the implementation details.
Animal.java
package com.test.facadepattern;
public interface Animal {
void printMsg();
void getFeet();
void life();
}
Bird.java
package com.test.facadepattern;
public class Bird implements Animal{
@Override
public void printMsg() {
// TODO Auto-generated method stub
System.out.println("bird class");
}
@Override
public void getFeet() {
// TODO Auto-generated method stub
}
@Override
public void life() {
// TODO Auto-generated method stub
}
}
Cat.java
package com.test.facadepattern;
public class Cat implements Animal {
@Override
public void printMsg() {
// TODO Auto-generated method stub
System.out.println("cat class");
}
@Override
public void getFeet() {
// TODO Auto-generated method stub
}
@Override
public void life() {
// TODO Auto-generated method stub
}
}
Dog.java
package com.test.facadepattern;
public class Dog implements Animal{
@Override
public void printMsg() {
// TODO Auto-generated method stub
System.out.println("dog class");
}
@Override
public void getFeet() {
// TODO Auto-generated method stub
}
@Override
public void life() {
// TODO Auto-generated method stub
}
}
Zoo.java
package com.test.facadepattern;
public class Zoo {
private Animal bird ;
private Animal cat ;
private Animal dog ;
public Zoo(){
bird = new Bird();
dog = new Dog();
cat = new Cat();
}
public void imDog(){
dog.printMsg();
}
public void imBird(){
bird.printMsg();
}
public void imCat(){
cat.printMsg();
}
}
testFacadePattern .java
package com.test.facadepattern;
public class testFacadePattern {
public static void main(String[] args) {
Zoo zoo = new Zoo();
zoo.imBird();
zoo.imCat();
zoo.imDog();
}
}
console
bird class
cat class
dog class