码迷,mamicode.com
首页 > 编程语言 > 详细

java工厂类与反射机制

时间:2014-06-09 18:05:58      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

java 简单工厂类  

2012-04-22 15:44:07|  分类: java |  标签:java工厂类  简单工厂类  |举报|字号 订阅

 
 

简单工厂模式需要由以下角色组成:
      接口
                        接口的实现类(简单工厂模式里面的具体产品角色)
                        工厂

注意对比以下三个实例的不同
实例1:

bubuko.com,布布扣package org.jzkangta.factorydemo01;
bubuko.com,布布扣
//定义接口
bubuko.com,布布扣
interface Car{
bubuko.com,布布扣    
public void run();
bubuko.com,布布扣    
public void stop();
bubuko.com,布布扣}

bubuko.com,布布扣
//具体实现类
bubuko.com,布布扣
class Benz implements Car{
bubuko.com,布布扣    
public void run(){
bubuko.com,布布扣        System.out.println(
"Benz开始启动了。。。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣    
public void stop(){
bubuko.com,布布扣        System.out.println(
"Benz停车了。。。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
//具体实现类
bubuko.com,布布扣
class Ford implements Car{
bubuko.com,布布扣    
public void run(){
bubuko.com,布布扣        System.out.println(
"Ford开始启动了。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣    
public void stop(){
bubuko.com,布布扣        System.out.println(
"Ford停车了。。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
//工厂
bubuko.com,布布扣
class Factory{
bubuko.com,布布扣    
public static Car getCarInstance(){
bubuko.com,布布扣        
return new Ford();
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
public class FactoryDemo01 {
bubuko.com,布布扣
bubuko.com,布布扣    
public static void main(String[] args) {
bubuko.com,布布扣        Car c
=Factory.getCarInstance();
bubuko.com,布布扣        c.run();
bubuko.com,布布扣        c.stop();
bubuko.com,布布扣
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣}

bubuko.com,布布扣


实例二:

bubuko.com,布布扣package fac;
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
//定义接口
bubuko.com,布布扣
interface Car{
bubuko.com,布布扣    
public void run();
bubuko.com,布布扣    
public void stop();
bubuko.com,布布扣}

bubuko.com,布布扣
//具体实现类
bubuko.com,布布扣
class Benz implements Car{
bubuko.com,布布扣    
public void run(){
bubuko.com,布布扣        System.out.println(
"Benz开始启动了。。。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣    
public void stop(){
bubuko.com,布布扣        System.out.println(
"Benz停车了。。。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
bubuko.com,布布扣
class Ford implements Car{
bubuko.com,布布扣    
public void run(){
bubuko.com,布布扣        System.out.println(
"Ford开始启动了。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣    
public void stop(){
bubuko.com,布布扣        System.out.println(
"Ford停车了。。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
//工厂
bubuko.com,布布扣
class Factory{
bubuko.com,布布扣    
public static Car getCarInstance(String type){
bubuko.com,布布扣        Car c
=null;
bubuko.com,布布扣        
if("Benz".equals(type)){
bubuko.com,布布扣            c
=new Benz();
bubuko.com,布布扣        }

bubuko.com,布布扣        
if("Ford".equals(type)){
bubuko.com,布布扣            c
=new Ford();
bubuko.com,布布扣        }

bubuko.com,布布扣        
return c;
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
public class FactoryDemo02 {
bubuko.com,布布扣
bubuko.com,布布扣    
public static void main(String[] args) {
bubuko.com,布布扣        Car c
=Factory.getCarInstance("Benz");
bubuko.com,布布扣        
if(c!=null){
bubuko.com,布布扣            c.run();
bubuko.com,布布扣            c.stop();
bubuko.com,布布扣        }
else{
bubuko.com,布布扣            System.out.println(
"造不了这种汽车。。。");
bubuko.com,布布扣        }

bubuko.com,布布扣        
bubuko.com,布布扣
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣}

bubuko.com,布布扣
bubuko.com,布布扣


实例三:

bubuko.com,布布扣interface Car{
bubuko.com,布布扣    
public void run();
bubuko.com,布布扣    
public void stop();
bubuko.com,布布扣}

bubuko.com,布布扣
bubuko.com,布布扣
class Benz implements Car{
bubuko.com,布布扣    
public void run(){
bubuko.com,布布扣        System.out.println(
"Benz开始启动了。。。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣    
public void stop(){
bubuko.com,布布扣        System.out.println(
"Benz停车了。。。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
bubuko.com,布布扣
class Ford implements Car{
bubuko.com,布布扣    
public void run(){
bubuko.com,布布扣        System.out.println(
"Ford开始启动了。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣    
public void stop(){
bubuko.com,布布扣        System.out.println(
"Ford停车了。。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
bubuko.com,布布扣
class Toyota implements Car{
bubuko.com,布布扣    
public void run(){
bubuko.com,布布扣        System.out.println(
"Toyota开始启动了。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣    
public void stop(){
bubuko.com,布布扣        System.out.println(
"Toyota停车了。。。。");
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
bubuko.com,布布扣
class Factory{
bubuko.com,布布扣    
public static Car getCarInstance(String type){
bubuko.com,布布扣        Car c
=null;
bubuko.com,布布扣        
try {
bubuko.com,布布扣            c
=(Car)Class.forName("org.jzkangta.factorydemo03."+type).newInstance();//利用反射得到汽车类型 
bubuko.com,布布扣        }
 catch (InstantiationException e) {
bubuko.com,布布扣            
// TODO Auto-generated catch block
bubuko.com,布布扣
            e.printStackTrace();
bubuko.com,布布扣        }
 catch (IllegalAccessException e) {
bubuko.com,布布扣            
// TODO Auto-generated catch block
bubuko.com,布布扣
            e.printStackTrace();
bubuko.com,布布扣        }
 catch (ClassNotFoundException e) {
bubuko.com,布布扣            
// TODO Auto-generated catch block
bubuko.com,布布扣
            e.printStackTrace();
bubuko.com,布布扣        }

bubuko.com,布布扣    
bubuko.com,布布扣        
return c;
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
public class FactoryDemo03 {
bubuko.com,布布扣
bubuko.com,布布扣    
public static void main(String[] args) {
bubuko.com,布布扣        Car c
=Factory.getCarInstance("Toyota");
bubuko.com,布布扣        
if(c!=null){
bubuko.com,布布扣            c.run();
bubuko.com,布布扣            c.stop();
bubuko.com,布布扣        }
else{
bubuko.com,布布扣            System.out.println(
"造不了这种汽车。。。");
bubuko.com,布布扣        }

bubuko.com,布布扣        
bubuko.com,布布扣
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣}

bubuko.com,布布扣
bubuko.com,布布扣



对比三个实例:
实例一,虽然实现了简单工厂,但每次只能得到一种汽车,如果我们想换一种,就得修改工厂,太不方便,而实例二则改变了这种情况,便得我们可以按照我们的需要更换汽车,但我们所更换的汽车必须是实现类中有的,如果我们想要增加一种汽车的时候,我们还是得更改工厂,通过改进,实例三利用反射机制,得到汽车类型,这样当我们需要增加一种新的汽车时,就无需要再修改工厂,而只需要增加要实现的类即可。也就是说要增加什么样的汽车直接增加这个汽车的类即可,而无需改变工厂。从而达到了工厂分离的效果。

 

java工厂类与反射机制,布布扣,bubuko.com

java工厂类与反射机制

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/cdutlihao/p/3777623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!