Oop 删除if is语句的设计选项
假设我有一个域对象的类层次结构,有一个基类和两个子类,一个级别 假设我有一个这些对象的列表(基类的列表),我想对我觉得不属于这些类的类应用一些逻辑(例如,特定于设计/用户界面的代码) 我的选择是什么Oop 删除if is语句的设计选项,oop,polymorphism,typechecking,Oop,Polymorphism,Typechecking,假设我有一个域对象的类层次结构,有一个基类和两个子类,一个级别 假设我有一个这些对象的列表(基类的列表),我想对我觉得不属于这些类的类应用一些逻辑(例如,特定于设计/用户界面的代码) 我的选择是什么 如果是语句。就个人而言,这本书甚至不应该被视为一个替代品,但我还是写了它 多态性。在某些情况下,这实际上是另一种选择,但在我上面的示例中,我不希望我的类包含任何UI细节 根据对象的类型,通过反射/IoC容器解析某些逻辑方法。 例如C#。Type Type=typeof(ILogic).MakeGen
例如C#。Type Type=typeof(ILogic).MakeGenericType(domainObject.GetType())
我真的很喜欢这一个,我没有得到任何编译时检查,但是如果一个子类缺少一个实现,或者这是可能的吗
有人有其他的技巧或窍门来解决这类问题吗?好问题。问题是有很多解决方案,其中大多数都能奏效 我经常和MVC合作,类似的情况经常发生。特别是在视图中,当需要在某些视图中进行类似渲染时。。。但并不真正属于这个观点 假设我们有一个子类
ChildList
,它扩展了BaseList
在子类中添加属性uiHandler
。重载呈现函数,比如说toString()
,并将uiHandler用于特定的UI/设计
我写了一个小东西,是用PHP写的。。。但你应该有个想法。它使您可以自由选择对象的显示方式,并且可以灵活地为特定对象使用特定的UI
看看下面的代码,看起来很多,但int并没有那么糟糕
-您的基类BaseList
-使用UI的基类,将可选UI类作为构造函数参数。在C#4中,您可以使用可选的,否则使用2个构造函数BaseListUIExtended
-UI类的接口UIBase
-UI类UIChildSpecific
-可使用或不使用UI的子类,因为ChildList
可选构造函数参数baselistuextended
定义接口
/**
* Base UI interface
*/
interface IUIBase {
/**
* Renders the Base Class
*
* @param UIBase $obj
* @return string
*/
public function render($obj);
}
定义基类、子类
//**************************************************************
// Define Base Classes
//**************************************************************
/**
* Base Class
*/
class BaseList {
/**
* List of items
* @var array
*/
protected $_items = array();
/**
* Gets collection of items
*
* @return array
*/
public function getItems() {
return $this->_items;
}
/**
* Adds new item to the list
* @param object $item
*/
public function add($item) {
$this->_items[] = $item;
}
/**
* Displays object
*/
public function display() {
echo $this->toString();
}
/**
* To String
*/
public function __toString() {
// Will output list of elements separated by space
echo implode(' ', $this->_items);
}
}
/**
* Extended BaseList, has UI handler
* This way your base class stays the same. And you
* can chose how you create your childer, with UI or without
*/
class BaseListUIExtended extends BaseList {
/**
* UI Handler
* @var UIBase
*/
protected $_uiHandler;
/**
* Default Constructor
*
* @param UIBase Optional UI parameter
*/
public function __construct($ui = null) {
// Set the UI Handler
$this->_uiHandler = $ui;
}
/**
* Display object
*/
public function display() {
if ($this->_uiHandler) {
// Render with UI Render
$this->_uiHandler->render($this);
} else {
// Executes default BaseList display() method
// in C# you'll have base:display()
parent::display();
}
}
}
//**************************************************************
// Define UI Classe
//**************************************************************
/**
* Child Specific UI
*/
class UIChildSpecific implements UIBase {
/**
* Overload Render method
*
* Outputs the following
* <strong>Elem 1</strong><br/>
* <strong>Elem 2</strong><br/>
* <strong>Elem 3</strong><br/>
*
* @param ChildList $obj
* @return string
*/
public function render($obj) {
// Output array for data
$renderedOutput = array();
// Scan through all items in the list
foreach ($obj->getItems() as $text) {
// render item
$text = "<strong>" . strtoupper(trim($text)) . "</strong>";
// Add it to output array
$renderedOutput[] = $text;
}
// Convert array to string. With elements separated by <br />
return implode('<br />', $renderedOutput);
}
}
//**************************************************************
// Defining Children classes
//**************************************************************
/**
* Child Class
*/
class ChildList extends BaseListUIExtended {
// Implement's logic
}
//**************************************************************
//定义基类
//**************************************************************
/**
*基类
*/
类基本列表{
/**
*项目清单
*@var数组
*/
受保护的$_items=array();
/**
*获取项的集合
*
*@return数组
*/
公共函数getItems(){
返回$this->\u项目;
}
/**
*将新项目添加到列表中
*@param对象$item
*/
公共功能添加($项){
$this->_items[]=$item;
}
/**
*显示对象
*/
公共功能显示(){
echo$this->toString();
}
/**
*串
*/
公共函数{
//将输出按空格分隔的元素列表
回声内爆(“”,$this->\u项);
}
}
/**
*扩展的基本列表,具有UI处理程序
*这样,您的基类保持不变。你呢
*您可以选择如何创建您的孩子,带UI或不带UI
*/
类BaseListUIExtended扩展了BaseList{
/**
*用户界面处理程序
*@var-UIBase
*/
受保护的$\u uiHandler;
/**
*默认构造函数
*
*@param UIBase可选用户界面参数
*/
公共函数构造($ui=null){
//设置UI处理程序
$this->_uiHandler=$ui;
}
/**
*显示对象
*/
公共功能显示(){
如果($this->\u uiHandler){
//使用UI渲染进行渲染
$this->\u uiHandler->render($this);
}否则{
//执行默认的BaseList display()方法
//在C#中,您将拥有base:display()
父::显示();
}
}
}
//**************************************************************
//定义UI类
//**************************************************************
/**
*子特定用户界面
*/
类UIChildSpecific实现UIBase{
/**
*重载渲染方法
*
*输出如下
*元素1
*元素2
*元素3
*
*@param ChildList$obj
*@返回字符串
*/
公共功能渲染($obj){
//数据的输出数组
$renderOutput=array();
//扫描列表中的所有项目
foreach($obj->getItems()作为$text){
//渲染项
$text=“”.strtoupper(修剪($text))。“”;
//将其添加到输出数组
$renderOutput[]=$text;
}
//将数组转换为字符串。元素之间用
分隔
返回内爆('
',$renderOutput);
}
}
//**************************************************************
//定义儿童班级
//**************************************************************
/**
*儿童班
*/
类ChildList扩展BaseListUIExtended{
//执行器逻辑
}
测试
//**************************************************************
// TESTING
//**************************************************************
// Test # 1
$plainChild = new ChildList();
$plainChild->add("hairy");
$plainChild->add("girl");
// Display the object, will use BaseList::display() method
$plainChild->display();
// Output: hairy girl
// Test # 2
$uiChild = new ChildList(new UIChildSpecific());
$uiChild->add("hairy");
$uiChild->add("girl");
// Display the object, will use BaseListUIExtended::display() method
$uiChild->display();
// Output: <strong>hairy</strong><br /><strong>girl</strong>
//**************************************************************
//测试
//**************************************************************
//测试#1
$plainChild=新的子列表();
$plainChild->add(“毛茸茸的”);
$plainChild->添加(“女孩”);
//显示对象时,将使用BaseList::Display()方法
$plainChild->display();
//输出:毛茸茸的女孩
//测试#2
$