存储模式的PHP OOP类
我对使用OOP管理商店物品的系统的设计表示怀疑。 我创建了具有$name和$price属性的抽象类产品。后来,我创建了一个类Smartphone来扩展产品。智能手机具有$brand和$os属性。后来,我创建了类Computer,它扩展了产品,并具有$cpuffrequency和$ram属性。现在,如果我想创建一个对应于Nexus5的对象,例如,我必须做什么? 例如,考虑这个对象将具有< /P>存储模式的PHP OOP类,php,oop,Php,Oop,我对使用OOP管理商店物品的系统的设计表示怀疑。 我创建了具有$name和$price属性的抽象类产品。后来,我创建了一个类Smartphone来扩展产品。智能手机具有$brand和$os属性。后来,我创建了类Computer,它扩展了产品,并具有$cpuffrequency和$ram属性。现在,如果我想创建一个对应于Nexus5的对象,例如,我必须做什么? 例如,考虑这个对象将具有< /P> $name = "Nexus 5" $price = "250" $brand = "LG" $os=
$name = "Nexus 5"
$price = "250"
$brand = "LG"
$os="Android"
$cpuFrequency "2.0"
$ram = "2".
下面是一个类的示例
abstract class Product {
private $name;
private $price;
//more methods
}
class Smartphone extends Product{
private $brand;
private $os;
}
class Computer extends Product {
private $cpuFrequency;
private $ram;
}
还考虑到可能存在没有$os的计算机和没有$ram的智能手机。只有一个例子:D
我知道智能手机是一台电脑。我做了一个坏榜样。请在电话和电脑前思考。电话有$number和$operator。现在我想创造一个智能手机,同时是电话和电脑
谢谢大家,再次为我的英语感到抱歉,正如其他人所说,上太多的课会让人困惑 其想法是创建一个可以覆盖许多不同场景的类,而不需要做太多工作 例如,您可以有一个类
Pet
。狗是宠物,蛇也是。。。这并不意味着你应该做一个FourLegged
或Nolegs
类
与您的产品非常相似,您可能有一个product
类,该类保存所有产品的公共内容。你甚至可能想要一个叫做电子学的子类
,它可以涵盖从电话到计算机的一切
您可能有一个
productType
属性来说明它是什么类型的电子产品(如手机),但最终,各个电子产品之间不会有太大的差异:电视、智能手机和电脑都有屏幕规格,智能手机和电脑都有ram、cpu等。正常情况下,您应该以“child”类初始化超类的某种方式为类编写构造函数,以保持所有内容的一致性
abstract class Product {
private $name;
private $price;
//more methods
public __construct($name, $price){
$this->name = $name;
$this->price =$price;
}
}
class Smartphone extends Product{
private $brand;
private $os;
// default value to $os: like that, we can create a Smartphone whitout an os
public __construct($name, $price, $brand, $os = null){
parent::__construct($name, $price);
$this->brand = $brand;
$this->os =$os;
}
}
class Computer extends Product {
private $cpuFrequency;
private $ram;
// default value to $ram: with this, we can create a Computer whiout ram
public __construct($name, $price, $frequency, $ram = null){
parent::__construct($name, $price);
$this->frequency = $frequency;
$this->ram =$ram;
}
}
然后,要创建一台计算机,您可以
$computer = new Computer('notBuiltToWindows', 560 , 2.0, 4);
或者,要在示例中创建Nexus:
$nexus = new Smartphone($name, $price, $frequency, $ram);
还有,创建一个没有内存的智能手机
$nexus = new Smartphone($name, $price, $frequency);
您可以尝试使用如下ProductProperties类:
class ProductProperties
{
private properties = array();
public function getProperties()
{
return this->properties;
}
public function addProperty($id, $value)
{
this->properties[$id] = $value;
}
public function getProperty($id)
{
return this->properties[$id];
}
}
abstract class Product
{
private $properties;
//more methods
public function __construct(ProductProperties $properties)
{
$this->properties = $properties;
}
}
您可以将ProductProperties类的对象传递给任何产品,并让它处理所有与属性相关的问题。这将使设计更加灵活,这样您就不需要仅仅因为产品具有不同的属性集就进行子类化。记住,子类化就是改变或添加行为 我们的工作方式是上述大多数答案的组合: 1) 您有一个产品类/对象-它包含任何产品都会有的通用内容-名称、描述、一些图片等 2) 您有一个子类,其中包含产品属性(产品属性类)-例如尺寸、重量(并非所有产品都是物理的,因此并非所有产品都有尺寸或重量)、价格、ram插槽数量、频率 我们允许通过使用将属性映射到数据库的ORM来用户定义属性。由于php不能进行多重继承,这两个方面都得到了最好的效果——从技术上讲,智能手机既是一台计算机也是一部电话,但通过上述方式,这并不重要,因为每一个都只是一个产品和一组用户定义的属性
本质上,这与@Edgar的实现是相同的,在顶层,我们创造了概念,非有形的东西。理论上的东西,必须实施才能有形
// make product an interface, or in other words a concept. its not tangible,
// but instead its just a concept thus making it an interface
interface Product {
public function getName();
public function getPrice();
public function getBrand();
}
// A computer is also a concept, thats implemented by many devices like a laptop,
// SMARTPHONE, desktop, calculator, etc...
interface Computer {
public function getRam();
public function getFrequency();
public function getOS();
}
电话逻辑:
// We can implement the concept of a Product here
// the phone is also a concept, implemented by home phones,
// telephones and mobile phones so we make it abstract
abstract class Phone implements Product {
private $name;
private $price;
private $brand;
function __construct ($name, $price, $brand) {
$this->name = $name;
$this->price = $price;
$this->brand = $brand;
}
public function getName() {
return $this->name;
}
public function getPrice() {
return $this->price;
}
public function getBrand() {
return $this->brand;
}
}
// Smartphone is another concept implemented by many devices like a Nexus 5,
// what makes this concept different from a Phone is that it doubles as a computer.
// so we extend the concept of a phone by implement the concept of a computer
// making the Phone 'smart'
abstract class Smartphone extends Phone implements Computer {
private $os;
private $frequency;
private $ram;
public function __construct ($name, $price, $brand, $os, $frequency, $ram) {
parent::__construct($name, $price, $brand);
$this->os = $os;
$this->frequency = $frequency;
$this->ram = $ram;
}
public function getRam() {
return $this->ram;
}
public function getFrequency() {
return $this->frequency;
}
public function getOS() {
return $this->os;
}
}
智能手机的逻辑:
// We can implement the concept of a Product here
// the phone is also a concept, implemented by home phones,
// telephones and mobile phones so we make it abstract
abstract class Phone implements Product {
private $name;
private $price;
private $brand;
function __construct ($name, $price, $brand) {
$this->name = $name;
$this->price = $price;
$this->brand = $brand;
}
public function getName() {
return $this->name;
}
public function getPrice() {
return $this->price;
}
public function getBrand() {
return $this->brand;
}
}
// Smartphone is another concept implemented by many devices like a Nexus 5,
// what makes this concept different from a Phone is that it doubles as a computer.
// so we extend the concept of a phone by implement the concept of a computer
// making the Phone 'smart'
abstract class Smartphone extends Phone implements Computer {
private $os;
private $frequency;
private $ram;
public function __construct ($name, $price, $brand, $os, $frequency, $ram) {
parent::__construct($name, $price, $brand);
$this->os = $os;
$this->frequency = $frequency;
$this->ram = $ram;
}
public function getRam() {
return $this->ram;
}
public function getFrequency() {
return $this->frequency;
}
public function getOS() {
return $this->os;
}
}
最后,我们有了一个完整的概念,一个可行/有形的概念。Nexus产品
// A Nexus implements the concept of a Smartphone.
// so now the Nexus is a Smartphone, Computer, Phone and a Product
class Nexus extends Smartphone {
public function __construct ($version) {
parent::__construct('Nexus '.$version, 250, 'LG', 'Android', '2.0', 2);
}
}
使用:
你将得到无数不同的类,它们本身是完全不可用/不可维护的。智能手机是一台能够打电话的计算机。就像笔记本电脑一样,它是一台便携式电脑,内置了屏幕/硬盘。你可以将产品和规格分开。这将允许您不必为每个新类型创建子类。您应该将
Product
作为一个接口,然后实现它。智能手机
应该扩展一个手机
类并实现一个计算机
接口从阅读问题开始,我假设op知道如何创建对象。据我所知,这个问题是关于他的应用程序的设计。谢谢。我脑子里有红宝石。我重读了这个问题,你是对的。我将编辑我的答案。我开始写一个答案,然后这个答案出现了,所以我不得不编辑我的答案并在这里给予赞扬:D-实际上,这是最好的方法,特别是如果你使用ORM@Jonathan这正是我在许多项目中使用它的方式。这只是最后一件事。为什么我必须创建一个只有数组作为属性的类?为什么我不能将这个属性(数组)直接放在类产品中?不过,谢谢大家,您已经非常详尽了。@user4304554请考虑灵活性和解耦。此时,您可能正在从数据库中获取产品属性,因此您需要将所有逻辑用于填充产品类中的属性数组。现在,您的系统需要从文本文件中读取产品属性,您必须更改产品类或将其子类化,以便从文本文件中读取属性。使用ProductProperties类,您可以让不同的类从不同的源读取属性,然后使用依赖项注入将正确类型的属性传递给产品,无需更改产品类记住子类化就是更改或添加行为。
这是否总是正确的?