0

I have a class called PhotoManager it's a singleton. This class contains methods like:

fun downloadPhoto(context: Context, photoUrl: String) { }

fun savePhotoUri(context: Context, uri: Uri) { } 

fun setWallpaper(context: Context, photoUri: Uri) { }

I also have some other classes that have Manager postfix in their names. For example, SuggestionManager that receives some input data and returns user-based suggestions.

I'd like to refactor these two classes but I don't know which pattern to use. Can it be the Facade pattern?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Serhii K.
  • 639
  • 1
  • 8
  • 17

3 Answers3

1

If you temped to call your classes Managers and often use singletons, in most cases, it would mean just lack of understanding design patterns and what problem they solve.

The similar questions have already been asked many times and I just feel that at this stage, you can get inspired by some topics below.

Naming Classes - How to avoid calling everything a "<WhatEver>Manager"?

How to avoid …Helper or …Manager classes

The Clean Code Talks - "Global State and Singletons"

Vladimir
  • 2,461
  • 2
  • 14
  • 18
1

Why does PhotoManager exist? Does PhotoManager have any state? If not, then just get rid of it. In Kotlin you can have functions outside of classes.

If it does have state, then figure out why you need the class, and that will help you figure out better name.

"PhotoManager" is a bad name, because nobody thinks "I need something to manage my photos"... and if someone did think that, then your class doesn't sound like it would do the job. As a consumer of your class, what is it to me?

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • PhotoManager doesn't have any state. It just contains util functions like downloadPhoto, setWallpaper all these functions are independent. I can't use a ViewModel for example, because I need these functions in multiple places and multiple ViewModels are using this class. – Serhii K. Mar 01 '20 at 09:55
  • 1
    Then just don't have a class. Make top-level functions like `downloadPhoto` in a file called `Photos.kt` or similar. From the signatures, it looks like you might want to make them extensions of `Context`, so you can write `theContext.downloadPhoto(theUri)`, for example – Matt Timmermans Mar 01 '20 at 13:39
1

When dealing with images from a remote source, I would suggest following the Repository pattern. It will give you much more flexibility and will be easier to extend and maintain. For Android, the pattern is described by Google in the Guide to app architecture. There are plenty of technologies to choose from for the implementation so do some research before you start coding.

That should cover the first two methods. As for the others, I don't think I understand what they do well enough to suggest a solution.

Sir Codesalot
  • 7,045
  • 2
  • 50
  • 56