Concepts
- Factory of Factories
- Factory of related objects
- Commons Interface
- Defer to Subclasses
- Examples: DocumentBuilder, Frameworks
---------------------------------------------------------------
Design
- Groups Factories together
- Factory is responsible for lifecycle
- Common Interface
- Concrete Classes
- Parameterized created method
- Composition
----------------------------------------------------------------------------
Everyday Example - DocumentBuilderFactory
package com.pattern.abstractfactory;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class AbstractFactoryEverydayDemo {
public static void main(String[] args) throws Exception {
String xml = "<document><body><stock>AAPL</stock></body></document>";
ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory abstractFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder factory = abstractFactory.newDocumentBuilder();
Document doc = factory.parse(bais);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
System.out.println(abstractFactory.getClass());
System.out.println(factory.getClass());
}
}
-------------------------------------------------------
Exercise AbstractFactory
- Code Walkthrough
- AbstractFactory
- Factory
- Product
枚举类型
Demo - main方法
抽象工厂类
子工厂子类
信用卡子类及信用卡抽象父类
校验器子类及校验器抽象父类
调试Demo
package com.pattern.abstractfactory;
public class AbstractFactoryDemo {
public static void main(String[] args) {
CreditCardFactory abstractFactory = CreditCardFactory.getCreditCardFactory(775);
CreditCard card = abstractFactory.getCreditCard(CardType.PLATINUM);
System.out.println(card.getClass());
abstractFactory = CreditCardFactory.getCreditCardFactory(600);
CreditCard card2 = abstractFactory.getCreditCard(CardType.GOLD);
System.out.println(card2.getClass());
}
}
package com.pattern.abstractfactory;
public abstract class CreditCardFactory {
public static CreditCardFactory getCreditCardFactory(int creditScore) {
if(creditScore > 650) {
return new AmexFactory();
} else {
return new VisaFactory();
}
}
public abstract CreditCard getCreditCard(CardType cardType);
public abstract Validator getValidator(CardType cardType);
}
package com.pattern.abstractfactory;
public class AmexFactory extends CreditCardFactory {
@Override
public CreditCard getCreditCard(CardType cardType) {
switch (cardType) {
case GOLD:
return new AmexGoldCreditCard();
case PLATINUM:
return new AmexPlatinumCreditCard();
default:
break;
}
return null;
}
@Override
public Validator getValidator(CardType cardType) {
return new AmexGoldValidator();
}
}
package com.pattern.abstractfactory;
public class AmexGoldCreditCard extends CreditCard {
}
package com.pattern.abstractfactory;
public abstract class CreditCard {
protected int carNumberLength;
protected int cscNumber;
public int getCarNumberLength() {
return carNumberLength;
}
public void setCarNumberLength(int carNumberLength) {
this.carNumberLength = carNumberLength;
}
public int getCscNumber() {
return cscNumber;
}
public void setCscNumber(int cscNumber) {
this.cscNumber = cscNumber;
}
}
package com.pattern.abstractfactory;
public class AmexGoldValidator extends Validator {
@Override
public boolean isValid(CreditCard creditCard) {
return false;
}
}
package com.pattern.abstractfactory;
public abstract class Validator {
public abstract boolean isValid(CreditCard creditCard);
}
--------------------------------------------------------
Pitfalls
- Complexity
- Runtime switch
- Pattern within a pattern
- Problem specific
- Starts as a Factory
--------------------------------------------------------
Contrast
Factory
- Returns various instances (Multiple constructors)
- Interface driven
- Adaptable to environment more easily
- Implemented with a Factory
- Hides the Factory
- Abstracts Environment
- Built through Composition
--------------------------------------------------------
- Group of similar Factories
- Complex
- Heavy abstraction
- Framework pattern
--------------------------------------------------------
Design Patterns in Java: Structural
https://blog.csdn.net/u012596785/article/details/79736113
Design Patterns in Java: Behavioral
https://blog.csdn.net/u012596785/article/details/79736120