Abstract Factory Model of 23 Design Patterns in GOF

Keywords: Session JDK JDBC Hibernate

Abstract factory pattern

Used to produce all products of different product families. (There is nothing we can do to add new products; support the increase of product family)
Abstract factory pattern is an upgraded version of factory method pattern. When there are multiple business categories and business classifications, it is a very good solution to generate the required objects through abstract factory pattern.

Sample code:
High and low end engines:

package com.xyj.factory.abstractFactory;

/**
 * Engine interface
 */
public interface Engine {
	void run();
	void start();
}

class LuxuryEngine implements Engine{
	@Override
	public void run() {
		System.out.println("Fast speed!");
	}

	@Override
	public void start() {
		System.out.println("Quick start-up!It can start and stop automatically.!");
	}
}

class LowEngine implements Engine{
	@Override
	public void run() {
		System.out.println("Slow speed!");
	}

	@Override
	public void start() {
		System.out.println("Start slowly!");
	}
}

High and low seats:

package com.xyj.factory.abstractFactory;

/**
 * Seat interface
 */
public interface Seat {
	void massage();
}

class LuxurySeat implements Seat{

	@Override
	public void massage() {
		System.out.println("High-end seats can be massaged automatically!");
	}
	
}

class LowSeat implements Seat{

	@Override
	public void massage() {
		System.out.println("Low-end seats should not be massaged!");
	}
	
}

High and low end tyres:

package com.xyj.factory.abstractFactory;

/**
 * Tire interface
 */
public interface Tyre {
	void revolve();
}

class LuxuryTyre implements Tyre{

	@Override
	public void revolve() {
		System.out.println("Small wear and tear of high-end tires!");
	}
	
}

class LowTyre implements Tyre{

	@Override
	public void revolve() {
		System.out.println("High-end tires wear heavily in rotation!");
	}
	
}

Abstract factory mode interface:

package com.xyj.factory.abstractFactory;

/**
 * Car Factory Interface
 */
public interface CarFactory {
	Engine createEngine();
	Seat createSeat();
	Tyre createTyre();
}

High-end automobile factory

package com.xyj.factory.abstractFactory;

/**
 * High-end automobile factory
 */
public class LuxuryCarFactory implements CarFactory{

	@Override
	public Engine createEngine() {
		return new LuxuryEngine();
	}

	@Override
	public Seat createSeat() {
		return new LuxurySeat();
	}

	@Override
	public Tyre createTyre() {
		return new LuxuryTyre();
	}

}

Low-end automobile factories:

package com.xyj.factory.abstractFactory;

/**
 * Low-end automobile factories
 */
public class LowFactory implements CarFactory{

	@Override
	public Engine createEngine() {
		return new LowEngine();
	}

	@Override
	public Seat createSeat() {
		return new LowSeat();
	}

	@Override
	public Tyre createTyre() {
		return new LowTyre();
	}

}

In this way, we can only use high-end factories to create high-end engines, seats, tires; high-end factories can only create low-end engines, seats, tires.

Test:

package com.xyj.factory.abstractFactory;

//test
public class Client {
	
	public static void main(String[] args) {
		CarFactory factory=new LuxuryCarFactory();
		//High-end Automobile Factory Creates High-end Engines, Seats and Tires
		Engine e = factory.createEngine();
		e.run();
		e.start();
		
	}
}

Summary of Factory Model

Key Points of Factory Model:
Simple factory mode (static factory mode) is used most in practice although it does not conform to design principles to some extent.
The factory method pattern extends by adding new factory classes without modifying existing classes.
Abstract factory model can not increase products, can increase product family!

Application scenarios:
The getInstance Method of Calendar in JDK
Acquisition of Connection Objects in JDBC
Session Factory Creates Session in Hibernate
IOC Container Creates Management bean Objects in spring
Document Builder Factory creates parser objects when parsing XML
newInstance() of Class object in reflection

Posted by Phate on Sat, 07 Sep 2019 04:08:49 -0700