• Java的设计模式 之 简单的工厂模式(二)


    下面的代码是稍微改进后的工厂模式

        abstract class MoveAble{
    	public abstract void run();
    }
    abstract class Woopen{
    	public abstract void attack();
    }
    
    abstract class Food{
    	public abstract void eat();
    }
    
    interface AllFactory{
    	public MoveAble createM();
    	
    	public Woopen createW();
    	
    	public Food createF();
    }
    
    class Car extends MoveAble{
    	public void run(){
    		System.out.println("Car is runing");
    	}
    }
    
    class AK47 extends Woopen{
    	public void attack(){
    		System.out.println("da da da");
    	}
    }
    
    class Apple extends Food{
    	public void eat(){
    		System.out.println("Name");
    	}
    }
    
    class Broom extends MoveAble{
    	public void run(){
    		System.out.println("flaying ....");
    	}
    }
    
    class MagicStick extends Woopen{
    	public void attack(){
    		System.out.println("Bang ....");
    	}
    }
    
    class MashRoom extends Food{
    	public void eat(){
    		System.out.println("Ya you du !!!");
    	}
    }
    
    class DefaultFactory implements AllFactory{
    	public MoveAble createM(){
    		return new Car();
    	}
    	
    	public Woopen createW(){
    		return new AK47();
    	}
    	
    	public Apple createF(){
    		return new Apple();
    	}
    }
    
    class MagicFactory implements AllFactory{
    	public MoveAble createM(){
    		return new Broom();
    	}
    	
    	public Woopen createW(){
    		return new MagicStick();
    	}
    	
    	public Food createF(){
    		return new MashRoom();
    	}
    }
    
    
    
  • 相关阅读:
    Django学习笔记之Cookie、Session和自定义分页
    sass表达式前后出现空格
    render总结
    vue双向绑定补充说明方法
    mutation与action
    keep-alive使用笔记
    this指向 一般函数与箭头函数
    vue-router原理分析
    history新增方法
    常用阻止ajax缓存方法集锦
  • 原文地址:https://www.cnblogs.com/gxcstyle/p/8672510.html
Copyright © 2020-2023  润新知