java设计模式学习之装饰模式
作者:南国木棉 时间:2021-10-02 04:22:54
装饰模式:动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更加灵活。
优点:装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。
缺点:多层装饰比较复杂。
实例:给一个人配置穿衣
1:代码结构图
2:创建一个person类( ConcreteComponent)
package DecoratorModel;
/**
* 2017-10-9 10:39:09
* 装饰器设计模式
* Person 类 ConcreteComponent
* @author 我不是张英俊
*
*/
public class Person {
public Person(){}
private String name;
public Person(String name){
this.name=name;
}
public void Show(){
System.out.println("装扮的"+name);
}
}
3:服饰类
package DecoratorModel;
/**
*服饰类(Decorator)
* @author 我不是张英俊
*
*/
public class Finery extends Person{
protected Person component;
//打扮
public void Decorate(Person component){
this.component=component;
}
public void Show(){
if(component!=null){
component.Show();
}
}
}
4:具体服饰类
public class Tshirts extends Finery {
public void Show(){
System.out.println("大T恤");
super.Show();
}
}
public class BigTrouser extends Finery {
public void Show(){
System.out.println("垮裤");
super.Show();
}
}
public class Sneakers extends Finery {
public void Show(){
System.out.println("破球鞋");
super.Show();
}
}
public class Suit extends Finery {
public void Show(){
System.out.println("西装");
super.Show();
}
}
public class Tie extends Finery {
public void Show(){
System.out.println("领带");
super.Show();
}
}
public class LeatherShoes extends Finery {
public void Show(){
System.out.println("皮鞋");
super.Show();
}
}
5:测试类
public class test {
public static void main(String[] args) {
Person xc=new Person("旺财");
Sneakers pqx=new Sneakers();
BigTrouser kk=new BigTrouser();
Tshirts dtx=new Tshirts();
pqx.Decorate(xc);
kk.Decorate(pqx);
dtx.Decorate(kk);
dtx.Show();
}
}
6:控制台
大T恤
垮裤
破球鞋
装扮的旺财
来源:http://www.cnblogs.com/hrlizhi/p/7641082.html
标签:java,设计模式,装饰模式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java swing实现简单的五子棋游戏
2022-04-16 22:10:10
![](https://img.aspxhome.com/file/2023/4/83124_0s.jpg)
解决spring mvc 多数据源切换,不支持事务控制的问题
2022-09-30 03:39:56
tk.mybatis实现uuid主键生成的示例代码
2022-01-22 00:17:13
![](https://img.aspxhome.com/file/2023/6/83446_0s.png)
Java中Set&List的迭代器实现步骤解析
2021-05-27 16:47:06
C#中TreeView节点的自定义绘制方法
2023-04-01 10:37:19
C#实现希尔排序
2023-11-02 08:15:04
![](https://img.aspxhome.com/file/2023/3/104213_0s.jpg)
Java面试题之基本语法(图解)
2023-11-24 09:37:34
![](https://img.aspxhome.com/file/2023/5/59915_0s.png)
在java代码中获取JVM参数的方法
2022-08-15 01:05:43
JavaWeb中上传和下载文件实例代码
2023-11-04 10:24:00
![](https://img.aspxhome.com/file/2023/5/78855_0s.png)
c#语言使用Unity粒子系统制作手雷爆炸
2021-10-11 11:13:46
![](https://img.aspxhome.com/file/2023/8/92918_0s.png)
Unity实现滑动更换界面效果
2021-10-06 19:56:40
![](https://img.aspxhome.com/file/2023/6/84466_0s.jpg)
如何用120行Java代码写一个自己的区块链
2023-07-17 03:44:33
使用springboot aop来实现读写分离和事物配置
2022-10-29 11:43:07
![](https://img.aspxhome.com/file/2023/6/69086_0s.jpg)
spring mvc+localResizeIMG实现HTML5端图片压缩上传
2023-07-11 15:31:51
Springboot 如何指定获取出 yml文件里面的配置值
2022-08-29 21:04:48
![](https://img.aspxhome.com/file/2023/6/62706_0s.png)
利用Java代码写一个并行调用模板
2023-07-04 16:00:39
![](https://img.aspxhome.com/file/2023/0/95960_0s.webp)
Java并发编程之浅谈ReentrantLock
2022-08-25 10:46:02
![](https://img.aspxhome.com/file/2023/5/91735_0s.png)
Spring Data JPA实现动态查询的两种方法
2022-08-04 17:02:27
java eclipse 中文件的上传和下载示例解析
2021-11-18 06:55:58
![](https://img.aspxhome.com/file/2023/9/112049_0s.png)
解决JTable排序问题的方法详解
2023-02-07 08:53:42