标签:out 就是 marked 测试结果 event https col override click
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
抽象工厂模式也就是不仅生产鼠标,同时生产键盘。
也就是 PC 厂商是个父类,有生产鼠标,生产键盘两个接口。
戴尔工厂,惠普工厂继承它,可以分别生产戴尔鼠标+戴尔键盘,和惠普鼠标+惠普键盘。
创建工厂时,由戴尔工厂创建。
后续工厂.生产鼠标()则生产戴尔鼠标,工厂.生产键盘()则生产戴尔键盘。
1 public interface Keybo { 2 void sayHi(); 3 }
1 public interface Mouse { 2 void sayHi(); 3 }
1 public class DellKeybo implements Keybo { 2 @Override 3 public void sayHi() { 4 System.out.println("Dellkeybo"); 5 } 6 }
1 public class HpKeybo implements Keybo { 2 @Override 3 public void sayHi() { 4 System.out.println("HpKeybo"); 5 } 6 }
1 public class DellMouse implements Mouse { 2 @Override 3 public void sayHi() { 4 System.out.println("DellMouse"); 5 } 6 }
1 public class HpMouse implements Mouse { 2 @Override 3 public void sayHi() { 4 System.out.println("HpMouse"); 5 } 6 }
1 public class KeyboFactory extends PcFactory { 2 @Override 3 public Keybo getKeybo(String keyboType) { 4 if(keyboType == null){ 5 return null; 6 } 7 if (keyboType.equals("DellKeybo")){ 8 return new DellKeybo(); 9 } 10 if (keyboType.equals("HpKeybo")){ 11 return new HpKeybo(); 12 } 13 return null; 14 } 15 16 @Override 17 public Mouse getMouse(String MouseType) { 18 return null; 19 } 20 }
1 public class MouseFactory extends PcFactory { 2 @Override 3 public Keybo getKeybo(String keyboType) { 4 return null; 5 } 6 7 @Override 8 public Mouse getMouse(String MouseType) { 9 if(MouseType == null){ 10 return null; 11 } 12 if(MouseType.equals("DellMouse")){ 13 return new DellMouse(); 14 } 15 if(MouseType.equals("HpMouse")){ 16 return new HpMouse(); 17 } 18 return null; 19 } 20 }
1 public abstract class PcFactory { 2 public abstract Keybo getKeybo(String keyboType); 3 4 public abstract Mouse getMouse(String MouseType); 5 }
1 public class FactoryProducer { 2 public static PcFactory getFactory(String choice){ 3 if(choice.equals("keyboFactory")){ 4 return new KeyboFactory(); 5 } 6 if(choice.equals("mouseFactory")){ 7 return new MouseFactory(); 8 } 9 return null; 10 } 11 }
1 public class PcFactoryPatternDemo { 2 public static void main(String[] args) { 3 PcFactory keybo = FactoryProducer.getFactory("keyboFactory"); 4 Keybo dellKeybo = keybo.getKeybo("DellKeybo"); 5 dellKeybo.sayHi(); 6 Keybo hpKeybo = keybo.getKeybo("HpKeybo"); 7 hpKeybo.sayHi(); 8 PcFactory mouse = FactoryProducer.getFactory("mouseFactory"); 9 Mouse dellMouse = mouse.getMouse("DellMouse"); 10 dellMouse.sayHi(); 11 Mouse hpMouse = mouse.getMouse("HpMouse"); 12 hpMouse.sayHi(); 13 } 14 }
Dellkeybo
HpKeybo
DellMouse
HpMouse
参考:https://www.runoob.com/design-pattern/abstract-factory-pattern.html
标签:out 就是 marked 测试结果 event https col override click
原文地址:https://www.cnblogs.com/hoje/p/11926479.html