Skip to content

Instantly share code, notes, and snippets.

@caligin
Created April 21, 2015 16:31
Show Gist options
  • Save caligin/15ff72c4f82416edd1fb to your computer and use it in GitHub Desktop.
Save caligin/15ff72c4f82416edd1fb to your computer and use it in GitHub Desktop.
From Class to State+Functions
// Start from a class, that is state and behaviour together
class UserProfile {
private String username;
private String password;
private Date lastLogIn;
private boolean isLoggedIn;
public UserProfile( String username,
String password,
Date lastLogin) {
this.username = username;
this.password = password;
this.lastLogin = lastLogin;
this.isLoggedIn = false;
}
public boolean tryLogin(String username, String password) {
final boolean ok = this.username.equals(username)
&& this.password.equals(password);
if(ok) {
this.isLoggedIn = true;
this.lastLogin = new Date();
}
}
public void logOut() {
this.isLoggedIn = false;
}
public String getUsername() { return this.username; }
public String getPassword() { return this.password; }
public Date getLastLogin() { return this.latLogin; }
public boolean isLoggedIn() { return this.isLoggedIn; }
public static void main(...) {
// snip...
final UserProfile requestedProfile = new UserProfile(usernameFromDb, passwordFromDb, lastPersistedLogin);
final boolean success = requestedProfile.tryLogin(providedUsername, providedPassword);
if(success) {
// do something
} else {
// or something else
}
}
}
# make 'this' (or self, in py) explicit (getters left for demonstrative purpose)
class UserProfile:
def __init__(self, username, password, lastLogin):
self.username = username;
self.password = password;
self.lastLogin = lastLogin;
self.isLoggedIn = false;
def tryLogin(self, username, password) {
ok = self.username == username and self._password == password
if ok:
self.isLoggedIn = True
self.lastLogin = datetime.now()
def logOut(self):
self.isLoggedIn = False;
def getUsername(self):
self.username
def getPassword(self):
self.password
def getLastLogin(self):
self.lastLogin
def isLoggedIn(self):
self.isLoggedIn
# snip...
requestedProfile = UserProfile(usernameFromDb, passwordFromDb, lastPersistedLogin)
success = requestedProfile.tryLogin(providedUsername, providedPassword)
if success:
# do something
else:
# or something else
%% drop class and make it a namespace instead
%% (keep login history instead of only last one
%% to show how encapsulation of a representation is still useful)
-module(user_profile).
-export([new/3]).
-export([try_login/3]).
-export([logout/1]).
-export([username/1]).
-export([password/1]).
-export([last_login/1]).
-export([is_logged_in/1]).
new(Username, Password, LastLogin) ->
{Username, {plain, Password}, [LastLogin], false}.
try_login(LoginUsername, LoginPassword, UserInfo) ->
{Username, {plain, Password}, Logins, _Logged} = UserInfo,
if
Username =:= LoginUsername andalso Password =:= LoginPassword -> {ok, {Username, {plain, Password}, [now() | Logins], true}};
true -> error
end.
log_out(UserInfo) ->
{Username, {Encryption, Password}, Logins, _Logged} = UserInfo,
{Username, {Encryption, Password}, Logins, false}.
get_username(UserInfo) ->
{Username, _EncryptionAndPassword, _Logins, _Logged} = UserInfo,
Username.
get_password(UserInfo) ->
{_Username, {plain, Password}, _Logins, _Logged} = UserInfo,
Password.
get_last_login(UserInfo) ->
{_Username, _EncryptionAndPassword, [LastLogin | OtherLogins], _Logged} = UserInfo,
LastLogin.
is_logged_in(UserInfo) ->
{Username, _EncryptionAndPassword, _Logins, Logged} = UserInfo,
Logged.
main(...) ->
% snip...
UserProfile = user_profile:new(UsernameFromDb, PasswordFromDb, LastPersistedLogin),
Success = User_profile:tryLogin(ProvidedUsername, ProvidedPassword, UserProfile),
case Success of
{ok, LoggedUserProfile} ->
% do something
;
error ->
% or something else
end.
%% use language feature awesomeness
-module(user_profile).
-export([new/3]).
-export([try_login/3]).
-export([logout/1]).
-export([username/1]).
-export([password/1]).
-export([last_login/1]).
-export([is_logged_in/1]).
new(Username, Password, LastLogin) ->
{Username, {plain, Password}, [LastLogin], false}.
try_login(Username, Password, {Username, {plain, Password}, Logins, _Logged}) ->
{ok, {Username, {plain, Password}, [now() | Logins], true}};
try_login(Username, Password, UserInfo) ->
error.
log_out({Username, {plain, Password}, Logins, _Logged}) ->
{Username, {plain, Password}, Logins, false}.
get_username({Username, _, _, _}) ->
Username.
get_password({_, {plain, Password}, _, _}) ->
Password.
get_last_login({_, _, [ LastLogin | _OtherLogins], _}) ->
LastLogin.
is_logged_in({_, _, _, IsLogged}) ->
IsLogged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment