简体   繁体   中英

Does facade class in facade pattern need to be static?

I was going through MSDN article about facade pattern. https://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx#Anchor_0

I couldnt find reason why they made facade a static class ? Why they used static variables to hold instances of subsystem classes ? Why did they use static methods ? I would like to know reason behind it.

Below is the static code section from that article. Full code can be found inside article.

public static class Facade 
{
     static SubsystemA a = new SubsystemA(  );
     static SubsystemB b = new SubsystemB(  );
     static SubsystemC c = new SubsystemC(  );

     public static void Operation1(  ) {
       Console.WriteLine("Operation 1\n" +
           a.A1(  ) +
           a.A2(  ) +
           b.B1(  ));
    }

     public static void Operation2(  ) {
       Console.WriteLine("Operation 2\n" +
           b.B1(  ) +
           c.C1(  ));
     }
   }

There is no requirement for a facade to be static. It just happens that in this example it is. A facade is simply a pattern. You could easily have a regular class that is still a facade. The requirement is that it provides simplified access to a more complex set of classes and methods. Instead of creating an instance of 3 or so different classes that need to have a bunch of set-up and would normally require several method calls to get the desired end output, the facade class does all of that for the programmer, providing a simplified single class references and a single (or drastically fewer) function calls.

Usually a facade will make assumptions with data that would normally require multiple steps to retrieve. This could be done through an algorithm that looks at previously used input parameters, or simply uses some pre-defined default data.

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