工厂模式(范例)
package com.sun.java.design;
//定义一个接口
interface Computer {
public void type();
}
class Dell implements Computer {
@Override
public void type() {
System.out.println("戴尔n5110");
}
}
class Lenovo implements Computer {
@Override
public void type() {
System.out.println("彪悍的小y");
}
}
class Factory {
public Computer produceComputer(String name) {
if (name.equalsIgnoreCase("dell")) {
return new Dell();
} else if (name.equalsIgnoreCase("lenovo")) {
return new Lenovo();
} else {
return null;
}
}
}
public class FactoryPattern {
public static void main(String[] args) {
Factory f = new Factory();
Computer c1 = f.produceComputer("dell");
Computer c2 = f.produceComputer("lenovo");
c1.type();
c2.type();
}
}