Guest User

Untitled

a guest
Oct 7th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. //---------------------------------------
  2. //--- Code tự nhiên ---------------------
  3. //---------------------------------------
  4.  
  5. class Account {
  6. username: string;
  7. password: string;
  8.  
  9. register (username: string, password: string){
  10. // Kiểm tra username và password có hợp lệ hay không rồi mới cho đăng ký
  11. // Ví dụ như username không bị trùng, password thì phải trên 8 ký tự chẳng hạn
  12. if (checkValidUsername(username) && checkValidPassword(password)) {
  13. this.username = username;
  14. this.password = password;
  15. }
  16. }
  17.  
  18. authenticate(username: string, password: string){
  19. return this.username === username && this.password === password;
  20. }
  21. }
  22.  
  23. let batman = new Account();
  24.  
  25. // Có đến 2 cách để thiết lập username, password cho người dùng.
  26. // Nếu dev hên mà dùng cách 1 thì không sao
  27. batman.register('Robinson', 'iloveu');
  28.  
  29. // Còn đen mà dùng cách này để thiết lập username password thì teo lúc nào không biết.
  30. batman.username = 'Robinson'; // Không kiểm tra được tính hợp lệ của username và password
  31. batman.password = 'iloveu';
  32.  
  33. //---------------------------------------
  34. //--- Áp dụng encapsulation vào----------
  35. //---------------------------------------
  36.  
  37. class Account {
  38. private _username: string;
  39. private _password: string;
  40.  
  41. register (username: string, password: string){
  42. // Kiểm tra username và password có hợp lệ hay không rồi mới cho đăng ký
  43. // Ví dụ như username không bị trùng, password thì phải trên 8 ký tự chẳng hạn
  44. if (checkValidUsername(username) && checkValidPassword(password)) {
  45. this._username = username;
  46. this._password = password;
  47. }
  48. }
  49. authenticate(username: string, password: string){
  50. return this._username === username && this._password === password;
  51. }
  52. }
  53. let superman = new Account();
  54. // Code chặt chẽ thế này thì chỉ làm sao mà ra bugs =)))
  55. superman.register('Robinson', 'iloveu');
Add Comment
Please, Sign In to add comment