Good day !
I want to learn how to properly design my applications. To make the code readable for other programmers. I know that there are design patterns , but I don't know how to use them.
I want you to help me understand how I can better design this task:
There is a class that converts an XML file and returns a
List<>with this data.
There is a class that does image manipulations (in the method parameter, the path to the file is taken from XML), and this method should return the nth number of images (there are a lot of them), since I crop one large one several times. Is it possible to make some method in this class to factory create pictures?
And a class that writes these pictures to the database.
There is an idea to implement threads (
Threads ) at the moment of dividing the picture into parts and writing them to the database.
What patterns are more suitable for my task? And explain to a beginner how you, professionals, choose them? The code is already written, but I would call it bad code. I want to learn how to do everything right! Thanks for understanding.
The topic of the applicability of design patterns draws on a very voluminous article or even a book; it is impossible to put all this in an answer on the forum. But since you asked, here are some tips:
- If the understanding of design patterns is poorly given, read the Design Patterns of Freeman, Sierra and Bates. This book explains the most common patterns very clearly.
- Read the classic work on this topic – Object Oriented Design Techniques. Gang of Four design patterns . This book is best used as a reference that describes the purpose, scope, implementation details, and benefits of using specific patterns.
- In addition to the classic patterns described in these books, there are patterns that are applied in specific areas: enterprise, parallel programming, web development, etc. There are pattern catalogs where you can find them.
- Think about whether you really need patterns. There are many refactoring techniques that allow you to make the code cleaner, more beautiful, readable, reliable. You call your code bad – think about why you think so. Many repeating sections – take out in separate methods. Many
if/else– take out in separate classes. A couple of hundred lines method – break it down into shorter ones that make meaningful and complete pieces of work. You can read about this in detail in the book Refactoring. Improving the existing code of Fowler and company, or briefly – in this cheat sheet .
- Think again if you need patterns. The main purpose of design patterns is to make code easily extensible and changeable. Think about whether your application will change, if so, how and in what places. If there are few or no such places, perhaps you are engaged in overengineering, that is, a senseless complication of the code.
- Never think "where should I put the pattern in my program". This approach is a direct path to the senseless complication of the code. The correct way of thinking is "there is a problem in my program that is well solved with the help of a pattern." To be sure, check the formulation of your problem with the areas of applicability of the pattern in the pattern catalogs.
Proper application design is a matter of experience. To gain this experience, examine your code as critically as possible, look for design problems, and fix them. Over time, you will begin to clearly see the "roughness" and potential errors, and later – to predict them at the design stage.
It is difficult to advise anything more specific within the framework of the answer. If you have specific questions about application design, feel free to ask. You will certainly be helped if you describe your task in more detail and give your own solution.