Factory Method Pattern (工厂方法模式)

package design_pattern;


/****
 * Factory Method Pattern (工厂方法模式)
 * 
 */
interface Mobile{
	public void call();
}
interface MobileFactory{
	public Mobile produceMobile();
}
class Motorla implements Mobile {
	public void call() {
		System.out.println("一部Motorla手机生成了!");
	}
}

class Nokia implements Mobile {
	public void call() {
		System.out.println("一部Nokia手机生成了!");
	}
}

class MotorlaFactory implements MobileFactory{
	public Mobile produceMobile() {
		// TODO Auto-generated method stub
		System.out.print("摩托罗拉工厂制造了");
		return new Motorla();
	}
}

class NokiaFactory implements MobileFactory{
	public Mobile produceMobile() {
		// TODO Auto-generated method stub
摩托罗拉工厂制造了一部Motorla手机生成了!
诺基亚工厂制造了一部Nokia手机生成了!
  System.out.print("诺基亚工厂制造了"); return new Nokia(); } } public class FactoryMethodPattern_02 { public static void main(String args[]){ MobileFactory mf; Mobile m ; mf = new MotorlaFactory(); m = mf.produceMobile(); m.call(); mf = new NokiaFactory(); m = mf.produceMobile(); m.call(); } }

 显示结果:

 

摩托罗拉工厂制造了一部Motorla手机生成了!
诺基亚工厂制造了一部Nokia手机生成了!

你可能感兴趣的:(制造,mobile,Nokia)