设计模式
来源:互联网 发布:网络特产商城行业分析 编辑:程序博客网 时间: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
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式~~~~~~~~~~
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- POJ 3625 Building Roads【MST】
- leetcode 160. Intersection of Two Linked Lists
- DbUtils component学习使用(jdbc封装工具)
- js中window.parent和window.opener,window.self、window.top用法
- hive筛选tomcat的日志文件到数据库中
- 设计模式
- 在Qt调用OpenCV库编写GUI程序
- android中改变窗体透明度
- java反射の动态代理与静态代理
- Android使用自定义字体
- android 客户端生成随机验证码的实现
- cocoPods安装及使用
- UIGestureRecognizer学习之——UIPinchGestureRecognizer
- iOS Crash文件的解析(一)