Skip to content

Commit 3102504

Browse files
committed
Pushing exercise files to repo
0 parents  commit 3102504

File tree

106 files changed

+1469
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1469
-0
lines changed

.project

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>design_patterns</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
<nature>com.aptana.editor.php.phpNature</nature>
11+
</natures>
12+
</projectDescription>

Adapter/ProductInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
interface ProductInterface {
3+
function getDescription();
4+
function getPrice();
5+
function getPicture();
6+
function sell();
7+
}

Adapter/RealWhiteRose.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
class RealWhiteRose implements TheOldRoseInterface {
4+
5+
6+
7+
public function getPriceFromDatabase() {
8+
9+
}
10+
11+
12+
public function sell() {
13+
14+
}
15+
16+
17+
public function showImage() {
18+
19+
}
20+
21+
}

Adapter/RosesToProductAdapter.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
//Consider making this an abstract class
3+
class RosesToProductAdapter implements ProductInterface {
4+
private $rose;
5+
function __construct(TheOldRoseInterface $rose) {
6+
$this->rose=$rose;
7+
}
8+
public function getDescription() {
9+
return "Nice flowers";
10+
}
11+
12+
public function getPicture() {
13+
return $this->rose->showImage();
14+
}
15+
16+
public function getPrice() {
17+
//Do any data manipulation needed
18+
return $this->rose->getPriceFromDatabase();
19+
}
20+
21+
public function sell() {
22+
return $this->rose->sell();
23+
}
24+
25+
}

Adapter/TheOldRoseInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
interface TheOldRoseInterface {
4+
function sell();
5+
function showImage();
6+
function getPriceFromDatabase();
7+
8+
}

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is my attempt at learning design patterns.
2+
3+
Feel free to look around at my various implimentations of the agile design patterns in PHP.
4+
5+
Note the modules will not actually run.

README.md~

Whitespace-only changes.

abstract_server/RedRose.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
require_once './includes.php';
3+
class RedRose {
4+
protected $sold;
5+
function sell() {
6+
7+
}
8+
9+
public function isSold() {
10+
return $this->sold;
11+
}
12+
13+
}

abstract_server/Roses.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+

abstract_server/ShopOwner.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
class ShopOwner {
3+
4+
function sell() {
5+
$redRose= new RedRose();
6+
$redRose->sell();
7+
}
8+
9+
}

abstract_server/YellowRose.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+

abstract_server/includes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
require_once './RedRose.php';
3+
require_once './YellowRose.php';
4+
require_once './ShopOwner.php';
5+
require_once './Roses.php';
6+

active_object/MultiFileUploader.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
class MultiFileUploader {
6+
private $uploader=array();
7+
function addUploader(UploaderCommand $uploader){
8+
$this->uploader[]=$uploader;
9+
}
10+
function run(){
11+
foreach ($this->uploader as $uploader) {
12+
$uploader->execute();
13+
}
14+
}
15+
}
16+

active_object/UploaderCommand.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
class UploaderCommand {
6+
function execute(){
7+
8+
}
9+
}

bridge/CreditBuyer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
class CreditBuyer {
4+
5+
function payNow(CreditPayment $payment) {
6+
if($payment->approve()){
7+
$payment->send();
8+
}
9+
}
10+
11+
}

bridge/CreditPayment.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
interface CreditPayment {
3+
function send();
4+
function approve();
5+
}
6+

bridge/CreditPaymentMethod.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
class CreditPaymentMethod extends PaymentMethod {
4+
5+
public function approve() {
6+
parent::approveImp();
7+
}
8+
9+
public function send() {
10+
parent::sendImp();
11+
}
12+
13+
}

bridge/DirectBuyer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
class DirectBuyer {
4+
5+
function payNow(DirectPayment $payment) {
6+
$payment->send();
7+
}
8+
9+
}

bridge/DirectPayment.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
interface DirectPayment {
4+
function send();
5+
}

bridge/DirectPaymentMethod.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
class DirectPaymentMethod extends PaymentMethod {
3+
4+
public function approve() {
5+
return TRUE;
6+
}
7+
8+
public function send() {
9+
parent::sendImp();
10+
}
11+
12+
}

bridge/MasterCard.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
class MasterCard implements PaymentSource {
4+
5+
public function approve() {
6+
7+
}
8+
9+
public function send() {
10+
11+
}
12+
13+
}

bridge/PaymentMethod.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
abstract class PaymentMethod implements DirectPayment, CreditPayment {
4+
5+
abstract function send();
6+
7+
abstract function approve();
8+
9+
private $paymentSource;
10+
11+
function setPaymentSource(PaymentSource $paymentSource) {
12+
$this->paymentSource = $paymentSource;
13+
}
14+
protected function sendImp() {
15+
$this->paymentSource->send();
16+
17+
}
18+
protected function approveImp() {
19+
$this->paymentSource->approve();
20+
}
21+
22+
}

bridge/PaymentSource.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
interface PaymentSource {
4+
function send();
5+
function approve();
6+
}

bridge/Visa.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
class Visa implements PaymentSource {
3+
4+
5+
6+
public function approve() {
7+
//Talk to bank and approve
8+
}
9+
10+
11+
public function send() {
12+
13+
}
14+
15+
}

command/PayPalPayment.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
class PaypalPayment implements PaymentMethod {
6+
7+
function execute(){
8+
9+
}
10+
}

command/PaymentMethod.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
interface PaymentMethod{
4+
function execute();
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
class PaymentProcessingException extends Exception {
6+
7+
8+
}

command/ProcessPayment.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
class ProcessPayment {
6+
7+
function processUserPayment($userName){
8+
$user= new User($userName);
9+
$paymentMethod=$user->getPaymentMethod();
10+
$this->executePayment($paymentMethod);
11+
}
12+
private function executePayment(PaymentMethod $paymentMethod){
13+
try{
14+
$paymentMethod->execute();
15+
}
16+
catch(Exception $e){
17+
throw new PaymentProcessingException('paying with' . $paymentMethod . 'failed');
18+
}
19+
}
20+
}

command/User.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
class User {
6+
private $paymentMethod;
7+
8+
public function getPaymentMethod($value='')
9+
{
10+
//VISA or Paypal
11+
return $this->paymentMethod;
12+
}
13+
}

command/VisaPayment.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
class VisaPayment implements PaymentMethod {
6+
7+
function execute(){
8+
return ;
9+
}
10+
}

composite/CompositeOrder.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
/**
10+
*
11+
* @author jacob
12+
*/
13+
class CompositeOrder implements Order {
14+
15+
private $orders = array();
16+
17+
function place() {
18+
foreach ($this->orders as $order) {
19+
$order->place();
20+
}
21+
}
22+
23+
function add(Order $order) {
24+
$this->orders[]=$order;
25+
}
26+
27+
}

0 commit comments

Comments
 (0)