zoukankan      html  css  js  c++  java
  • PHP面向对象:接口实例

    PHP面向对象:接口实例

    2010-07-15

    转自http://www.nowamagic.net/php/php_InterfaceInstance.php

    我们设计一个在线销售系统,用户部分设计如下:

    将用户分为,NormalUser, VipUser, InnerUser三种。

    要求根据用户的不同折扣计算用户购买产品的价格。

    并要求为以后扩展和维护预留空间。

    用户部分先声明了一个接口User,用户都是User的实现。

    User.php

    User.php
    1 <?
    2  /*
    3 * 定义了 User接口.
    4 * 和子类 NormalUser,VipUser,InnerUser
    5  */
    6  //User接口,定义了三个抽象方法.
    7  interface User{
    8 public function getName();
    9 public function setName($_name);
    10 public function getDiscount();
    11 }
    12 abstract class AbstractUser implements User{
    13 private $name = ""; //名字
    14 protected $discount = 0; //折扣
    15 protected $grade = ""; //级别
    16
    17 public function __construct($_name){
    18 $this->setName($_name);
    19 }
    20 public function getName(){
    21 return $this->name;
    22 }
    23 public function setName($_name){
    24 $this->name = $_name;
    25 }
    26 public function getDiscount(){
    27 return $this->discount;
    28 }
    29
    30 public function getGrade(){
    31 return $this->grade;
    32 }
    33 }
    34 class NormalUser extends AbstractUser {
    35 protected $discount = 1.0;
    36 protected $grade = "NormalUser";
    37 }
    38
    39 class VipUser extends AbstractUser {
    40 protected $discount = 0.8;
    41 protected $grade = "VipUser";
    42 }
    43
    44 class InnerUser extends AbstractUser {
    45 protected $discount = 0.7;
    46 protected $grade = "InnerUser";
    47 }
    48 ?>

    关于产品,我们进行了如下设计。

    声明一个接口Product,然后从Product继承下Book接口。

    在线销售的图书最后是实现了Book接口的BookOnline类。

    Product.php

    Product.php
    1 <?
    2 /*与产品相关的类放.*/
    3 Interface Product{ //定义产品接口
    4 public function getProductName();
    5 public function getProductPrice();
    6 }
    7
    8 interface Book extends Product { // book是产品的一个分类
    9 public function getAuthor();
    10 }
    11
    12 class BookOnline implements Book{ // 定义book类.
    13 private $productName; // 产品名
    14 private $productPrice; // 产品价格
    15 private $author; //作者
    16
    17 public function __construct($_bookName){
    18 $this->productName = $_bookName;
    19 //这里放置相关初始化的代码.
    20 //与数据库关联的代码.
    21 }
    22
    23 public function getProductName(){
    24 return $this->productName;
    25 }
    26
    27 public function getProductPrice(){
    28 //这里从数据库读取价格.
    29 //假设价格是 100元.
    30 $this->productPrice = 100;
    31 return $this->productPrice;
    32 }
    33
    34 public function getAuthor(){
    35 //从数据库里面取值.
    36 return $this->author;
    37 }
    38 }
    39 ?>

    关于结算,我们使用了独立的结算类,使用静态方法做计算。产品结算。注意参数类型。

    ProductSettle.php

    ProductSettle.php
    1 <?
    2 include_once("User.php");
    3 include_once("Product.php");
    4 //买了产品到底多少钱呢?
    5 class ProductSettle{
    6 public static function finalPrice(User $_user,Product $_product,$number = 1){
    7 $price = $_user->getDiscount() * $_product->getProductPrice() * $number;
    8 return $price;
    9 }
    10 }
    11 ?>

    下面的例子是实现。大家可以自己分析下。

    View Code
    1 <?
    2 include_once("./class/User.php");
    3 include_once("./class/Product.php");
    4 include_once("./class/ProductSettle.php");
    5
    6 $number = 10;
    7 $book = new BookOnline("设计模式");
    8
    9
    10 $user = new NormalUser("Tom");
    11 $price = ProductSettle::finalPrice($user,$book,$number);
    12 $str = "您好,尊敬的用户 " . $user->getName() . " <br>";
    13 $str .= "您的级别是 ". $user->getGrade() .", <br>";
    14 $str .= "您的折扣是 " . $user->getDiscount() . "<br>";
    15 $str .= "购买 $number 本 《 ". $book->getProductName() ;
    16 $str .= "》的价格是 $price <br><br>";
    17 echo $str;
    18
    19
    20 $user = new vipUser("Tom");
    21 $price = ProductSettle::finalPrice($user,$book,$number);
    22 $str = "您好,尊敬的用户 " . $user->getName() . " <br>";
    23 $str .= "您的级别是 ". $user->getGrade() .", <br>";
    24 $str .= "您的折扣是 " . $user->getDiscount() . "<br>";
    25 $str .= "购买 $number 本 《 ". $book->getProductName() ;
    26 $str .= "》的价格是 $price <br><br>";
    27 echo $str;
    28
    29 $user = new InnerUser("Tom");
    30 $price = ProductSettle::finalPrice($user,$book,$number);
    31 $str = "您好,尊敬的用户 " . $user->getName() . " <br>";
    32 $str .= "您的级别是 ". $user->getGrade() .", <br>";
    33 $str .= "您的折扣是 " . $user->getDiscount() . "<br>";
    34 $str .= "购买 $number 本 《 ". $book->getProductName() ;
    35 $str .= "》的价格是 $price <br><br>";
    36 echo $str;
    37 ?>
  • 相关阅读:
    C++引用之引用的使用
    C++引用之声明方法
    C++const与指针
    C++默认参数值函数
    LeanCloud 调研报告
    [译] 为何流处理中局部状态是必要的
    Z-Stack
    Think twice before starting the adventure
    Multi-pattern string match using Aho-Corasick
    C pointer again …
  • 原文地址:https://www.cnblogs.com/tenny/p/1999930.html
Copyright © 2011-2022 走看看