装饰模式

结构型模式————装饰器模式(3.1)

為{幸葍}努か 提交于 2019-11-27 05:45:45
什么是装饰器模式? 【先吃三颗栗子:】 1.PC= 主机 +显示器+键盘+鼠标+鼠标垫 主机是核心,而其他的组成部分都是装饰。 2.手抓饼= 饼 +鸡蛋+培根+黄瓜 饼是核心,鸡蛋,培根是可选的,可以理解为装饰。 3.咖啡= 咖啡 +牛奶+冰+方糖 咖啡是核心,牛奶等可选。 比喻虽然形象生动,但是与实际或多或少会产生偏差。 抽象的解释:装饰器模式的目的—— 核心部分和装饰部分可以自由组合。 装饰器模式的功能 对于软件开发来说,聚焦于软件的灵活性和可扩展性。 装饰器模式要求: 装饰可选 装饰可扩展 核心部分可扩展 手抓饼中的装饰器模式 实现饼加各种配料的价格计算。 思路: 1.定义抽象类手抓饼,其中包含获取种类的抽象方法和获取价格的抽象方法。 2.然后定义两个分别为大份手抓饼和中份手抓饼来继承这个抽象类,重写两个方法定义种类和价格。 3.定义具体的配料类继承手抓饼抽象类,先定义构造器传入已经创建的手抓饼种类,然后重写种类和价格方法。 在网上看的一些博客,是创建出一个抽象的配料类来继承抽象产品类,然后用具体的配料类来实现抽象配料类,实现装饰。 可是如果直接将抽象产品类的方法全部定义抽象方法,配料类直接继承重写,实现装饰,功能上也是可以实现的,如下所示,Bacon类直接继承HandPancake,然后重写getName和getCost方法,实现装饰。似乎也没有问题:

设计模式之结构类模式大PK

家住魔仙堡 提交于 2019-11-27 00:36:46
结构类模式大PK 结构类模式包括 适配器模式 、 桥梁模式 、组合模式、 装饰模式 、 门面模式 、 享元模式 和 代理模式 。之所以称其为结构类模式,是因为他们都是通过组合类或对象产生更大结构以适应更高成层次的逻辑需求。我们来分析一下装饰模式和代理模式、装饰模式和适配器模式。 装饰模式VS代理模式    首先要说明的是装饰模式是代理模式的特殊应用,两者的共同点是有相同的接口,不同点事代理模式着重对代理过程的控制,而装饰模式则是对类的功能进行加强或减弱,着重类功能的变化。我们看看两者的类图吧!就是一模一样只是代理类和装饰类的名字不一样了而已。我们通过代理类和装饰类看看二者的区别吧。 //代理模式还是侧重于对代理过程的控制,是否允许功能的执行 public class SingerAgent{ private Singer singer; public SingerAgent(Singer singer){ this.singer = singer; } public void singing(){ Random rand = new Random(); if(rand.nextBoolean()){ System.out.println("代理人同意歌手唱歌"); singer.sing(); }else{ System.out.println("代理人不同意歌手唱歌"); } }

设计模式之GOF23装饰模式

微笑、不失礼 提交于 2019-11-26 23:34:32
装饰模式decorator 作用: -动态的为一个对象增加新功能 -装饰模式是一种用于代替继承的技术, 无需通过增加子类就能扩展对象的新功能 ,适用对象的组合关系代替继承关系,更加灵活, 同时避免类型体系的快速膨胀 角色: -Component抽象构件角色:真实对象和装饰对象具有相同的接口,这样,客户端对象就能够以与真实对象相同的方式与装饰对象进行交互 -ConcreteComponent具体构件角色(真实对象):被装饰的角色 - Decorator装饰角色 :实现相同接口,并持有一个抽象构件的引用,这样既可以完成客户端的所有请求,又可以增添新的功能 -ConcreteDecorator具体装饰角色:负责给构建对象增加新的责任 装饰模式和桥接模式的区别: 两个模式都是为了解决过多子类对象问题,但桥接模式的诱因是多个维度多个原因导致的多重继承,装饰模式是为了增加新的功能 例子:跑车 来源: https://www.cnblogs.com/code-fun/p/11335280.html

<人人都懂设计模式>-装饰模式

谁都会走 提交于 2019-11-26 22:13:22
书上,真的用一个人穿衣打拌来讲解装饰模式的呢。 from abc import ABCMeta, abstractmethod class Person(metaclass=ABCMeta): def __init__(self, name): self._name = name @abstractmethod def wear(self): print("着装。。。") class Engine(Person): def __init__(self, name, skill): super().__init__(name) self.__skill = skill def get_skill(self): return self.__skill def wear(self): print("我是{}工程师{}。".format(self.get_skill(), self._name)) super().wear() class Teacher(Person): def __init__(self, name, title): super().__init__(name) self.__title = title def get_title(self): return self.__title def wear(self): print("我是{}{}。".format(self

装饰器模式

蓝咒 提交于 2019-11-26 19:28:03
定义 动态的给一个对象添加一些额外的职责。就功能来说,装饰模式相比生成子类更加灵活 使用场景 装饰类和被装饰类都可以独立发展,不会耦合,它是继承关系的一个替代方案 需要扩展一个类的功能,或者给一个类增加附加功能 动态增加一个类的功能,再动态的撤销 需要为一批兄弟类,进行改装或者加装功能首选装饰模式 实现方式 组件接口 /** * 基础接口 **/ public interface Component { public void operate(); } 组件实现类 /** * 具体实现类 **/ public class ConcretComponent implements Component { public void operate() { System.out.println("ConcretComponent operate"); } } 装饰类 /** * 装饰类 **/ public class Decorator implements Component { public Component component; public Decorator(Component component) { this.component = component; } public void operate() { this.component.operate(); } }

转载:唐磊的个人博客《python中decorator详解》【转注:深入浅出清晰明了】

空扰寡人 提交于 2019-11-26 19:25:56
转载请注明来源: 唐磊的个人博客《python中decorator详解》 前面写python的AOP解决方案时提到了decorator,这篇文章就详细的来整理下python的装饰器——decorator。 python中的函数即objects 一步一步来,先了解下python中的函数。 def shout(word='hello,world'): return word.capitalize() + '!'print shout()#输出:Hello,world!#跟其他对象一样,你同样可以将函数对象赋值给其他变量scream = shout#注意,shout后面没有用括号(),用括号就是调用了shout方法,这里仅仅是将shout的函数对象复制给scream。#意味着,你可以通过scream调用shout方法。print scream()#同样输出:Hello,world!#此外,你将代表函数变量的shout删除,shout现在失效,但scream同样可以调用del shoutprint shout()#会抛异常#Traceback (most recent call last):# File "S:\decorator.py", line 18, in # print shout()#NameError: name 'shout' is not defined#

结构型设计模式——装饰模式

筅森魡賤 提交于 2019-11-26 17:07:46
这个玩意儿吧,其实用处就是为了在之后的项目中动态的给某一个对象更多的责任,或者说是执行方法,(当时程序是在运行时)它的设计方法的重点在那个Component接口,首先创建一个ConcreteComponent对象,将其作为参数传递到ConcreteDecoratorA这个装饰器内,这个装饰器给你返回一个全新的具有你原本功能的Component,实际上也就是把你的引用放进去,包一层返回给你。包的那一层就是它给你加的功能package mainimport "fmt"type Component interface { operation()}type ConcreteComponent struct {}func(it *ConcreteComponent)operation(){ fmt.Println("asf")}type ConcreteDecoratorA struct { concretecomponent Component}func(it *ConcreteDecoratorA)set(component Component ) Component{ it.concretecomponent = component return it}func(it *ConcreteDecoratorA)operation(){ it.concretecomponent

设计模式——装饰者模式

◇◆丶佛笑我妖孽 提交于 2019-11-26 14:53:51
装饰者模式 1. 定义 装饰者模式 动态的将责任附加到对象上。如要扩展功能,装饰者提供了比继承更有弹性的替代方案。 2. 类图 3. Example 假如有这样一个需求:有一天,奶茶店的老板喊你给他们做一个简单版的计算价格程序。新店开张,供应的饮料有奶茶(8元)、红茶(7元)、绿茶(5元)三种,提供的配料有波霸1元,布丁5元,红豆3元。 则可如下设计: Component及其实现如下: package com.gitlearning.hanldegit.patterns.decorator; /** * 相当于Component */ public abstract class Beverage { String description = "Unkown beverage"; public String getDescription() { return description; } public abstract double cost(); } class MilkTea extends Beverage { @Override public String getDescription() { return "奶茶"; } @Override public double cost() { return 8.00; } } class RedTea extends

单例模式的装饰器实现

点点圈 提交于 2019-11-26 13:05:59
def singleton(cls): __instance = {} def wrapper(x): if cls in __instance: return __instance[cls] else: __instance[cls] = cls(x) return __instance[cls] return wrapper# @singletonclass A: def __init__(self,x=0): self.x = xa1 = A(1)a2 = A(2)print(a1)print(a2) 来源: https://www.cnblogs.com/llbky/p/11321742.html

装饰器模式

只谈情不闲聊 提交于 2019-11-26 11:10:23
装饰器模式 这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。 1、创建一个抽象类 1 public interface Shape { 2 void draw(); 3 } Shape 2、编写抽象类的实现类 1 public class Circle implements Shape { 2 @Override 3 public void draw() { 4 System.out.println("Circle"); 5 } 6 } Circle 1 public class Rectangle implements Shape { 2 @Override 3 public void draw() { 4 System.out.println("Rectangle"); 5 } 6 } Rectangle 3、创建实现了 Shape 接口的抽象装饰类 1 public abstract class ShapeDecorator implements Shape{ 2 protected Shape decoratedShape; 3 4 public ShapeDecorator (Shape decoratedShape){ 5 this.decoratedShape = decoratedShape; 6 } 7 8