1

I have open source code,my target was to add some functionality to this code without breaking the opensource structure. For example

class OpensourceClass{
       String getValue(){
         return "";
       }
   }

so I created a interface int class and a instance of that interface is introduced in opensource class

 class OpensourceClass{
       public static interface userImpl{
          String getValue();
       }

        private static userImpl obj;

      public static  void setUserImpl(userImpl ob){
          obj=ob;
      }
       String getValue(){
           if(userImpl)
              return userImpl.getValue();
             return "";
      }
    }

Just want to know which design pattern this.. Is it a strategy pattern.

dead programmer
  • 4,223
  • 9
  • 46
  • 77

2 Answers2

1

Yes. It is Strategy pattern.

shruti1810
  • 3,920
  • 2
  • 16
  • 28
1

This is the Strategy (Policy) design pattern. Depending on the context it can also be called the State pattern. These two patterns are pretty similar, you can check this question for more details: What is the difference between Strategy Design pattern and State Design pattern?

A more broader term is Inversion of control. Using strategy design pattern is one of the basic techniques to implement inversion of control.

Also by trying to make your open-source class extensible you follow the Open/closed principle. As stated in this article the Strategy pattern is one of the ways to achieve Open/Closed.

Community
  • 1
  • 1
medvedev1088
  • 3,645
  • 24
  • 42