反射可以动态加载一个类 生成该类的实例 new 只能静态加载一个类 生成一个类的实例 package day0815.factory; /** * 简单工厂模式 * @author Cool * */ public class TestFactory1 { public static void main(String[] args) { // Fruit apple = new Apple(); // apple.eat(); // Fruit orange = new Orange(); // orange.eat(); Fruit f = Factory.getFruit("orange"); if(f!=null){ f.eat(); } }
} 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 fruitName){ Fruit f = null; if("apple".equals(fruitName)){ f = new Apple(); } if("orange".equals(fruitName)){ f= new Orange(); } return f; } }
/** * 代理模式 * @author Cool * */ public class TestProxy { public static void main(String[] args) { //Give give = new ProxyGive(new RealGive()); Give give = new RealGive(); give.giveMoney(); } } //讨债 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 beforeGive(){ System.out.println("准备:各种讨债工具"); } public void giveMoney() {// 讨债方法 this.beforeGive(); this.give.giveMoney();//讨债 this.afterGive(); } public void afterGive(){ System.out.println("销毁所有的罪证"); } }
new一个对象和反射的区别就相当于宰一只鸡和桌子的区别 你说宰一只鸡和桌子的区别是什么
new 只能静态加载一个类 生成一个类的实例 package day0815.factory;
/**
* 简单工厂模式
* @author Cool
*
*/
public class TestFactory1 {
public static void main(String[] args) {
// Fruit apple = new Apple();
// apple.eat();
// Fruit orange = new Orange();
// orange.eat();
Fruit f = Factory.getFruit("orange");
if(f!=null){
f.eat();
}
}
}
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 fruitName){
Fruit f = null;
if("apple".equals(fruitName)){
f = new Apple();
}
if("orange".equals(fruitName)){
f= new Orange();
}
return f;
}
}
* 代理模式
* @author Cool
*
*/
public class TestProxy {
public static void main(String[] args) {
//Give give = new ProxyGive(new RealGive());
Give give = new RealGive();
give.giveMoney();
}
}
//讨债
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 beforeGive(){
System.out.println("准备:各种讨债工具");
}
public void giveMoney() {// 讨债方法
this.beforeGive();
this.give.giveMoney();//讨债
this.afterGive();
}
public void afterGive(){
System.out.println("销毁所有的罪证");
}
}
2.不怎么会用设计模式。代理算吗?
class Carrier {
private Fighter J10;
private J10Count = 10; Carrier() {
for(int i=0; i<J10Count; i++)
J10Factory.createFighter();
} public void attack() {
J10.prepare();
J10.fly();
J10.searchEnemy();
if(J10.findTarget()) {
J10.suicideBombing();
J10Count--;
}
}
}
2.单例模式常用
至于各种模式也只是听说的比较多!嘿嘿嘿嘿..
单态(曾经用过,后来看了一些文章,把单态换成静态类了,由tomcat启动时自动做初始化)
工厂
代理
反射
观察者
恩,就这么多了,项目还没开发完,有待进一步完善,另外jive很值得看看,学模式很好,我项目中的缓存就是用的它的缓存类,呵呵