My understanding of the abstract factory pattern

First of all, we want to understand the abstract factory pattern must first understand simple factory pattern and factory pattern!

The concept: to create a series of related or dependent objects interface, without specifying their concrete classes.
The following roles: the
abstract factory class
concrete factory class
abstract product category
specific product type
different from the factory method is:
in the abstract factory model, the concept of a product family: the so-called product family, means located in different product grades structure function associated product family composed. Abstract factory model provides a range of products on the formation of a product family; the factory method provides a range of products called hierarchical structure.
Factory Method pattern: an abstract class product, it can be derived from a plurality of specific product categories.
An abstract factory class, can be derived from a plurality of concrete factory class.
Each concrete factory class can only create an instance of a specific product category.
Abstract factory pattern: a plurality of abstract classes of products, each product abstract class can be derived the plurality of specific product categories.
An abstract factory class, can be derived from a plurality of concrete factory class.
Each concrete factory class can create multiple instances of a specific product category.

Factory Method pattern is only an abstract product class, and there are more abstract factory pattern.
Factory method pattern of concrete factory class can only create an instance of a specific product category, and abstract factory pattern can create more.

Note that: in the time of writing revolves around four characters of the abstract factory, all the classes in addition to the customer's output class must belong to one of four roles:
the abstract factory class
concrete factory class
abstract product class
specific product category

After complete understanding if ye are directly on the code:

Abstract Factory Test:

There are two brand nike adidas

Everyone has clothes (clothes) and shoes (shoes) products such as: nike shoes, nike clothes

Please use the abstract factory to write the above content (does not require the customer class), and to each key module Note well, what is the specific modules such as: Which specific product category

package com.java.textfactory;
/**
 * 客户输出类
 * @author Think
 *
 */
public class Client {
	public static void main(String[] args) {
		//买一件Nike的衣服
		Factory factory=new Clothes();//先找到卖衣服的商店
		Nike nike=factory.buyNike();//再找到商店里Nike的品牌
		nike.show();//得到耐克的衣服
		
		//买一件Nike鞋子也是同样的原理
		Factory factory2=new Shoes();//找到到卖鞋子的商店
		Nike nike2=factory2.buyNike();//找到商店里Nike的品牌鞋子
		nike2.show();//输出Nike鞋子
		
		
	}
}

package com.java.textfactory;
/**
 * 阿迪达斯品牌
 * 抽象产品
 * @author Think
 *
 */
public interface Adidas {
	void show();
}

package com.java.textfactory;
/**
 *阿迪达斯的鞋子
 *具体产品类
 * @author Think
 *
 */
public class Adidasshoes implements Adidas  {

	@Override
	public void show() {
		System.out.println("adidas鞋子");
	}

}

package com.java.textfactory;
/**
 * 阿迪达斯的衣服
 * 具体产品类
 * @author Think
 *
 */
public class Adidasclothes implements Adidas{

	@Override
	public void show() {
		System.out.println("Adidas衣服");
	}

}

package com.java.textfactory;
/**
 * 耐克品牌
 * 抽象产品类
 * @author Think
 *
 */
public interface Nike {
	void show();
}

package com.java.textfactory;
/**
 * 耐克的鞋子
 * 具体产品类
 * @author Think
 *
 */
public class Nikeshoes implements Nike{

	@Override
	public void show() {
		System.out.println("耐克鞋子");
	}

}

package com.java.textfactory;
/**
 * 耐克衣服
 * 具体产品类
 * @author Think
 *
 */
public class Nikeclothes implements Nike{

	@Override
	public void show() {
		System.out.println("耐克衣服");
	}

}

package com.java.textfactory;

/**
 * 商店 
 * 抽象工厂类
 * 
 * @author Think
 *
 */
public interface Factory{
	
	Nike buyNike();

	Adidas buyAdidas();
}

package com.java.textfactory;
/**
 * 衣服工厂
 * 具体工厂类
 * @author Think
 *
 */
public class Clothes implements Factory{

	@Override
	public Nike buyNike() {
		return new Nikeclothes();
	}

	@Override
	public Adidas buyAdidas() {
		
		return new Adidasclothes();
	}

}

package com.java.textfactory;
/**
 * 鞋子工厂
 * 具体工厂类
 * @author Think
 *
 */
public class Shoes implements Factory{

	@Override
	public Nike buyNike() {
		return new Nikeshoes();
	}

	@Override
	public Adidas buyAdidas() {
		
		return new Adidasshoes();
	}

}

Published 108 original articles · won praise 46 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_44739706/article/details/103088477