[PHP] 装饰器模式-结构型设计模式
2023-02-18 15:36:34 时间
动态地为类的实例添加功能,一层一层的套功能
先定义好接口
interface Booking{ public function getDescription(): string; }
这个就是装饰器实现了Booking,通过构造函数传递Booking对象进来
abstract class BookingDecorator implements Booking{ protected Booking $booking; public function __construct(Booking $booking) { $this->booking = $booking; } }
这个类直接实现Booking
class DoubleRoomBooking implements Booking { public function getDescription(): string{ return 'double room'; } }
这个类继承了装饰器,实现了Booking
class WiFi extends BookingDecorator{ public function getDescription(): string { return $this->booking->getDescription() . ' with wifi'; } } class ExtraBed extends BookingDecorator { public function calculatePrice(): int { return $this->booking->calculatePrice() + self::PRICE; } public function getDescription(): string { return $this->booking->getDescription() . ' with extra bed'; } }
$booking = new DoubleRoomBooking();
//继承装饰器的可以一层层套
$booking = new WiFi($booking);
$booking = new ExtraBed($booking);
相关文章
- 论文解读《Bilinear Graph Neural Network with Neighbor Interactions》
- 论文解读《Deep Attention-guided Graph Clustering with Dual Self-supervision》
- 论文解读(ClusterSCL)《ClusterSCL: Cluster-Aware Supervised Contrastive Learning on Graphs》
- 论文解读(SimGRACE)《SimGRACE: A Simple Framework for Graph Contrastive Learning without Data Augmentation》
- 论文解读(GTN)《Graph Transformer Networks》
- 论文解读(SAGPool)《Self-Attention Graph Pooling》
- 论文解读(DiffPool)《Hierarchical Graph Representation Learning with Differentiable Pooling》
- 论文解读《Deeper Insights into Graph Convolutional Networks for Semi-Supervised Learning》
- 论文解读(GMT)《Accurate Learning of Graph Representations with Graph Multiset Pooling》
- 论文解读《Measuring and Relieving the Over-smoothing Problem for Graph NeuralNetworks from the Topological View》
- 论文解读(DAGNN)《Towards Deeper Graph Neural Networks》
- 论文解读(Debiased)《Debiased Contrastive Learning》
- 论文解读(IGSD)《Iterative Graph Self-Distillation》
- 论文解读(SUBG-CON)《Sub-graph Contrast for Scalable Self-Supervised Graph Representation Learning》
- 论文解读(MERIT)《Multi-Scale Contrastive Siamese Networks for Self-Supervised Graph Representation Learning》
- 论文解读(MCNS)《Understanding Negative Sampling in Graph RepresentationLearning》
- 论文解读(GROC)《Towards Robust Graph Contrastive Learning》
- 图神经网络的攻击防御
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- 论文解读(DCRN)《Deep Graph Clustering via Dual Correlation Reduction》