简体   繁体   中英

Create Singleton using Strategy Design Pattern - Android

I know about Singleton Design Pattern , Strategy Design Pattern and Composite Design Pattern and the usage of these patterns.

What I want is that I want an only method (might be static) that is responsible for creating objects that accepts the class and return the Object of that class (will return singleton object if already created)

For instance,

class AuthRepository
class SettingRepository
class PaymentRepository
....

How can I create singleton of these classes using Strategy Design Pattern?

I feel bad you are receiving a bunch of downvotes. From your reputation, you are probably a newbie and are just throwing out a bunch of technical terms you have heard without actually knowing what they are. Let me try to give you a quick tutorial, and then perhaps you will be able to better ask your question.

What Is the Singleton Pattern? The Singleton Pattern is a software design pattern that guarantees a class has one instance only and a global point of access to it is provided by that class. Anytime multiple classes or clients request for that class, they get the same instance of the class. This Singleton class may be responsible for instantiating itself, or you can delegate the object creation to a factory class.

Simple example using one of your classes

public class PaymentRepository {

    private static PaymentRepository INSTANCE = null;

    // other instance variables can be here

    private PaymentRepository() {};

    public static PaymentRepository getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new PaymentRepository();
        }
        return(INSTANCE);
    }

    // other instance methods can follow 
}

A couple notes, notice that the constructor is non-static and private, while the getInstance() method is static and public.

Regarding Strategy pattern, this is used when a class behavior or its algorithm can be changed at runtime.

What Is the Strategy Pattern? In Strategy pattern, we create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object.

Start by creating an interface

public interface OperationStrategy {
   public int doOperation(int num1, int num2);
}

Create concrete classes implementing that interface

public class OperationAddition implements OperationStrategy {
   @Override
   public int doOperation(int num1, int num2) {
      return num1 + num2;
   }
}

Create Context class

public class Context {
   private OperationStrategy strategy;

   public Context(OperationStrategy strategy){
      this.strategy = strategy;
   }

   public int executeStrategy(int num1, int num2){
      return strategy.doOperation(num1, num2);
   }
}

Use the Context to see change in behaviour when it changes its Strategy.

public class StrategyPatternDemo {
   public static void main(String[] args) {
      Context context = new Context(new OperationAddition());       
      System.out.println("10 + 5 = " + context.executeStrategy(10, 5));
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM