Skip to content

Conversation

@ilopX
Copy link
Collaborator

@ilopX ilopX commented May 16, 2022

Abstract Factory Pattern

Abstract Factory is a creational design pattern that lets you produce families of related objects
without specifying their concrete classes.

Tutorial: here.

Online demo:

Click on the picture to see the demo.

image

Diagram:

image

Client code (using the "createShape" method):

final app = App(
  tools: Tools(
    factories: [
      TxtFactory(),
      LineFactory(),
      CircleFactory(),
      TriangleFactory(),
      StarFactory(),
    ],
  ),
);

class App {
  void addShape(double x, double y) {
    final newShape = activeFactory.createShape(x, y, activeColor);
    shapes.add(newShape);
  }
}

mixin IconBoxMixin implements FactoryTool {
  Image? _icon;

  @override
  Image get icon => _icon!;

  Future<void> updateIcon(Color color) async {
    final shape = createShape(0, 0, color);
    final pngBytes = await _pngImageFromShape(shape);
    _icon = Image.memory(pngBytes);
  }
}

@ilopX ilopX merged commit f3d74d6 into RefactoringGuru:main May 16, 2022
@ilopX ilopX deleted the too_panel branch May 16, 2022 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant