zoukankan      html  css  js  c++  java
  • 设计模式工厂模式

    /**
     * @author Rollen-Holt 设计模式之 工厂模式
     */
    
    interface fruit{
    	public abstract void eat();
    }
    
    class Apple implements fruit{
    	public void eat(){
    		System.out.println("Apple");
    	}
    }
    
    class Orange implements fruit{
    	public void eat(){
    		System.out.println("Orange");
    	}
    }
    
    // 构造工厂类
    // 也就是说以后如果我们在添加其他的实例的时候只需要修改工厂类就行了
    class Factory{
    	public static fruit getInstance(String fruitName){
    		fruit f=null;
    		if("Apple".equals(fruitName)){
    			f=new Apple();
    		}
    		if("Orange".equals(fruitName)){
    			f=new Orange();
    		}
    		return f;
    	}
    }
    class hello{
    	public static void main(String[] a){
    		fruit f=Factory.getInstance("Orange");
    		f.eat();
    	}
    
    }
    

      

  • 相关阅读:
    [POJ] 食物链
    [POJ] Palindrome
    [POJ] The Triangle
    [Cpp primer] Library vector Type
    Shift Operations on C
    Masking operations
    [CSAPP] The Unicode Standard for text coding
    [Cpp primer] Library string Type
    [Cpp primer] range for (c++11)
    [Cpp primer] Namespace using Declarations
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2144851.html
Copyright © 2011-2022 走看看