AbstractFactory Pattern

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

image.png


----------------------------------------------------------------------------

Everyday Example - DocumentBuilderFactory

image.png

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
AbstractFactory
  • Implemented with a Factory
  • Hides the Factory
  • Abstracts Environment
  • Built through Composition


--------------------------------------------------------

AbstractFactory Summary
  • 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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值