设计模式

来源:互联网 发布:网络特产商城行业分析 编辑:程序博客网 时间:2024/05/18 19:40
单利模式
  ###懒汉式###  public class News{        public static News n=null;        public News getNews(){                if(n==null){                n=new News();                }                return n;            }} }  
###饿汉式###    public class News{        public static News n=new News();        public News getNews(){                return n;            }} }  
观察者模式
     btn.setOnClickListenter(new OnClickListenter(){        public void onClick(View v){        //给一个按钮添加监听事件,一旦按钮被点击,就执行相应的处理内容        }});
适配模式

比如android中的BaseAdapter

工厂模式
   //动物工厂   public interface AnimalsFactory{      Animals getAnimals();    }    public class TigerFactory implements AnimalsFactory{     public Animals getAnimals(){       return new Tiger();      }     }     public class SheepFactory implements AnimalsFactory{      public Animals getAnimals(){       return new Sheep();       }      }//具体创建的动物对象   public interface Animals{      void EatAction();   }  public class Tiger implements Animals{     public void EatAction(){      System.out.pirntln("老虎吃肉");      }   }   public class Sheep implements Animals{     public void EatAction(){      System.out.println("羊吃草");      }    }//测试类  public class Test{       public static void main(String args[]){        AnimalsFactory tigerFactory=new TigerFactory();        Tiger tiger=tigerFactory.getAnimals();        tiger.EatAction();        AnimalsFactory sheepFactory=new SheepFactory();        Sheep sheep=sheepFactory.getAniamls();        sheep.EatAction();        }  }

结果:

老虎吃肉
羊吃草
0 0
原创粉丝点击