1 Commits

Author SHA1 Message Date
n8r 55d6069b84 beauty builders 2024-05-02 13:04:05 -06:00
+62 -42
View File
@@ -20,32 +20,68 @@ class MyApp extends StatelessWidget {
}
}
abstract class ShapeFactory {
Widget build(Size size, Color color);
}
class SquareFactory extends ShapeFactory {
@override
Widget build(Size size, Color color) {
return SizedBox.fromSize(
size: size,
child: Container(color: color),
///
/// Shape Builder
///
abstract class ShapeBuilder {
Widget build(Size size, Widget child) {
return Container(
height: size.height,
width: size.width,
decoration: BoxDecoration(shape: boxShape, color: shapeColor),
child: Center(child: child),
);
}
BoxShape get boxShape;
Color get shapeColor;
}
class CircleFactory extends ShapeFactory {
class RedSquareBuilder extends ShapeBuilder {
@override
Widget build(Size size, Color color) {
return SizedBox.fromSize(
size: size,
child: Container(
decoration: BoxDecoration(shape: BoxShape.circle, color: color),
),
);
}
BoxShape get boxShape => BoxShape.rectangle;
@override
Color get shapeColor => Colors.red;
}
class BlueCircleBuilder extends ShapeBuilder {
@override
BoxShape get boxShape => BoxShape.circle;
@override
Color get shapeColor => Colors.blue;
}
///
/// Message Builder (Writer seemed appropriate)
///
abstract class MessageWriter {
Text write(String text);
}
class HotMessageWriter extends MessageWriter {
@override
Text write(String text) => Text(text, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18));
}
class ColdMessageWriter extends MessageWriter {
@override
Text write(String text) => Text(text, style: const TextStyle(color: Colors.white, fontSize: 14));
}
final List<ShapeBuilder> builders = [
RedSquareBuilder(),
BlueCircleBuilder(),
RedSquareBuilder(),
];
final List<MessageWriter> writers = [
ColdMessageWriter(),
HotMessageWriter(),
HotMessageWriter(),
];
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@@ -54,40 +90,24 @@ class MyHomePage extends StatefulWidget {
}
class _MyHomePageState extends State<MyHomePage> {
final List<ShapeFactory> shapeFactories = [CircleFactory(), SquareFactory()];
int factoryIndex = 0;
ShapeFactory get factory => shapeFactories[factoryIndex];
void swapFactories() {
if (factoryIndex == 0) {
setState(() => factoryIndex = 1);
return;
}
setState(() => factoryIndex = 0);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Abstract Factory'),
title: const Text('Builders'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Abstract Factories!!'),
Text('Using ${shapeFactories[factoryIndex].runtimeType.toString()}'),
const Text('Builders!!'),
...List.generate(
3,
(index) => factory.build(
Size(20.0 * (index + 1), 30.0 * (index + 1)),
[Colors.red, Colors.green, Colors.blue][index],
)),
ElevatedButton(
onPressed: () => swapFactories(),
child: const Text('Swap Factories'),
3,
(index) => builders[index].build(
Size(80.0 * (index + 1), 90.0 * (index + 1)),
writers[index].write('build ${index + 1}'),
),
),
],
),