Skip to content
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

Improve abstract factory example #82

Merged
merged 2 commits into from
Apr 19, 2024
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
@startuml

class Bootstrap

package views {

class Header

class MainView

}

package components {

together {
interface Label

class LightLabel

class DarkLabel
}

together {
interface Button

class LightButton

class DarkButton
}

Label <|.. LightLabel
Label <|.. DarkLabel

Button <|.. LightButton
Button <|.. DarkButton
}

package legend <<Rectangle>> {
<> example_diamond

note left of example_diamond
if darkmode
endnote
}

<> header_label

<> main_label
<> main_button

Bootstrap "creates" ---> Header
Bootstrap "creates" ---> MainView

Header --> header_label
header_label --> LightLabel
header_label --> DarkLabel

MainView --> main_label
main_label --> LightLabel
main_label --> DarkLabel

MainView --> main_button
main_button --> LightButton
main_button --> DarkButton

@enduml
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@startuml

class Bootstrap

package views {

class Header

class MainView

}

package components {

together {
interface Label

class LightLabel

class DarkLabel
}

together {
interface Button

class LightButton

class DarkButton
}

interface ComponentFactory

class LightComponentFactory implements ComponentFactory

class DarkComponentFactory implements ComponentFactory


Label <|.. LightLabel
Label <|.. DarkLabel

Button <|.. LightButton
Button <|.. DarkButton

DarkComponentFactory --> DarkLabel
DarkComponentFactory --> DarkButton

LightComponentFactory --> LightLabel
LightComponentFactory --> LightButton
}

package legend <<Rectangle>> {
<> example_diamond

note left of example_diamond
if darkmode
endnote
}

<> create

Bootstrap "creates" ---> create
create --> LightComponentFactory
create --> DarkComponentFactory
Bootstrap "creates" ---> Header
Bootstrap ---> MainView

Header --> ComponentFactory

MainView --> ComponentFactory

@enduml
41 changes: 23 additions & 18 deletions topics/sw_concepts/sw_concept_slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,34 +351,39 @@ class CasiceConfigurationFactory:
AbstractFactory
-------

<https://github.com/iluwatar/java-design-patterns/blob/master/abstract-factory/README.md>
* Use Case:
* Es gibt 2 oder mehr Gruppen von Komponenten. Es sollen immer nur Komponenten aus einer der Gruppen erstellt werden.
* Beispiel Light/Dark Theme: es soll entweder Light oder Dark Theme sein, aber nie gemischt.
* Die gleichen If-Statements tauchen wiederholt an unterschiedlichen Stellen im Code auf. Man möchte diese Code-Duplizierung
verhindern, indem man die If-Statements an einem zentralen Ort platziert.

AbstractFactory Beispiel 1
von [Java Design Patterns/AbstractFactory](https://github.com/iluwatar/java-design-patterns/tree/07663ce2bdd46ca4697307068b9eb0d4c8888ead/abstract-factory/README.md)

AbstractFactory Beispiel: vorher
------
\colBegin{0.8}
![AbstractFactory Beispiel: vorher](images/abstract-factory/abstract-factory-bad-case.png){width=100%}
\colNext{0.2}
\colEnd

```python
class ButtonFactory:
def get_bwd_button(self, name, dim, text):
return BwdButton(name, dim, text)
AbstractFactory Beispiel: nachher
------

def get_fwd_button(self, name, dim, text):
return FwdButton(name, dim, text)
\colBegin{0.8}
![AbstractFactory Beispiel: nachher](images/abstract-factory/abstract-factory-good-case.png){width=100%}
BacLuc marked this conversation as resolved.
Show resolved Hide resolved
\colNext{0.2}
\small

def get_info_button(self, name, info_text):
return InfoButton(name, info_text)
* Die Anzahl "if darkmode" statements ist reduziert.
* Es ist jetzt viel einfacher, ein "HighContrast"-Theme einzubauen.

class ButtonFactoryC(ButtonFactory):
pass

class ButtonFactoryL(ButtonFactory):
pass
```
\colEnd

AbstractFactory Beispiel 2
------

![footer_m](images/button_factory/footer_m.png)
![footer_c](images/button_factory/footer_c.png)
![footer_m](images/abstract-factory/footer_m.png)
![footer_c](images/abstract-factory/footer_c.png)

Strategy
-------
Expand Down