Skip to content

Refactoring v0.18.9 #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ It contains **Dart** examples for all classic **GoF** design patterns.
# Implementation checklist:
- [ ] **Creation**
- [ ] **Abstract Factory**
- [x] **Builder** - [[Color Text Format](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/builder/color_text_format)]
- [x] **Builder** - [[Color Text Format](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/builder/color_text_format)]
- [ ] **Factory Method**
- [x] **Prototype**] - [[Shapes](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/prototype/shapes)]
- [x] **Prototype**] - [[Shapes](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/prototype/shapes)]
- [ ] **Singleton**
- [ ] **Behavioral**
- [x] **Chain of Responsibility** - [[Server Middleware](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/chain_of_responsibility/server_middleware)]
- [x] **Command** - [[Text Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/command/text_editor)]
- [x] **Chain of Responsibility** - [[Server Middleware](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/chain_of_responsibility/server_middleware)]
- [x] **Command** - [[Text Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/text_editor)]
- [ ] Interpreter
- [ ] **Iterator**
- [ ] **Mediator**
- [x] **Memento** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/memento/conceptual)] [[Memento Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/memento/memento_editor)]
- [x] **Observer** - [[Open-Close Editor Events](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/observer/open_close_editor_events)] [[AppObserver](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/observer/app_observer)] [[Subscriber Flutter Widget](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/observer/subscriber_flutter_widget)]
- [x] **Memento** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/memento/conceptual)] [[Memento Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/memento/memento_editor)]
- [x] **Observer** - [[Open-Close Editor Events](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/open_close_editor_events)] [[AppObserver](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/app_observer)] [[Subscriber Flutter Widget](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/subscriber_flutter_widget)]
- [ ] **State**
- [ ] **Template Method**
- [ ] **Visitor**
- [ ] **Strategy**
- [ ] **Structural**
- [x] **Adapter** - [[Text Graphics](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/adapter/text_graphics)] [[Square Round conflict](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/adapter/square_round_conflict)] [[Flutter Adapter](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/adapter/flutter_adapter)]
- [x] **Bridge** - [[Remote Device Control](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/bridge/devices_remote_control)] [[Clock](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/bridge/clock)]
- [x] **Composite** - [[Image Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/composite/image_editor)] [[Products and Boxes](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/composite/products_and_boxes)]
- [x] **Decorator** - [[Data Source Decoder](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/decorator/data_source_decoder)]
- [x] **Adapter** - [[Text Graphics](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/adapter/text_graphics)] [[Square Round conflict](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/adapter/square_round_conflict)] [[Flutter Adapter](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/adapter/flutter_adapter)]
- [x] **Bridge** - [[Remote Device Control](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/bridge/devices_remote_control)] [[Clock](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/bridge/clock)]
- [x] **Composite** - [[Image Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/composite/image_editor)] [[Products and Boxes](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/composite/products_and_boxes)]
- [x] **Decorator** - [[Data Source Decoder](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/decorator/data_source_decoder)]
- [ ] **Facade**
- [ ] **Flyweight**
- [ ] **Proxy**
Expand Down
4 changes: 2 additions & 2 deletions patterns/adapter/flutter_adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ This example shows how to adapt a non-reactive classic type application for Flut

### Dependency
This complex example includes these implementations:
- [[AppObserver](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/observer/app_observer)]
- [[SubscriberWidget](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/observer/subscriber_flutter_widget)]
- [[AppObserver](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/app_observer)]
- [[SubscriberWidget](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/subscriber_flutter_widget)]

### Online demo:
Click on the picture to see a [demo](https://refactoringguru.github.io/design-patterns-dart/#/adapter/flutter_adapter).
Expand Down
2 changes: 1 addition & 1 deletion patterns/adapter/text_graphics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ https://refactoring.guru/design-patterns/adapter
Adding shapes from a third-party library.

### Class Diagram:
![image](https://user-images.githubusercontent.com/8049534/146028651-1262b66d-b0ac-4fc9-9487-76a2d588f97e.png)
![image](https://user-images.githubusercontent.com/8049534/165749605-5361d5e2-e4a1-41ed-9d27-1c4965fa30d2.png)

### Client code:
```dart
Expand Down
2 changes: 1 addition & 1 deletion patterns/bridge/devices_remote_control/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Bridge pattern
This example rewrite from [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/master/src/refactoring_guru/bridge/example)
This example rewrite from [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/main/src/refactoring_guru/bridge/example)
But removed the ability to use null for devices. Instead, the EmptyDevice class is used.

### Remote device example
Expand Down
6 changes: 2 additions & 4 deletions patterns/builder/color_text_format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ https://refactoring.guru/design-patterns/builder
Using different text formats.

### Class diagram
![image](https://user-images.githubusercontent.com/8049534/152780645-4eed27fc-a483-432e-99b0-4a77f2adbd5d.png)
![image](https://user-images.githubusercontent.com/8049534/165747411-75d587d4-aa16-4a14-a16d-52f3fa461a11.png)

### Client code:
```dart
main() {
final reader = ColorTextReader(
text: 'I love looking at the blue sky, '
'eating red apples, '
'sitting on the green grass.',
text: 'I love looking at the blue sky, eating red apples and sitting on the green grass.',
);

final html = reader.convert(HtmlConverter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Chain of Responsibility is a behavioral design pattern that lets you pass reques
https://refactoring.guru/design-patterns/chain-of-responsibility?#problem

### Origin source code:
This example rewrite from [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/master/src/refactoring_guru/chain_of_responsibility/example)
This example rewrite from [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/main/src/refactoring_guru/chain_of_responsibility/example)

### Diagram:
![image](https://user-images.githubusercontent.com/8049534/149488654-7ff5f659-4086-4d1a-ae44-326c71fb880a.png)
Expand Down
2 changes: 1 addition & 1 deletion patterns/command/text_editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ possible to revert an operation if needed.
More detailed explanation on [RefactoringGuru](https://refactoring.guru/design-patterns/command?#pseudocode).

## Origin source code:
This example rewrite from [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/master/src/refactoring_guru/command/example).
This example rewrite from [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/main/src/refactoring_guru/command/example).
But slightly changed, see the class diagram.

## Diagram:
Expand Down
2 changes: 1 addition & 1 deletion patterns/composite/image_editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Composite is a structural design pattern that lets you compose objects into tree
https://refactoring.guru/design-patterns/composite?#pseudocode

## Origin source code:
This example rewrite from [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/master/src/refactoring_guru/composite/example)
This example rewrite from [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/main/src/refactoring_guru/composite/example)

### Diagram:
![image](https://user-images.githubusercontent.com/8049534/149174388-25ca21b1-d762-40b5-a853-528abe18b66c.png)
Expand Down
9 changes: 4 additions & 5 deletions patterns/memento/memento_editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ object without revealing the details of its implementation.
Tutorial: [here](https://refactoring.guru/design-patterns/memento).

### Online demo:
Click on the picture to see a [demo](https://RefactoringGuru.github.io/design-patterns-dart/#/memento/flutter_memento_editor).
Click on the picture to see the [demo](https://RefactoringGuru.github.io/design-patterns-dart/#/memento/flutter_memento_editor).

[![image](https://user-images.githubusercontent.com/8049534/165401175-88bc4593-4624-45b4-8c03-6f1390ed771a.png)](https://refactoringguru.github.io/design-patterns-dart/#/memento/flutter_memento_editor)


### Dependency Patterns
This complex example includes these implementations:
- [[AppObserver](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/observer/app_observer)]
- [[SubscriberWidget](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/observer/subscriber_flutter_widget)]
- [[AppObserver](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/app_observer)]
- [[SubscriberWidget](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/subscriber_flutter_widget)]

### Diagram:
![image](https://user-images.githubusercontent.com/8049534/165399085-06835617-8ef1-4e2f-930f-03d730433afb.png)
![image](https://user-images.githubusercontent.com/8049534/165758516-1de543f5-666d-4e07-958d-2d8fceb73af9.png)

### Client code:
```dart
Expand Down
16 changes: 1 addition & 15 deletions patterns/memento/memento_editor/editor/manipulator.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import '../../../adapter/flutter_adapter/classic_app/classic_app.dart';
import '../shapes/shapes.dart';

mixin Manipulator implements ClassicApp, Shapes {
mixin Manipulator implements Shapes {
var _isMouseDown = false;

@override
void onMouseDown(double x, double y) {
_isMouseDown = true;
final currSelection = activeShape;

select(x, y);

if (currSelection == activeShape) {
return;
}

if (activeShape == null) {
unSelect();
}

repaint();
}

@override
void onMouseMove(double x, double y) {
if (_isMouseDown) {
activeShape?.dragTo(x, y);
repaint();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui';

import '../../../adapter/flutter_adapter/classic_app/classic_app.dart';
import '../shapes/shapes.dart';
import '../shapes/shape.dart';
import 'snapshot.dart';

mixin Originator implements Shapes, ClassicApp {
mixin Originator implements Shapes {
Snapshot backup() {
final data = _allocateBuffer();
_writeShapes(data);
Expand Down
9 changes: 7 additions & 2 deletions patterns/memento/memento_editor/shapes/active_shape.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ class ActiveShape {
final Shape shape;
final void Function() repaint;

ActiveShape(this.shape, this._xStart, this._yStart, this.repaint);
ActiveShape(this.shape, this._xStart, this._yStart, this.repaint) {
repaint();
}

void changeSize(double delta) {
final currentSize = shape.size;
Expand All @@ -30,7 +32,6 @@ class ActiveShape {
void dragTo(double x, double y) {
shape._x = x + _xStart;
shape._y = y + _yStart;

repaint();
}

Expand All @@ -53,4 +54,8 @@ class ActiveShape {
..color = Color(0xff26e6ff),
);
}

void dispose() {
repaint();
}
}
7 changes: 6 additions & 1 deletion patterns/memento/memento_editor/shapes/shapes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mixin Shapes implements ClassicApp {
if (shape != null) {
_activeShape = ActiveShape(shape, shape.x - x, shape.y - y, repaint);
} else {
_activeShape = null;
unSelect();
}
}

Expand All @@ -30,6 +30,11 @@ mixin Shapes implements ClassicApp {
}

void unSelect() {
if (_activeShape == null) {
return;
}

_activeShape?.dispose();
_activeShape = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class ShapePropertiesWidget extends StatelessWidget {
colors: colors,
onColorSelect: (newColor) {
app.editor.activeShape?.changeColor(newColor);

},
),
],
Expand Down
6 changes: 4 additions & 2 deletions patterns/observer/app_observer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ multiple objects about any events that happen to the object they’re observing.
Tutorial: [here](https://refactoring.guru/design-patterns/observer).

## AppObserver example
This example was created to be used in a more complex example.
A complex example will be implemented later.
This example was created to be used in a more complex examples:
- [[SubscriberWidget](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/subscriber_flutter_widget)]
- [[FlutterAdapter](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/adapter/flutter_adapter)]
- [[MementoEditor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/memento/memento_editor)]

### Diagram:
![image](https://user-images.githubusercontent.com/8049534/152783537-0c39c119-2e5b-44fb-9be1-a88840c7f7a1.png)
Expand Down
4 changes: 2 additions & 2 deletions patterns/observer/app_observer/observer/app_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class AppObserver {
}

void unsubscribe<T extends Event>(EventFunction<T> func) {
final isDelete = _subscribers[T.hashCode]?.remove(func) ?? false;
final isDeleted = _subscribers[T.hashCode]?.remove(func) ?? false;

if (isDelete) {
if (isDeleted) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion patterns/observer/open_close_editor_events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ changes in its state.
More detailed explanation on [RefactoringGuru](https://refactoring.guru/design-patterns/observer?#pseudocode).

### Origin source code:
This example rewrite from [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/master/src/refactoring_guru/observer/example).
This example rewrite from [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/main/src/refactoring_guru/observer/example).

### Diagram:
![image](https://user-images.githubusercontent.com/8049534/150955865-7fbb29f3-ed48-4317-a356-cbb9cc79ed11.png)
Expand Down
2 changes: 1 addition & 1 deletion patterns/observer/subscriber_flutter_widget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Tutorial: [here](https://refactoring.guru/design-patterns/observer).
## Subscriber flutter widget example
This is a complex example of an Observer pattern, connected to a Flutter application. The example includes,
the previously implemented **AppObserver** example pattern,
which you can see [here](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/observer/app_observer).
which you can see [here](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/app_observer).

### Online demo:
Click on the picture to see a [demo](https://RefactoringGuru.github.io/design-patterns-dart/#/observer/subscriber_flutter_widget).
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: design_patterns_dart
description: Dart examples for all classic GoF design patterns.
version: 0.18.0
version: 0.18.9
homepage: https://refactoring.guru/design-patterns
repository: https://github.com/RefactoringGuru/design-patterns-dart
issue_tracker: https://github.com/RefactoringGuru/design-patterns-dart/issue
Expand Down