标签:factory 技术 ide ati ima 一个 print 图片 rri
简单工厂的弊端可以用工厂方法一定程度的避免;
public interface Screw {
public void useScrew();
}
public class EgihtScrew implements Screw{
@Override
public void useScrew() {
System.out.println("使用8号螺丝。");
}
}
public class SixScrew implements Screw{
@Override
public void useScrew() {
System.out.println("使用6号螺丝。");;
}
}
public class SevenScrew implements Screw {
@Override
public void useScrew() {
System.out.println("使用7号螺丝。");
}
}
public interface ScrewFactory {
public Screw produceScrew();
}
public class EgihtScrewFactory implements ScrewFactory{
@Override
public Screw produceScrew() {
return new EgihtScrew();
}
}
public class SixScrewFactory implements ScrewFactory{
@Override
public Screw produceScrew() {
return new SixScrew();
}
}
public class SevenScrewFactory implements ScrewFactory{
@Override
public Screw produceScrew() {
return new SevenScrew();
}
}
public class Store {
public static void main(String[] args) {
ScrewFactory factory = new SevenScrewFactory();
Screw screw = factory.produceScrew();
screw.useScrew();
}
}
优点:
标签:factory 技术 ide ati ima 一个 print 图片 rri
原文地址:https://www.cnblogs.com/kaka-qiqi/p/14844087.html