(1) 适配器
- interface Window{
- public void open() ; // 打开窗口
- public void close() ; // 关闭窗口
- public void icon() ; // 最小化
- public void unicon() ; // 最大化
- }
- abstract class WindowAdapter implements Window{
- public void open(){}
- public void close(){}
- public void icon(){}
- public void unicon(){}
- };
- class MyWindow extends WindowAdapter{
- public void open(){
- System.out.println("打开窗口!") ;
- }
- };
- public class AdpaterDemo{
- public static void main(String args[]){
- Window win = new MyWindow() ;
- win.open() ;
- }
- }
(2) 工厂设计模式
- interface Fruit{
- public void eat() ;
- }
- class Apple implements Fruit{
- public void eat(){
- System.out.println("吃苹果。。。") ;
- }
- };
- class Orange implements Fruit{
- public void eat(){
- System.out.println("吃橘子。。。") ;
- }
- };
- class Factory{ // 工厂类
- public static Fruit getFruit(String className){
- Fruit f = null ;
- if("apple".equals(className)){
- f = new Apple() ;
- }
- if("orange".equals(className)){
- f = new Orange() ;
- }
- return f ;
- }
- };
- public class InterDemo{
- public static void main(String args[]){
- Fruit f = Factory.getFruit(args[0]) ;
- if(f!=null){
- f.eat() ;
- }
- }
- }
(3) 代理设计模式
- interface Give{
- public void giveMoney() ;
- }
- class RealGive implements Give{
- public void giveMoney(){
- System.out.println("把钱还给我。。。。。") ;
- }
- };
- class ProxyGive implements Give{ // 代理公司
- private Give give = null ;
- public ProxyGive(Give give){
- this.give = give ;
- }
- public void before(){
- System.out.println("准备:小刀、绳索、钢筋、钢据、手枪、毒品") ;
- }
- public void giveMoney(){
- this.before() ;
- this.give.giveMoney() ; // 代表真正的讨债者完成讨债的操作
- this.after() ;
- }
- public void after(){
- System.out.println("销毁所有罪证") ;
- }
- };
- public class ProxyDemo{
- public static void main(String args[]){
- Give give = new ProxyGive(new RealGive()) ;
- give.giveMoney() ;
- }
- };