2

I'm learning the java Spring framework. So far, I understood that Spring will make stuff transparent with its configurations so that IoC and Factory are not too complicated to implement...

Now I have coded A LOT but I have never thought of organizing my code based on design patterns...

My query is that for a basic CRUD, how (and for what goal) one would design classes based on IoC, then on Factory to that in the main the user can just calla simple MyObject.Insert(MyData) ?

Jason Krs
  • 179
  • 6

1 Answers1

5

->I'm learning the java Spring framework.

My congratulations, and condolences.

So far, I understood that Spring will make stuff transparent with its configurations so that IoC and Factory are not too complicated to implement

Well no. Spring encourages Dependency Injection, which can be a good thing. But you don't need Spring to do Dependency Injection.

With Spring what you get is either so many @autowired annotations spread in your code that you are now hopelessly coupled to the framework, or all your composition root (usually main) code moved into an xml file.

The annotations basically hide the fact that you're programming in a new language now. It's following a philosophy called convention over configuration, which works so long as everything goes as predicted. If you don't mind not being able to hire simple Java programmers but now must hire Java/Spring programmers, then fine. But these things hopelessly couple you to the framework. If that bothers you consider writing POJO's.

Moving the main code into an xml file gives you a clear separation of construction code and behavior code. The separation is a good thing and works fine with POJO's but you could have left that in main and done the separation by hand if you just trusted yourself not to mix construction and behavior when they are expressed in the same language. If you need this separation enforced fine but you could learn to just not make a mess by spreading new all over the place. new is most at home in a static context. POJO's are best off not even knowing that static methods exist.

Here is a design pattern for doing this:

static void main() {
    //Construct many objects and "wire" them together using dependency injection
    String greetee = "world"; // <- A dependency
    Greeter greeter = new Greeter(greetee); // <- An injection

    //Start the object graph ticking by calling only one method.
    greeter.greet(); // All behavior for this program happens now
}

You can construct many objects this way and connect them in wonderful ways. It doesn't have to be flat. You could use any creational patterns you like to do your construction. What you shouldn't do is mix any business logic behavior in that part of your code. We should only see behavior start when we execute main's last line. At this point we're out of the static context.

Spring and most all DI frameworks use this pattern. It just lumps the wiring of the "many objects" into one factory call that reads an xml file. If you think that makes it easier than using java constructors or creational patterns fine.

IoC is an important principle to understand. Simply using Spring won't make you understand it and doesn't really do anything to help implement it.

Inversion of Control (IoC) is the art of not forcing high level code to depend directly on low level code. This allows low level details to change without impacting the high level code or each other. Being able to change is what keeps software soft.

IoC does this by having one object talk to another through polymorphism. Some call this Tell, don't ask. The point is that you shouldn't have to know what you're talking to.

Now I have coded A LOT but I have never thought of organizing my code based on design patterns...

Design patterns are not so much a way of organizing code as they are a recurring pattern for solving problems in languages that can't trivially solve those problems. They are not a best practice nor are they a design principle. They're just stuff that happens, a lot. So we gave them a name to make talking about them easier. Patterns are not always good things in the design sense; if you're trying to use them to design your programs instead of treating them as tools that solve certain, specific problems, then you're doing it wrong.

IoC is a design principle (not a pattern). It requires some form of polymorphism. Some patterns give you different kinds of polymorphism. For example the strategy pattern and the template pattern both give you polymorphism. The first though composition and delegation. The second through inheritance. I'm not a fan of the second.

Play with Spring and discover what it's really doing for you. Decide for yourself if you really need what it offers. I've seen it clean up code. I've seen clean code that doesn't need it. It's really a question of who you're coding with.

candied_orange
  • 102,279
  • 24
  • 197
  • 315
  • In what way do @Autowired annotations couple you to the framework? – Jan Van den bosch Aug 08 '17 at 16:21
  • @JanVandenbosch mostly by forcing you to hire programmers who know what @ Autowired means rather than plain old java programmers. – candied_orange Aug 08 '17 at 16:25
  • lol Why did you also said condolences ? So Spring is mainly popular for annotation (since it only implements what's already existing... in a convivial manner one might say)... I come from a world where we code. The when we are stuck we look for frameworks... I was reading an advanced Spring documentation then I started to ask myself why I did not pay more attention to my Software engineering class subjects like design patterns... – Jason Krs Aug 08 '17 at 16:26
  • @JasonKrs Frameworks are wildly propular but they come at a cost. When they move into the language space and show up in your business rule code stop imagining that you're programming in well known Java. You might as well switch to one of the new JVM languages at this point. Dependency Injection is a good thing to learn. It's not a good thing to think you can only do with some framework. It's just making you do something you could have done anyway. – candied_orange Aug 08 '17 at 16:31
  • 1
    *A pattern having a name doesn't make it a good thing. It makes it a recognizable thing* Thanks for that https://softwareengineering.stackexchange.com/questions/329728/are-design-patterns-frowned-upon/329731#329731 – Jason Krs Aug 08 '17 at 16:45
  • @JasonKrs: `I started to ask myself why I did not pay more attention to... design patterns` -- You didn't miss much. – Robert Harvey Aug 08 '17 at 18:34
  • Heh, well they build a vocabulary that lets you talk about some code ideas with a short hand. But if you're the only person in your shop that knows them all they do is help you read some books. You still have to figure out how to talk to your team. Use what they know. Not what you know. – candied_orange Aug 08 '17 at 18:41
  • `when we are stuck we look for frameworks` When I'm stuck I look for solutions I can put in a box and forget about. Sometimes I can do that to frameworks by treating them like libraries. I just have to be willing to use them in ways their authors hoped I wouldn't figure out. [I'm not the only one](https://8thlight.com/blog/uncle-bob/2014/05/11/FrameworkBound.html) – candied_orange Aug 08 '17 at 19:33
  • We are missing something here. Spring as any other framework want you to focus on your business. It makes you quite more productive than without it. Spring will implement all the patterns you can imagine (and more) for you. That's as good or as bad as could be learning Latin before/after learning Spanish. Is matter of needs and preferences. However, being familiar and aware of the underlaying best practices that Spring brings to you, make you better developer. Or at least more skillful than the average. – Laiv Aug 08 '17 at 20:22
  • But Spring, per se, is a bless for people who also likes to build things without having to care too much about the details. Not everybody likes the software engineery as we like. Otherwise, SE would be way more crowded ;-p. – Laiv Aug 08 '17 at 20:23
  • @Laiv hahaha ;-) – Jason Krs Aug 08 '17 at 20:49
  • @CandiedOrange I read [I'm not the only one](https://8thlight.com/blog/uncle-bob/2014/05/11/FrameworkBound.html). The thing is many programmers (like me) get introduced to coding with very a little language syntax (declare a var, loop, declare a class, instantiate...) and after that they jump in the framework world. A good old fashion Java programmer would know how to keep a Java framework at *arm's length* but an amateur would not an will probably surrender. I don't think a professional woudl surrender that easily. – Jason Krs Aug 08 '17 at 20:55
  • 1
    @CandiedOrange - I know you don't *like* Spring, so maybe you're not keeping up with the latest developments, but these days it encourages writing your configuration in Java code, not XML. – Jules Aug 08 '17 at 22:25
  • @Jules by "in java code" do you mean annotations? Because I already covered that. I like Spring just fine so long as I can rip it out whenever I like. – candied_orange Aug 08 '17 at 23:20
  • No, I really do mean in Java code -- see https://www.mkyong.com/spring3/spring-3-javaconfig-example/ for an example. – Jules Aug 08 '17 at 23:28
  • @Jules I have to admit that looks like a clean pojo to me. – candied_orange Aug 09 '17 at 01:46