Let's Take The Adapter Design Pattern To The Next Level

preview_player
Показать описание

The Adapter pattern is really useful if you need to connect your application with another system, but you can't change the code of that system and you want to reduce coupling. In this video, I show you how the pattern works, and then show you a functional variant of the pattern using partial application.

🔖 Chapters:
0:00 Intro
0:55 Explaining the example
2:13 Using another config type
3:16 What is the Adapter pattern?
3:57 Object-based Adapter
5:33 Changing the code to use an (object-based) XML adapter
12:21 Class-based Adapter
13:12 Changing the code to use a class-based adapter
15:25 Using a partially applied function as an adapter
18:44 Changing the XML adapter to a functional variant

#arjancodes #softwaredesign #python
Рекомендации по теме
Комментарии
Автор

I am a professional dev but I still enjoy listening to your videos, which I think speaks to your content and skill.

Contra
Автор

You have revealed to me a new way to implement this pattern that I had not thought of using partial. Moreover, the explanation you make is very elegant....
I really like this channel because it talks about something different in python, a part that was not being talked about.
I hope to talk about the algorithms in due course

davidmoran
Автор

If you know for sure that a getter is all you'll ever need from Config, the functional approach is fine. But say in the future your need Experiment to have the ability to fetch nested items, or to modify the config. The Protocol can easily be extended to support these features. But for the getter, the code may need serious refactoring to accommodate these.

pukkandan
Автор

Wow, you got class, the new setup is amazing, keep it up!

agar.iodestiny
Автор

I love that you use vim mode and it feels magical

civfiveaddict
Автор

Arjan, awesome tutorial as always.
You can use CMD+D (CTRL+D on windows) to select multiple occurrences of a string in VS code. so you can modify them all at once :)

UNgineering
Автор

Hi Arjan, you can use or to fetch the file contents without open. Thanks for your work!

ferchen
Автор

I never really understood why the naming convention for a BeautifulSoup object is 'soup' until this video. "bs" would decrease my productivity as I would always stop to giggle a bit each time I use it.

CaptainCsaba
Автор

You always explain the topics very understandable and excellent. I can get them even not knowing english well. Thanks for videos!

markervictor
Автор

The Partial Function seems like a massive cheat code! Can't believe haven't been using this. Going to have to watch the other video.

thekishanpatel
Автор

Thanks, Arjan! This helped me adapt a strategy based codebase to a function based web app. 😸

prestonfrasch
Автор

Hmm, in the XML get() method you do "if not value return default". This should probably be "if value is None return default" because value could be 0 or "".

ChrisBNisbet
Автор

I swear your channel gave me more knowledge than 4 years of studying computer science

eZwoodbne
Автор

Nice video!
I would love to see a video comparing bridge and adapter design pattern :)

EW-mbih
Автор

6:43 If you want some extra fun, you can name the function `__getitem__()` and have your adapter look and behave exactly like a dictionary. That way you won't have to do but could simplify to `self.config['data_path']`.
The obvious caution here is that it's not actually a dictionary. One might get away with read-only data like key->value configuration objects, but I'd not use it for mutable data structures. You might get away with implementing the dunder methods for dictionaries, but it gets messy having a thing behave like a dictionary that isn't one.

I'd probably not pick an adapter pattern at all but have a Builder/Factory that re-writes the data-structure on loading it from the config file and returns for example a dataclass. `def config_from_json(filename: str) -> dict: pass` and `def config_from_xml(filename: str) -> dict: pass` etc.

frustbox
Автор

Great video and I love the new set!
Glad to see some more videos about design patterns :)

Just a quick note: The camera in the intro is not exactly parallel to the table. The table edge on the cameras left is a tiny bit lower than on the right.

ItsThoronath
Автор

Changing background light is moving focus from the content to it. I hope this feedback will be useful. 🤞
Besides that, very good video, keep rolling to the 💯k 💪

vasilijestosic
Автор

Hey Arjan,
i like that video as all your other stuff :)

would it be wrong or "bad" style to move file open and bs instantiation in adapter XMLConfig?
for e.g. i would do in main only
path_to_cfg = pathlib.Path.cwd() / "config.xml"
adapter = XMLConfig(path_to_config)

and then adapter be a dataclass
where in __post_init__ i would do
with open(file...
and s.bs = BeautifulSoup...

and finally i would also create a JSONConfig adapter with the slight changes in regards of the json file handling

antecavlina
Автор

Hi Arjan, another great video. Thanks! How about doing some DDD with Python examples? I think there’s too little such tutorials.

WojciechStudio
Автор

Great video! It really shows the power of adapter pattern. Keep up the good work. I love the new studio - but please don’t cycle the color of the lights, it is quite distracting…

nnenad
welcome to shbcf.ru