private static final class Apple implements IFruit {
@Override
public void eat() {
System.out.println(" eat Apple");
}
}
private static final class Banana implements IFruit {
@Override
public void eat() {
System.out.println(" eat Banana");
}
}
private static final class FruitFactory {
IFruit getFruit(String name) {
switch (name) {
case "Apple":
return new Apple();
case "Banana":
return new Banana();
default:
throw new UnsupportedOperationException("unsupported name");
}
}
}
public static void main(String[] args) {
FruitFactory fruitFactory = new FruitFactory();
fruitFactory.getFruit("Apple").eat();
fruitFactory.getFruit("Banana").eat();
fruitFactory.getFruit("Orange").eat();
}
执行输出:
eat Apple
eat Banana
Exception in thread "main" java.lang.UnsupportedOperationException: unsupported name
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
private final class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
private final class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
public interface Color {
void fill();
}
private final class Red implements Color {
@Override
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}
private final class Green implements Color {
@Override
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}
private final class Blue implements Color {
@Override
public void fill() {
System.out.println("Inside Blue::fill() method.");
}
}
public abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape);
}
private final class ShapeFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
} else if (shapeType.equalsIgnoreCase("SQUARE")) {
return new Square();
}
return null;
}
@Override
public Color getColor(String color) {
return null;
}
}
private final class ColorFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType) {
return null;
}
@Override
public Color getColor(String color) {
if (color == null) {
return null;
}
if (color.equalsIgnoreCase("RED")) {
return new Red();
} else if (color.equalsIgnoreCase("GREEN")) {
return new Green();
} else if (color.equalsIgnoreCase("BLUE")) {
return new Blue();
}
return null;
}
}
private static final class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
} else if(choice.equalsIgnoreCase("COLOR")){
return new ColorFactory();
}
return null;
}
}
上面代码不能直接执行,这边就是使用这个结构来说明一下抽象工厂方法的实现。可以看到,不同的行为提供了不同的工厂,最终由一个超级工厂来管理这些工厂。
public final class ImageLoaderConfiguration {
final Resources resources;
final int maxImageWidthForMemoryCache;
final int maxImageHeightForMemoryCache;
final int maxImageWidthForDiskCache;
final int maxImageHeightForDiskCache;
final BitmapProcessor processorForDiskCache;
final Executor taskExecutor;
final Executor taskExecutorForCachedImages;
final boolean customExecutor;
final boolean customExecutorForCachedImages;
final int threadPoolSize;
final int threadPriority;
final QueueProcessingType tasksProcessingType;
final MemoryCache memoryCache;
final DiskCache diskCache;
final ImageDownloader downloader;
final ImageDecoder decoder;
final DisplayImageOptions defaultDisplayImageOptions;
final ImageDownloader networkDeniedDownloader;
final ImageDownloader slowNetworkDownloader;
public ImageLoaderConfiguration.Builder threadPoolSize(int threadPoolSize) {
if (this.taskExecutor != null || this.taskExecutorForCachedImages != null) {
L.w("threadPoolSize(), threadPriority() and tasksProcessingOrder() calls can overlap taskExecutor() and taskExecutorForCachedImages() calls.", new Object[0]);
}
public ImageLoaderConfiguration build() {
this.initEmptyFieldsWithDefaultValues();
return new ImageLoaderConfiguration(this, (ImageLoaderConfiguration.SyntheticClass_1)null);
}
static {
mSupportAttrs.put(SkinConstant.ATTR_NAME_BACKGROUND, new BackgroundAttr());
mSupportAttrs.put(SkinConstant.ATTR_NAME_TEXTCOLOR, new TextColorAttr());
mSupportAttrs.put(SkinConstant.ATTR_NAME_SRC, new SrcAttr());
mSupportAttrs.put(SkinConstant.ATTR_NAME_DRAWABLE_LEFT, new TextDrawableLeftAttr());
mSupportAttrs.put(SkinConstant.ATTR_NAME_DRAWABLE_RIGHT, new TextDrawableRightAttr());
mSupportAttrs.put(SkinConstant.ATTR_ITEM_ICON_HINT, new BottomNavigationItemIconTintAttr());
mSupportAttrs.put(SkinConstant.ATTR_ITEM_TEXT_COLOR, new BottomNavigationItemTextColorAttr());
}