Last active
October 4, 2015 02:07
-
-
Save hidayat365/2559703 to your computer and use it in GitHub Desktop.
Hello World OOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* contoh interface | |
*/ | |
interface Printer | |
{ | |
public function printOut($message); | |
} | |
/* | |
* class Message | |
* berisi pesan string yang ingin ditampilkan | |
*/ | |
class Message | |
{ | |
private $message; | |
public function __construct($message) { | |
$this->message = $message; | |
} | |
public function printOut($printer) { | |
$printer->printOut($this->toString()); | |
} | |
public function toString() { | |
return $this->message; | |
} | |
} | |
/* | |
* contoh class abstract | |
* harus dibuat object inheritance kalau mau pake | |
*/ | |
abstract class AbstractPrinterFactory | |
{ | |
public static function getFactory(){ | |
return new SystemOutPrinterFactory(); | |
} | |
public abstract function getPrinter(); | |
} | |
/* | |
* contoh inheritance dari class abstract | |
*/ | |
class SystemOutPrinterFactory extends AbstractPrinterFactory | |
{ | |
public function getPrinter() { | |
return new SystemOutPrinter(); | |
} | |
} | |
/* | |
* contoh implementasi interface | |
*/ | |
class SystemOutPrinter implements Printer | |
{ | |
public function printOut($message) { | |
print $message; | |
} | |
} | |
/* | |
* program utama :) | |
*/ | |
class HelloWorld | |
{ | |
public static function run($arg) | |
{ | |
$message = new Message($arg); | |
$factory = AbstractPrinterFactory::getFactory(); | |
$printer = $factory->getPrinter(); | |
$message->printOut($printer); | |
} | |
} | |
/* | |
* Sekarang jalankan aplikasi | |
*/ | |
HelloWorld::run("Hello, World!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the most amazing "hello world" syntax :o