{"id":2009,"date":"2004-04-21T10:24:47","date_gmt":"2004-04-21T13:24:47","guid":{"rendered":"http:\/\/brockerhoff.net\/bb\/viewtopic.php?p=878"},"modified":"2010-05-08T21:45:38","modified_gmt":"2010-05-09T00:45:38","slug":"hello-complicated-world","status":"publish","type":"post","link":"https:\/\/brockerhoff.net\/blog\/2004\/04\/21\/hello-complicated-world\/","title":{"rendered":"Hello, complicated world"},"content":{"rendered":"<p>Traditionally the first program one sees or writes in a new computer language is the \u201cHello, world\u201d program: a simple program that simply prints out, or sends, or extrudes, the string \u201cHello, world\u201d through the most convenient interface. For instance, in PHP this would look like:<\/p>\n<pre><code>&lt;?php echo \"Hello, world\"; ?&gt;<\/code><\/pre>\n<p>For contrast, check out this cautionary counterexample of writing a \u201cHello, world\u201d program using POSA (Pattern-Oriented Software Architecture):<\/p>\n<pre><code>&lt;?php\r\n\/********************************************************************\r\nModel-View-Controller implementation according to POSA\r\n(Pattern-Oriented Software Architecture\r\n  http:\/\/www.hillside.net\/patterns\/books\/Siemens\/book.html)\r\n********************************************************************\/\r\nclass HelloWorldController {\r\n    private $model;\r\n    function __construct($model) {\r\n        $this-&gt;model = $model;\r\n    }\r\n    function handleEvent($args) {\r\n        $this-&gt;model-&gt;setStrategy($args[2]);\r\n        $this-&gt;model-&gt;addText($args[1]);\r\n    }\r\n}\r\nclass HelloWorldModel {\r\n    private $text;\r\n    private $observers = array();\r\n    private $strategy;\r\n\r\n    function attach($observer) {\r\n        $this-&gt;observers[] = $observer;\r\n    }\r\n    function getData() {\r\n        $facade = new HelloWorldFacade($this-&gt;strategy);\r\n        return $facade-&gt;getHelloWorld().$this-&gt;text.\"\\n\";\r\n    }\r\n    function addText($text='') {\r\n        $this-&gt;text = $text;\r\n        $this-&gt;notify();\r\n    }\r\n    function setStrategy($strategy) {\r\n        $this-&gt;strategy = $strategy;\r\n    }\r\n\r\n    function notify() {\r\n        foreach ($this-&gt;observers as $observer) {\r\n            $observer-&gt;update();\r\n        }\r\n    }\r\n}\r\nclass HelloWorldView {\r\n    private $model;\r\n    function initialize($model) {\r\n        $this-&gt;model = $model;\r\n        $model-&gt;attach($this);\r\n        return $this-&gt;makeController();\r\n    }\r\n    function makeController() {\r\n        return new HelloWorldController($this-&gt;model);\r\n    }\r\n    function update() {\r\n        $this-&gt;display();\r\n    }\r\n    function display() {\r\n        echo $this-&gt;model-&gt;getData();\r\n    }\r\n}\r\n\/*********************************************************************\r\n\"Business logic\"\r\n********************************************************************\/\r\nclass HelloWorld {\r\n   function execute() {\r\n       return \"Hello world\";\r\n   }\r\n}\r\nclass HelloWorldDecorator {\r\n   private $helloworld;\r\n   function __construct($helloworld) {\r\n       $this-&gt;helloworld = $helloworld;\r\n   }\r\n   function execute() {\r\n       return $this-&gt;helloworld-&gt;execute();\r\n   }\r\n}\r\nabstract class HelloWorldEmphasisStrategy {\r\n    abstract function emphasize($string);\r\n}\r\nclass HelloWorldBangEmphasisStrategy extends HelloWorldEmphasisStrategy {\r\n    function emphasize($string) {\r\n       return $string.\"!\";\r\n    }\r\n}\r\nclass HelloWorldRepetitionEmphasisStrategy extends HelloWorldEmphasisStrategy {\r\n    function emphasize($string) {\r\n       return $string.\" and \".$string.\" again\";\r\n    }\r\n}\r\nclass HelloWorldEmphasizer extends HelloWorldDecorator {\r\n   private $strategy;\r\n   function HelloWorldEmphasizer($helloworld,$strategy) {\r\n       $this-&gt;strategy = $strategy;\r\n       parent::__construct($helloworld);\r\n   }\r\n   function execute() {\r\n       $string = parent::execute();\r\n       return $this-&gt;strategy-&gt;emphasize($string);\r\n   }\r\n}\r\nclass HelloWorldStrategyFactory {\r\n    static function make($type) {\r\n        if ($type == 'repetition') return self::makeRepetitionStrategy();\r\n        return self::makeBangStrategy();\r\n    }\r\n    static function makeBangStrategy() {\r\n        return new HelloWorldBangEmphasisStrategy;\r\n    }\r\n    static function makeRepetitionStrategy() {\r\n        return new HelloWorldRepetitionEmphasisStrategy;\r\n    }\r\n}\r\nclass HelloWorldFormatter extends HelloWorldDecorator {\r\n   function execute() {\r\n       $string = parent::execute();\r\n       return $string.\"\\n\";\r\n   }\r\n}\r\nclass HelloWorldFacade {\r\n    private $strategy;\r\n    function __construct($strategyType) {\r\n        $this-&gt;strategy = HelloWorldStrategyFactory::make($strategyType);\r\n    }\r\n    function getHelloWorld() {\r\n        $formatter = new HelloWorldFormatter(\r\n                new HelloWorldEmphasizer(\r\n                    new HelloWorld,$this-&gt;strategy));\r\n        return $formatter-&gt;execute();\r\n    }\r\n}\r\n$model = new HelloWorldModel;\r\n$view = new HelloWorldView;\r\n$controller = $view-&gt;initialize($model);\r\n$controller-&gt;handleEvent($_SERVER['argv']);\r\n?&gt;<\/code><\/pre>\n<p>I couldn\u2019t resist quoting the whole code. The sharp-eyed observer will notice that the actual string is generated at the beginning of the \u201cbusiness logic\u201d part and the rest is just handwaving. This quote from the author is also irresistible:<br \/>\n\u2026And the program works. In spite of its deadness, it executes and produces a result. You might say it\u2019s like one of those severed frog\u2019s legs that twitch when you apply current.<br \/>\nThis reminds me of a program I had occasion to look over recently; a friend wrote it as his final assignment for Java certification.<br \/>\nIt exhibited all of the same symptoms: elegant formatting; repetition of dozens of function with slight variations in names and one or two lines in the body; frequent referral to buzzwords like model, controller, strategy, and so forth; complete obscurity of actual function. Indeed, I can\u2019t recall what its ostensive purpose was, beyond making sure that the examiner understood that the writer had a thorough grasp of orthodox design patterns.<br \/>\nSpace precludes going into details now, expect a longer rant later about the subject&#8230; \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Traditionally the first program one sees or writes in a new computer language is the \u201cHello, world\u201d program: a simple program that simply prints out, or sends, or extrudes, the string \u201cHello, world\u201d through the most convenient interface. For instance, in PHP this would look like: &lt;?php echo &#8220;Hello, world&#8221;; ?&gt; For contrast, check out [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2009","post","type-post","status-publish","format-standard","hentry","category-misc"],"featured_image_src":null,"author_info":{"display_name":"Rainer Brockerhoff","author_link":"https:\/\/brockerhoff.net\/blog\/author\/rbrockerhoff\/"},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q3Zc-wp","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/2009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/comments?post=2009"}],"version-history":[{"count":0,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/2009\/revisions"}],"wp:attachment":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/media?parent=2009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/categories?post=2009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/tags?post=2009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}