Basics of Façade Patterns for iOS App Development

The Façade pattern provides a single, simplified interface for using the functionality provided for iOS app development. Façades are often used for simplifying access to the facilities of a subsystem. The next set of code uses the Façade pattern to draw a pie chart utilizing Circle, Line, and Point classes to represent the pies.

The code begins with the following interface file of the PieChart class. The initializer initWithViewAndSegments sets the enclosing view of the pie chart, the center coordinates, the radius, the number of slices, and the proportion of each slice. There's also a draw method that draws the pie chart inside its enclosing view.

#import <Foundation/Foundation.h>
@interface PieChart : NSObject{
 // Member variables
 @private (UIView *) enclosingView;
 @private int radius;
 @private int numberOfSlices;
 @private float[] pieProportions;
}
 -(id) initWithViewAndSegments :(UIView *) enclosingView
         :(Point *) center
         :(int) radius
         :(int) numberOfSlices
         :float[] pieProportions;
 -(void) draw;
 // Other operations
–d

You can see how easy it is for a client program of the PieChart class to draw a pie chart. All the program has to do is initialize the chart using initWithViewAndSegments and then call draw.

Now compare the preceding use of a Façade to the implementation of the draw method (pseudocode shown here), which is what someone directly using Circle and Line objects to draw the pie chart would have to do:

@implementation PieChart
 …
 -(void) draw{
  Circle *pieChartCircle =
   [[Circle alloc] initWithViewRadiusAndOrigin :enclosingView
              :radius :center];
    pieChartCircle.draw; // draw the circle
    for (int k = 0; k <= numberOfPies-1; k++){ // for each pie
     Point *endPoint; // end point of radius that bounds a pie
     float slopeOfLine;
     // Calculate slopeOfLine from pieProportions[k].
     // 100% => 360 degrees
     …
     // Calculate endPoint of radial line from origin, slope
     // and length (= radius)
     …
     // Draw the line
     Line *radialLine =
      [[Line alloc] initWithCoordinates :enclosingView
              :center
              :endPoint];
     oneLine.draw;
    }
 }
 …

To be thorough, we show the interfaces for Circle, Line, and Point here:

@interface Circle : NSObject{
 // Member variables
 @private UIView *enclosingView;
 @private int radius;
 @private int centerX;
 @private int centerY;
}
 -(id) initWithViewRadiusAndOrigin :(UIView *) enclosingView
               :(int) radius
               :(Point *) center;
 -(void) draw;
 // Other operations
–d
@interface Line : NSObject{
 // Member variables
 @private Point *start, *end
}
 -(id) initWithCoordinates :(UIView *) enclosingView :(Point) *start
              :(Point) *end;
 -(void) draw;
 // Other operations
–d
@interface Point : NSObject{
 // Member variables
 @private int x, y;
}
 -(id) initWithCoordinates :(int) x :(int) y;
–d

In regard to OO techniques and principles, you can clearly see that Façade uses delegation extensively. Also, if a Façade is well designed, it illustrates the OO principles of information hiding, high cohesion, and low coupling very well.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com