Creation-Abstract Factory Learning

1. The intent of the abstract factory pattern;

  Provides an interface for creating a series of related or interdependent objects without specifying their concrete classes.

 

2. The applicability of the abstract factory pattern;

  • When a system is to be created, composed and represented independently of its products.
  • When a system is to be configured by one of several product families.
  • When you want to emphasize the design of a series of related product objects for joint use.
  • When you provide a product class library, but only want to show their interface and not the implementation.

3. Scenario description;

  Consider a FurnitureFactory that produces many different styles of furniture. Different styles of furniture collections can provide different combinations of doors, windows, floors, etc., to provide different looks and behaviors for the same house.

 

4. Abstract factory pattern class diagram;

  

  Role :

  • AbstractFactory: Provides an interface for the user class Client to use the factory to create objects;
  • Client: User class;
  • ConcreteFactory1, ConcreteFactory2: The concrete implementation class of the AbstractFactory class, including the method of creating concrete objects; encapsulates different concrete products as a series (such as ProductA1, ProductB1, ProductC1);
  • ProductA1, ProductA2: generally have the same parent class ProductA, but ProductA1 and ProductA2 provide different presentation forms;

5. Code implementation;

  5.1 Project structure drawing;

  

  5.1.2 Instance class diagram;

   
   The classes in the instance class diagram basically correspond to the generalized class diagram of the design pattern.

   Where FurnitureFactory is an interface, ExpensiveDoor is a subclass of Door, ExpensiveFloor is a subclass of Floor, and ExpensiveWindow is a subclass of Window.

  5.2 Practice;
    5.2.1 Basic component class;

    Window.java

package com.crazysnail.furnitures;

public class Window {
	public Window(){
		System.out.println("I'm a ordinary window");
	}
}

   Floor.java

package com.crazysnail.furnitures;

public class Floor {
	public Floor(){
		System.out.println("I'm a ordinary floor.");
	}
}

    Door.java

package com.crazysnail.furnitures;

public class Door {
	public Door(){
		System.out.println("I'm a ordinary door.");
	}
}

   ExpensiveWindow.java 

package com.crazysnail.furnitures;

public class ExpensiveWindow extends Window {
	public ExpensiveWindow(){
		System.out.println("I'm a expensive window.");
	}
}

   ExpensiveFloor.java 

package com.crazysnail.furnitures;

public class ExpensiveFloor extends Floor {
	public ExpensiveFloor(){
		System.out.println("I'm a expensive floor");
	}
}

    ExpensiveDoor.java 

package com.crazysnail.furnitures;

public class ExpensiveDoor extends Door{
	public ExpensiveDoor(){
		System.out.println("I'm a expensive door.");
	}
}

    5.2.2工厂类;

    FurnitureFactory.java

package com.crazysnail.abstractfactory;

//抽象工厂模式中对外开放的接口,接口中定义了抽象工厂能够生产的产品种类
public interface FurnitureFactory{
	public Window createWindow();
	public Floor createFloor();
	public Door createDoor();
}

    OrdinaryFurnitureFactory.java 

package com.crazysnail.abstractfactory;

//普通家具的工厂类,组合了普通的Window、Floor、Door类作为一个系列来使用
public class OrdinaryFurnitureFactory implements FurnitureFactory {
	@Override
	public Window createWindow() {
		return new Window();
	}
	@Override
	public Floor createFloor() {
		return new Floor();
	}
	@Override
	public Door createDoor() {
		return new Door();
	}
}

   ExpensiveFurnitureFactory.java 

package com.crazysnail.abstractfactory;

//昂贵家具工厂类,组合了昂贵的ExpensiveWindow、ExpensiveFloor、ExpensiveDoor类,作为一个系列来使用
public class ExpensiveFurnitureFactory implements FurnitureFactory {
	@Override
	public Window createWindow() {
		return new ExpensiveWindow();
	}
	@Override
	public Floor createFloor() {
		return new ExpensiveFloor();
	}
	@Override
	public Door createDoor() {
		return new ExpensiveDoor();
	}
}

   5.2.3 使用抽象工厂举例;

    DecoretionCompany.java 

package com.crazysnail.abstractfactory;

/**
 * 
 * 
 * 易于交换产品系列
 * 有利于控制产品的一致性,控制使用相同系列的门、窗、地板
 * 提供给用户的都是一些系列化的产品
 * 
 * But,难以支持新种类的产品;
 * 
 * 提供给用户的接口仅仅是不同的家具工厂,但是用户并不知晓具体的类
 *
 */
public class DecorationCompany {
	public void decorateHouse(FurnitureFactory factory, House house){
		Window window = factory.createWindow();
		Door door = factory.createDoor();
		Floor floor = factory.createFloor();
		
		/*装饰*/
		house.addWindow(window);
		house.addDoor(door);
		house.addFloor(floor);
	}
}

6、总结;

  6.1 抽象工厂的优点;

  • 分离了具体的类——提供给用户类的接口仅仅是抽象工厂类及其子类,隐藏了组件类的具体实现。
  • 使得易于交换产品系列——在用户类中使用时,可以很方便的替换产品的系列,如只要DecorationCompany类中的decorateHouse方法中factory参数,便可以替换整个样式。
  • 有利于产品的一致性——将一组不同的组件组成一个系列来使用,如例子中的将Window、Door、Floor封装到OrdinaryFurnitureFactory中作为一个系列使用,将ExpensiveWindow、ExpensiveDoor、ExpensiveFloor封装到ExpensiveFurnitureFactory中来作为一个系列来使用。

  6.2 抽象工厂的缺点;

  •   难以支持新种类的产品——若要能够生产新的产品,则需要同时修改抽象工厂类及其子类。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326523991&siteId=291194637
Recommended