Design pattern abstract factory pattern

Last article: Factory mode

1. What is abstract factory mode?

In the last article, we also said that the abstract factory pattern can be said to be a super factory, through which other factories can be created. In the abstract factory pattern, the interface is the factory responsible for creating a related object. Without specifying their specific classes, each generated factory can create objects according to the factory pattern.

1. When to use abstract factory mode?

The products of   system have multiple product families, and we only need to consume products in one of them. What is a product family?

Take mobile phones for example:

3. How to implement abstract factory pattern?

1) Define mobile interface
/**
 * Define mobile interface
 */
public interface Phone {
	
	//Define phone features
	/**
	 * Boot up
	 */
	void boot();
	/**
	 * Shutdown
	 */
	void shutdown();
	/**
	 * Send message
	 */
	void sendMessage();
	
}

2) Define router interface
/**
 * Define router interface
 */
public interface Router {

	//Define router functions

	/**
	 * Boot up
	 */
	void boot();
	/**
	 * Shutdown
	 */
	void shutdown();
	/**
	 * Open wifi
	 */
	void openwifi();
}
3) Define Xiaomi Mobile

This is the specific implementation of Xiaomi mobile phone

/**
 * Define Xiaomi Mobile
 */
public class XiaomiPhone implements Phone {

	@Override
	public void boot() {
		System.out.println("Turn on Xiaomi Mobile");
	}

	@Override
	public void shutdown() {
		System.out.println("Xiaomi's cell phone is off");
	}

	@Override
	public void sendMessage() {
		System.out.println("Xiaomi sends SMS via mobile phone");
	}
}

4) Define Xiaomi router

This class is the specific implementation class of Xiaomi router.

/**
 * Define Xiaomi router class
 */
public class XiaomiRouter implements Router {

	@Override
	public void boot() {
		System.out.println("Turn on Xiaomi router");
	}

	@Override
	public void shutdown() {
		System.out.println("Turn off Xiaomi router");
	}

	@Override
	public void openwifi() {
		System.out.println("Xiaomi router on wifi");
	}
}

5) Define Huawei Mobile

This is the specific implementation of Huawei mobile phones

/**
 * Define Huawei Mobile
 */
public class HuaweiPhone implements Phone {

	@Override
	public void boot() {
		System.out.println("Turn on Huawei Mobile");
	}

	@Override
	public void shutdown() {
		System.out.println("Huawei mobile phone shutdown");
	}

	@Override
	public void sendMessage() {
		System.out.println("Huawei mobile sends SMS");
	}
}
6) Define Huawei router class

This is the specific implementation class of Huawei router

/**
 * Define Huawei router class
 */
public class HuaweiRouter implements Router {

	@Override
	public void boot() {
		System.out.println("Turn on Huawei router");
	}

	@Override
	public void shutdown() {
		System.out.println("Turn off Huawei router");
	}

	@Override
	public void openwifi() {
		System.out.println("Huawei router on wifi");
	}
}
7) Define abstract factory class
/**
 * Define abstract factory class
 */
public interface AbstractFactory {

	/**
	 * Cell phone production
	 */
	Phone productPhone();

	/**
	 * Production router
	 */
	Router productRouter();

}
8) Define millet factory
/**
 * Define millet factory
 */
public class XiaomiFactory implements AbstractFactory {

	@Override
	public Phone productPhone() {
		return new XiaomiPhone();
	}

	@Override
	public Router productRouter() {
		return new XiaomiRouter();
	}
}
9) Define Huawei factory
/**
 * Define Huawei factory
 */
public class HuaweiFactory implements AbstractFactory {

	@Override
	public Phone productPhone() {
		return new HuaweiPhone();
	}

	@Override
	public Router productRouter() {
		return new HuaweiRouter();
	}
}

10) Write test class
/**
 * Test class
 */
public class Test {

	public static void main(String[] args) {
		//Create a millet factory
		XiaomiFactory xiaomiFactory = new XiaomiFactory();
		//Xiaomi factory produces Xiaomi mobile phones and obtains the objects of Xiaomi mobile phones
		Phone phone = xiaomiFactory.productPhone();
		//Use Xiaomi mobile function
		phone.boot();
		phone.sendMessage();
		phone.shutdown();

		/**
		 * Huawei's operations are as follows
		 */

	}
}
11) Operation results

4. Advantages and disadvantages of abstract factory mode?

1) Advantages:

1. When a product family (Xiaomi, Huawei) is designed to work together, this mode can ensure that only one product is always used.
2. When using, we don't need to care about the details of its implementation, just go to the factory to get it.

2) Disadvantages:

1. In the factory, it has been specified which products to produce, and it is troublesome to expand the production type (for example, millet produces household appliances, so the factory code needs to be modified)
2. The factory mode is easy, but it is more abstract and more difficult to understand.

Next article: Builder pattern

73 original articles published, 36 praised, 6965 visited
Private letter follow

Tags: Mobile

Posted on Thu, 16 Jan 2020 00:35:04 -0800 by wiegs