1. 程式人生 > >移動開發之設計模式- 外觀模式(IOS&Android)

移動開發之設計模式- 外觀模式(IOS&Android)

資源

完全參照 外觀模式|菜鳥教程 ,但不包括IOS程式碼

外觀模式

外觀模式(Facade Pattern)隱藏系統的複雜性,並向客戶端提供了一個客戶端可以訪問系統的介面。這種型別的設計模式屬於結構型模式,它向現有的系統新增一個介面,來隱藏系統的複雜性。
這種模式涉及到一個單一的類,該類提供了客戶端請求的簡化方法和對現有系統類方法的委託呼叫。

介紹

意圖: 為子系統中的一組介面提供一個一致的介面,外觀模式定義了一個高層介面,這個介面使得這一子系統更加容易使用。
主要解決: 降低訪問複雜系統的內部子系統時的複雜度,簡化客戶端與之的介面。
何時使用:
1、客戶端不需要知道系統內部的複雜聯絡,整個系統只需提供一個"接待員"即可。
2、定義系統的入口。

如何解決: 客戶端不與系統耦合,外觀類與系統耦合。
關鍵程式碼:在客戶端和複雜系統之間再加一層,這一層將呼叫順序、依賴關係等處理好。
應用例項:
1、去醫院看病,可能要去掛號、門診、劃價、取藥,讓患者或患者家屬覺得很複雜,如果有提供接待人員,只讓接待人員來處理,就很方便。
2、JAVA 的三層開發模式。

優點:
1、減少系統相互依賴。
2、提高靈活性。
3、提高了安全性。

缺點: 不符合開閉原則,如果要改東西很麻煩,繼承重寫都不合適。
使用場景:
1、為複雜的模組或子系統提供外界訪問的模組。
2、子系統相對獨立。
3、預防低水平人員帶來的風險。

注意事項:在層次化結構中,可以使用外觀模式定義系統中每一層的入口。

在這裡插入圖片描述

#Android

Shape.java

public interface Shape {
   void draw();
}

Rectangle.java

public class Rectangle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Rectangle::draw()");
   }
}

Square.java

public class Square implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Square::draw()");
   }
}

Circle.java

public class Circle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Circle::draw()");
   }
}

ShapeMaker.java

public class ShapeMaker {
   private Shape circle;
   private Shape rectangle;
   private Shape square;
 
   public ShapeMaker() {
      circle = new Circle();
      rectangle = new Rectangle();
      square = new Square();
   }
 
   public void drawCircle(){
      circle.draw();
   }
   public void drawRectangle(){
      rectangle.draw();
   }
   public void drawSquare(){
      square.draw();
   }
}

FacadePatternDemo.java

public class FacadePatternDemo {
   public static void main(String[] args) {
      ShapeMaker shapeMaker = new ShapeMaker();
 
      shapeMaker.drawCircle();
      shapeMaker.drawRectangle();
      shapeMaker.drawSquare();      
   }
}

結果

Circle::draw()
Rectangle::draw()
Square::draw()

IOS

Shape.h

#import <Foundation/Foundation.h>

@protocol Shape <NSObject>
-(void)draw;
@end

@interface Rectangle:NSObject<Shape>
@end

@interface Square:NSObject<Shape>
@end

@interface Circle:NSObject<Shape>
@end

Shape.m

#import "Shape.h"

@implementation Rectangle

- (void)draw {
    NSLog(@"Rectangle::draw()");
}

@end

@implementation Square

- (void)draw {
    NSLog(@"Square::draw()");
}

@end

@implementation Circle

- (void)draw {
    NSLog(@"Circle::draw()");
}

@end

ShapeMaker.h

#import <Foundation/Foundation.h>

@interface ShapeMaker : NSObject
-(void)drawCircle;
-(void)drawRectangle;
-(void)drawSquare;
@end

ShapeMaker.m

#import "ShapeMaker.h"
#import "Shape.h"
@interface ShapeMaker()
@property (nonatomic, strong) id<Shape> circle;
@property (nonatomic, strong) id<Shape> rectangle;
@property (nonatomic, strong) id<Shape> square;
@end
@implementation ShapeMaker

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.circle = Circle.new;
        self.rectangle = Rectangle.new;
        self.square = Square.new;
    }
    return self;
}

- (void)drawCircle{
    [self.circle draw];
}

- (void)drawRectangle{
    [self.rectangle draw];
}

- (void)drawSquare{
    [self.square draw];
}
@end

ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    ShapeMaker *shapeMaker = ShapeMaker.new;
    [shapeMaker drawCircle];
    [shapeMaker drawRectangle];
    [shapeMaker drawSquare];
}

結果

 Circle::draw()
 Rectangle::draw()
 Square::draw()