1. 程式人生 > >移動開發之設計模式- 資料訪問物件模式(IOS&Android)

移動開發之設計模式- 資料訪問物件模式(IOS&Android)

資源

完全參照資料訪問物件模式|菜鳥教程但不包括IOS程式碼

資料訪問物件模式

資料訪問物件模式(Data Access Object Pattern)或 DAO 模式用於把低階的資料訪問 API 或操作從高階的業務服務中分離出來。以下是資料訪問物件模式的參與者。

  • 資料訪問物件介面(Data Access Object Interface) - 該介面定義了在一個模型物件上要執行的標準操作。
  • 資料訪問物件實體類(Data Access Object concrete class) - 該類實現了上述的介面。該類負責從資料來源獲取資料,資料來源可以是資料庫,也可以是 xml,或者是其他的儲存機制。
  • 模型物件/數值物件(Model Object/Value Object) - 該物件是簡單的 POJO,包含了 get/set 方法來儲存通過使用 DAO 類檢索到的資料。

在這裡插入圖片描述

Android

Student

public class Student {
   private String name;
   private int rollNo;
 
   Student(String name, int rollNo){
      this.name = name;
      this.rollNo = rollNo;
   }
 
   public String getName() {
      return name;
   }
 
   public void setName(String name) {
      this.name = name;
   }
 
   public int getRollNo() {
      return rollNo;
   }
 
   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }
}

StudentDao.java

import java.util.List;
 
public interface StudentDao {
   public List<Student> getAllStudents();
   public Student getStudent(int rollNo);
   public void updateStudent(Student student);
   public void deleteStudent(Student student);
}

StudentDaoImpl

import java.util.ArrayList;
import java.util.List;
 
public class StudentDaoImpl implements StudentDao {
   
   //列表是當作一個數據庫
   List<Student> students;
 
   public StudentDaoImpl(){
      students = new ArrayList<Student>();
      Student student1 = new Student("Robert",0);
      Student student2 = new Student("John",1);
      students.add(student1);
      students.add(student2);    
   }
   @Override
   public void deleteStudent(Student student) {
      students.remove(student.getRollNo());
      System.out.println("Student: Roll No " + student.getRollNo() 
         +", deleted from database");
   }
 
   //從資料庫中檢索學生名單
   @Override
   public List<Student> getAllStudents() {
      return students;
   }
 
   @Override
   public Student getStudent(int rollNo) {
      return students.get(rollNo);
   }
 
   @Override
   public void updateStudent(Student student) {
      students.get(student.getRollNo()).setName(student.getName());
      System.out.println("Student: Roll No " + student.getRollNo() 
         +", updated in the database");
   }
}

DaoPatternDemo

public class DaoPatternDemo {
   public static void main(String[] args) {
      StudentDao studentDao = new StudentDaoImpl();
 
      //輸出所有的學生
      for (Student student : studentDao.getAllStudents()) {
         System.out.println("Student: [RollNo : "
            +student.getRollNo()+", Name : "+student.getName()+" ]");
      }
 
 
      //更新學生
      Student student =studentDao.getAllStudents().get(0);
      student.setName("Michael");
      studentDao.updateStudent(student);
 
      //獲取學生
      studentDao.getStudent(0);
      System.out.println("Student: [RollNo : "
         +student.getRollNo()+", Name : "+student.getName()+" ]");      
   }
}

結果

Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]

IOS

Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic, strong) NSString* name;
@property (nonatomic, assign) int rollNo;
- (instancetype)initWithName:(NSString*)name rollNo:(int)rollNo;
@end

Student.m

#import "Student.h"
@interface Student()
@end

@implementation Student
- (instancetype)initWithName:(NSString*)name rollNo:(int)rollNo
{
    self = [super init];
    if (self) {
        self.name = name;
        self.rollNo = rollNo;
    }
    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"Student: [RollNo : %d , Name : %@]", self.rollNo, self.name];
}
@end

StudentDaoImpl.h

#import <Foundation/Foundation.h>
#import "Student.h"


@protocol StudentDao <NSObject>
-(NSArray*)getAllStudents;
-(Student*)getStudent:(int)rollNo;
-(void)updateStudent:(Student*)student;
-(void)deleteStudent:(Student*)student;
@end

@interface StudentDaoImpl : NSObject <StudentDao>

@end

StudentDaoImpl.m

#import "StudentDaoImpl.h"

@interface StudentDaoImpl ()
@property (nonatomic, strong) NSMutableArray *students;
@end

@implementation StudentDaoImpl

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.students = [[NSMutableArray alloc]init];
        Student *student1 = [[Student alloc] initWithName:@"Robert" rollNo:0];
        Student *student2 = [[Student alloc] initWithName:@"John" rollNo:1];
        [self.students addObject:student1];
        [self.students addObject:student2];
    }
    return self;
}

- (void)deleteStudent:(Student *)student {
    [self.students removeObject:student];
    NSLog(@"Student: roll No %d , deleted from database.", student.rollNo);
}

- (NSArray *)getAllStudents {
    //self.students removeObject:
    return self.students;
}

- (Student *)getStudent:(int)rollNo {
    if(rollNo <= [self.students count]) {
        return [self.students objectAtIndex:rollNo];
    }
    return nil;
}

- (void)updateStudent:(Student *)student {
    [[self.students objectAtIndex:student.rollNo] setName:student.name];
    NSLog(@"Student: Roll No %d ,updated in the database.", student.rollNo);
}

@end

ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    id<StudentDao> studentDao = StudentDaoImpl.new;
    
    //輸出所有的學生
    for (Student *student in [studentDao getAllStudents]) {
        NSLog(@"%@", [student description]);
    }

    //更新學生
    Student *student = [[studentDao getAllStudents] objectAtIndex:0];
    [student setName:@"Michael"];
    [studentDao updateStudent:student];
    
    //獲取學生
    NSLog(@"%@", [[studentDao getStudent:0] description]);
}

結果

 Student: [RollNo : 0 , Name : Robert]
 Student: [RollNo : 1 , Name : John]
 Student: Roll No 0 ,updated in the database.
 Student: [RollNo : 0 , Name : Michael]