标签:case nts 静态 log 技术 turn ase logs creat
简单工厂模式,又称为静态方法工厂模式。结构图如下:
实现:
水果接口:
package com.design.simplefactory; public interface Fruit { String fruitName(); }
实现类:
package com.design.simplefactory; public class Apple implements Fruit { @Override public String fruitName() { return "Big Apple"; } }
package com.design.simplefactory; public class Banana implements Fruit { @Override public String fruitName() { return "Big Banana"; } }
工厂:
package com.design.simplefactory; public class FruitFactory { public static Fruit createFriut(String name){ switch (name){ case "apple": return new Apple(); case "banana": return new Banana(); default: return null; } } public static void main(String[] args){ Fruit apple = FruitFactory.createFriut("apple"); Fruit banana = FruitFactory.createFriut("banana"); Fruit other = FruitFactory.createFriut("other"); System.out.println(apple == null?"null":apple.fruitName()); System.out.println(banana == null?"null":banana.fruitName()); System.out.println(other == null?"null":other.fruitName()); } }
输出:
标签:case nts 静态 log 技术 turn ase logs creat
原文地址:http://www.cnblogs.com/LUA123/p/7794685.html