Skip to main content

Building object with arbitrary functionalities [Resolved]

I am trying to look for the right design pattern for the below scenario.

I am trying to create an object/binary with different modules, a module is a functionality that I want to provide to that object. Examples of a module are like reading a file, writing a file, sending some requests to the server, etc...

Let say I have the following functions

func readFiles(path) {
  /* */ 

func writeFiles(Content, Path) {
  /* */

func playMusic(path) {
    /*  */ 

func playVideo(path) {
    /* */

The object/client program will usually have the following feature by taking input from the user

switch input {
    case "playmusic":
       call playmusic('path') 
    case "playvideo":
       call playvideo('path') 
    case "readfile":
       call readfile('path') 
    case "writefile":
       call writefile('path') 
       /* Cannot understand your input */ 

The above approach works fine if I want to include all the functions in the object, and I will generate a binary that contains all the functions.

But for now, I want to provide a greater granularity when generating the binary. I want the user to be able to choose the function they want to include in the binary and hence the unused functions will not be included in the binary. What design pattern will be suitable for that?

I have thought about decorator pattern, but that is not exactly right since I'm not decorating a function, they are all separate functions.

I have checked the strategy pattern, but that is not exactly right as well since it will still include all the functions but just changing the strategy at runtime.

Question Credit: Timothy Leung
Question Reference
Asked September 13, 2018
Posted Under: Programming
1 Answers

Your Answer