vjame

优化代码是无止境的
随笔 - 65, 文章 - 9, 评论 - 26, 引用 - 0
数据加载中……

抽象工厂


女娲造物,她可以造人和动物,用阴绳造女人和雌性动物,用阳绳造男人和雄性动物,人要吃东西,会说话。动物也要吃东西,还会睡觉.
package com.strongit.factory;

interface Person{
    
public void eat();
    
public void talk();
}

class Man implements Person{

    
public void eat() {
        
// TODO Auto-generated method stub
        System.out.println("男人在吃东西。。。。");
    }

    
public void talk() {
        
// TODO Auto-generated method stub
        System.out.println("男人在说话。。。。");
    }
    
}

interface Animal{
    
public void eat();
    
public void sleep();
}

class Bull implements Animal{

    
public void eat() {
        
// TODO Auto-generated method stub
        System.out.println("公牛在吃东西。。。。");
    }

    
public void sleep() {
        
// TODO Auto-generated method stub
        System.out.println("公牛在睡觉。。。。");
    }
    
}
class Cow implements Animal{

    
public void eat() {
        
// TODO Auto-generated method stub
        System.out.println("母牛在吃东西。。。。");
    }

    
public void sleep() {
        
// TODO Auto-generated method stub
        System.out.println("母牛在睡觉。。。。");
    }
    
}

class Woman implements Person{

    
public void eat() {
        
// TODO Auto-generated method stub
        System.out.println("女人在吃东西。。。。");
    }

    
public void talk() {
        
// TODO Auto-generated method stub
        System.out.println("女人在说话。。。。");
    }
    
}

//表示女娲
interface NWFactory{
    
public Person createPerson(String type);
    
public Animal createAnimal(String type);
}

//阳绳-->用来造男人和雄性动物(Bull)
class YangSheng implements NWFactory{

    Man man 
= null;
    Bull bull 
= null;
    
public Bull createAnimal(String type) {
        
try {
            bull 
= (Bull)Class.forName("com.strongit.factory."+type).newInstance();
        } 
catch (InstantiationException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (IllegalAccessException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (ClassNotFoundException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        
return bull;
    }

    
public Man createPerson(String type) {
        
try {
            man 
= (Man)Class.forName("com.strongit.factory."+type).newInstance();
        } 
catch (InstantiationException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (IllegalAccessException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (ClassNotFoundException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        
return man;
    }
}

//阴绳-->用来造女人和雌性动物(Cow)
class YinSheng implements NWFactory{

    Woman woman 
= null;
    Cow cow 
= null;
    
public Cow createAnimal(String type) {
        
try {
            cow 
= (Cow)Class.forName("com.strongit.factory."+type).newInstance();
        } 
catch (InstantiationException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (IllegalAccessException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (ClassNotFoundException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        
return cow;
    }

    
public Woman createPerson(String type) {
        
try {
            woman 
= (Woman)Class.forName("com.strongit.factory."+type).newInstance();
        } 
catch (InstantiationException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (IllegalAccessException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (ClassNotFoundException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        
return woman;
    }
    
}


public class FactoryDemo1 {

    
/**
     * 
@param args
     
*/
    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        
//实例化一个阳绳,ys
        YangSheng ys=new YangSheng();
        
//实例化一个阴绳,ys1
        YinSheng ys1=new YinSheng();
        
//造男人和女人,p1是男人,p2是女人
        Person p1=ys.createPerson("Man");
        Person p2
=ys1.createPerson("Woman");
        
//造动物,a1是公牛(Bull),a2是母牛(Cow)
        Animal a1=ys.createAnimal("Bull");
        Animal a2
=ys1.createAnimal("Cow");
        a1.eat();
        a1.sleep();
        a2.eat();
        a2.sleep();
        p1.eat();
        p1.talk();
        p2.eat();
        p2.talk();

    }

}

源代码下载: http://www.blogjava.net/Files/vjame/FactoryDemo03.rar

posted on 2008-11-26 15:59 lanjh 阅读(220) 评论(0)  编辑  收藏 所属分类: 设计模式


只有注册用户登录后才能发表评论。


网站导航: