设计模式
来源:互联网 发布:php获取表单数据并输出 编辑:程序博客网 时间:2024/05/10 19:01
单利模式
###懒汉式### public class Human{ public staic Human human=null; public Human getHuman(){ if(human==null){ human=new Human(); } return human; } } ###饿汉式### public class Human{ public staic Human human=new Human(); public Human getHuman(){ return human; } }
工厂模式
//创建实例的工厂 public interface FruitsFactory{ Fruit getFruit(); } public class AppleFactory implements FruitsFactory{ public Fruit getFruit(){ return new Apple(); } } public class BananaFactory implements FruitsFactory{ public Fruit getFruit(){ return new Banana(); } } //具体创建的实例对象 public interface Fruit{ void Color(); } public class Apple implements Fruit{ public void getColor(){ System.out.println("The apple's color is red!"); } } public class Banana implements Fruit{ public void getColor(){ System.out.println("The banana's color is yellow!"); } } //测试类 public class Test{ public static void main(String args[]){ FruitsFactory fruitsFactory=new AppleFactory(); Apple apple=fruitsFactory.getFuit(); apple.getColor(); FruitsFactory fruitsFactory=new BananaFactory(); Banana banana=fruitsFactory.getFuit(); banana.getColor(); } }
结果:
- The apple’s color is red!
- The banana’s color is yellow!
0 0
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式~~~~~~~~~~
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 涨知识:数学家又发现了平分披萨的新方法
- MD-code-test aa
- JavaScript 三种创建对象的方法
- JavaScript 滚动条示例,控制居中document.documentElement.scrollTop,document.body.scrollTop,oDiv.offsetHeight
- PPT幻灯片手动翻页设置技巧
- 设计模式
- iText系列(一) Itext简单介绍
- Android事件传递
- document.body.scrollTop与document.documentElement.scrollTop兼容
- [转]大型网站架构系列:负载均衡详解(1)
- 准备学习微信蓝牙
- popupwindow中有editext,点击出来软键盘后,popupwindow没法响应键盘回退事件
- Spring配置项<context:annotation-config/>解释说明
- Mycat数据排序和数据汇聚的实现