Giter Club home page Giter Club logo

Comments (5)

HelgeStenstrom avatar HelgeStenstrom commented on May 25, 2024 2

The two main problems I see in Main are connected. Nothing should depend on Main (according to the book Clean Architecture). Global state is bad (according to many books and web sources). The public fields in Main are clearly global state. I have some ideas on how to convert them into fields of a separate class, but since there are so many public fields in Main, it takes a lot of changes to change them into private fields i a different class. Rather than trying to solve that i one go, perhaps I should demonstrate how one or a couple of public fields can be treated. Perhaps you don't like that change; then we can try some other solution.

Another source of inspiration regarding this are some articles and youtube videos by Misko Hevery. He talks about how to remove Singletons (as in the Singleton patterns, because Singletons are global state, and are bad for testing), and replace them with factories and dependency injection. I'm not sure I fully understand his ideas yet, and haven't seen a large scale implementation, so it takes some experimentation.

He distinguishes singletons from Singletons. A Singleton is an implementation of the Singleton pattern (as described by GOF in the Design Patterns book). A singleton is an object which is the only object of its class. If you do "new MySpecialClass()" only once, you have a singleton. That is totally OK.

He also wants to separate classes that creates objects from classes that do work. Factories create objects. Classes with business logic shouldn't; they should get the objects given to them. This makes the business logic easier to test.

One single factory class can create many different kinds of objects. According to Hevery, it's good to have classes/objects with the same lifetime created by the same factory.

Most of the public fields in Main seem to be created at the same time, and live as long as the application is running. They have the same lifetime. So they can be created by the same factory. The created objects can be used by other classes, passed to them as arguments. Or if an object or a constructor needs too many objects, a reference to the single instance of this factory can be passed to the method or constructor.

Have a look at what Hevery writes about singeltons: http://misko.hevery.com/?s=singleton,
for example http://misko.hevery.com/2009/03/30/collaborator-vs-the-factory/

from xr3player.

HelgeStenstrom avatar HelgeStenstrom commented on May 25, 2024 1

Some books that have helped me, and which have inspired my proposed changes:
https://www.goodreads.com/book/show/3735293-clean-code
https://www.goodreads.com/book/show/18043011-clean-architecture
https://www.goodreads.com/book/show/37646821-effective-java

from xr3player.

HelgeStenstrom avatar HelgeStenstrom commented on May 25, 2024

My changes were a step in the wrong direction. All fields that were moved are static, and represent global state. It’s still global state after the move. Instead the fields should be made member variables of a class does not exist yet.

from xr3player.

goxr3plus avatar goxr3plus commented on May 25, 2024

@HelgeStenstrom THank you for the book links :)

from xr3player.

goxr3plus avatar goxr3plus commented on May 25, 2024

I need to read your comment 5 times to understand :) Thank you for such detailed and informative comments.

I am opened to completely refractor the code. I know it's not a good practice to have all the dependencies in Main class.

from xr3player.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.