Skip to content

Instantly share code, notes, and snippets.

@omayib
Last active May 21, 2020 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omayib/0f9fed4bda6e76a7b94185a7f9c79b73 to your computer and use it in GitHub Desktop.
Save omayib/0f9fed4bda6e76a7b94185a7f9c79b73 to your computer and use it in GitHub Desktop.
Sharing ngabuburit Ramadhan 2020 bersama Yogya Android Community dan Android Amikom Developer.
// JUMP TO LINE CODE 214 TO SEE THE MAIN APPS
//=============== CASE 1 ===============
data class Nasabah(var name:String, var alamatTinggal:String,
var alamatKtp:String, var tglLahir:String, var tempatLahir:String,
var umur:String, var jmlAnak:String, var statusNikah:String="menikah",
var statusRumah:String, var nmrKtp:String, var namaIbuKandung:String,
var namaAliasIbuKandung:String, var namaPasangan:String="", var statusPasangan:String="menikah")
//=====================================
// CASE 2
//=====================================
class QuestionForm () {
var listQuestion:ArrayList<Question> = ArrayList()
fun addQuestion(question: Question){
this.listQuestion.add(question)
}
fun showQuestion() {
listQuestion.forEach {
it.printQuestion()
}
}
}
//====================================
//========== CASE 3 ================
interface CallEngine{
fun setup()
fun dial()
fun hangup()
}
class GoogleCallEngine:CallEngine{
override fun setup() {
println("setup from google call engine")
}
override fun dial() {
println("dial from google call engine")
}
override fun hangup() {
println("hangup from google call engine")
}
}
class WhatsappCallEngine:CallEngine{
override fun setup() {
println("setup from whatsaapp call engine")
}
override fun dial() {
println("dial from whatsaapp call engine")
}
override fun hangup() {
println("hangup from whatsaapp call engine")
}
}
class SDKCallEngine{
lateinit var callEngine:CallEngine
fun addCallEngine(engine: CallEngine){
this.callEngine = engine
}
fun makeACall(){
this.callEngine.setup()
this.callEngine.dial()
}
fun closeThePhone(){
this.callEngine.hangup()
}
}
class QuestionEssay(var title:String):Question{
var listQuestion:ArrayList<Question> = ArrayList()
override fun printQuestion() {
println("question (title='$title') and has sub ${listQuestion.size}")
if(!listQuestion.isEmpty()){
listQuestion.forEach {
it.printQuestion()
}
}
}
override fun addSubQuestion(objects: Question) {
this.listQuestion.add(objects)
}
}
class QuestionOptional(var title:String):Question{
var listQuestion:ArrayList<Question> = ArrayList()
override fun printQuestion() {
println("question (title='$title') and has sub ${listQuestion.size}")
if(!listQuestion.isEmpty()){
listQuestion.forEach {
it.printQuestion()
}
}
}
override fun addSubQuestion(objects: Question) {
this.listQuestion.add(objects)
}
}
interface Question{
fun printQuestion()
fun addSubQuestion(objects: Question)
}
//=================================
// CASE 4
//=================================
interface ConnectionService{
fun connecting()
fun onConnected()
fun onDisconnected()
fun disconnecting()
fun addDownloaderService(downloaderService: DownloaderService)
}
interface DownloaderService{
fun download()
fun resume()
fun cancel()
fun addConnectionMonitor(connectionService: ConnectionService)
}
class AndroidMainApps{
lateinit var downloader:DownloaderService
lateinit var connection: ConnectionService
fun onCreated() {
connection = ConnectionMonitor()
downloader = FasterDownloader()
connection.addDownloaderService(downloader)
downloader.addConnectionMonitor(connection)
}
fun Download(){
downloader.download()
}
fun cancel(){
downloader.cancel()
}
fun resume(){
downloader.resume()
}
fun airplaneMode(){
connection.disconnecting()
}
}
class ConnectionMonitor:ConnectionService{
lateinit var downloaderService:DownloaderService
override fun connecting() {
println("connecting....")
}
override fun onConnected() {
downloaderService.resume()
}
override fun onDisconnected() {
downloaderService.cancel()
}
override fun disconnecting() {
println("disconnecting....")
downloaderService.cancel()
}
override fun addDownloaderService(downloaderService: DownloaderService) {
this.downloaderService = downloaderService
}
}
class FasterDownloader:DownloaderService{
lateinit var connection: ConnectionService
override fun download() {
this.connection.connecting()
println("donwloading in progresss....")
}
override fun resume() {
this.connection.connecting()
println("donwloading in progresss....")
}
override fun cancel() {
println("donwload cancel....")
}
override fun addConnectionMonitor(connectionService: ConnectionService) {
this.connection = connectionService
}
}
//==========================================
// THE MAIN APPS
//==========================================
fun main(args: Array<String>) {
//=============== BUILDER PATTERN ===============
var nasabah = Nasabah(
name = "joko",
alamatTinggal = "ngaglik",
alamatKtp = "sleman",
umur = "40",
jmlAnak = "3",
tglLahir = "06/04/80",
tempatLahir = "sleman",
statusRumah = "kontrak",
namaPasangan = "sri",
namaIbuKandung = "maryani",
namaAliasIbuKandung = "yani",
statusPasangan = "menikah",
nmrKtp = "3314140604800045",// mandatory, if you delete this row will effect to line code error
statusNikah = "menikah"// optional, you can delete it
)
//====== COMPOSITE PATTERN EXAMPLE ==========
val form = QuestionForm()
var q2 = QuestionEssay("sudah menikah ?")
var q3 = QuestionOptional("dimana domisilimu");
var q4 = QuestionEssay("kabupaten?");
var q5 = QuestionOptional("siapa nama ibu?");
q2.addSubQuestion(q3)
q3.addSubQuestion(q4)
form.addQuestion(q2)
form.addQuestion(q5)
form.showQuestion()
//============ STRATEGY PATTERN EXAMPLE =================
var phoneByWa = WhatsappCallEngine()
var phoneByGoogle = GoogleCallEngine()
var sdk = SDKCallEngine()
sdk.addCallEngine(phoneByWa)
sdk.makeACall()
sdk.closeThePhone()
sdk.addCallEngine(phoneByGoogle)
sdk.makeACall()
sdk.closeThePhone()
//============ Coordinator Pattern Example ==========
var androidApps = AndroidMainApps()
androidApps.onCreated()
androidApps.Download()
androidApps.cancel()
androidApps.resume()
androidApps.airplaneMode()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment