Decorator和Factory设计模式的混搭
来源:互联网 发布:推荐系统算法 编辑:程序博客网 时间:2024/04/30 11:29
今天学了Decorator设计模式,但是main函数内太过复杂,突然想到了前几天学的Factory设计模式,因此尝试组合在一起看看效果如何。在Factory –条件语句if里面有一些重复代码,请问怎么处理。请多指教。
interface Product{ public void productName();}class TV implements Product{ public void productName(){ System.out.println("cctv-5 NBA"); }}class Washer implements Product{ public void productName(){ System.out.println("wash cloth...."); }}class TCL implements Product{ private Product product; public TCL(Product product){ this.product = product; } public void productName(){ product.productName(); System.out.println("****produced by TCL corporation*****"); }}class Factory{ public static Product getInstance(String company,String product){ if(company.equals("TCL") && product.equals("TV")){ Product tv = new TV(); TCL tclProduct = new TCL(tv); tclProduct.productName(); return tclProduct; }else if(company.equals("TCL") && product.equals("Washer")){ Product washer = new Washer(); TCL tclProduct = new TCL(washer); tclProduct.productName(); return tclProduct; }else{ return null; } }}public class TestDemo{ public static void main(String args[]){ Factory pd = new Factory(); pd.getInstance("TCL","TV"); pd.getInstance("TCL","Washer"); }}/*Outputcctv-5 NBA****produced by TCL corporation*****wash cloth....****produced by TCL corporation******/
0 0
- Decorator和Factory设计模式的混搭
- Head First 设计模式 Design Pattern 3-4 Decorator, Factory
- 设计模式的解析和实现(C++)之一-Factory模式
- 设计模式的解析和实现(C++)之一-Factory模式
- Java设计模式之Builder和Decorator
- 设计模式 - Decorator模式
- 设计模式-decorator模式
- 设计模式-Decorator模式
- 设计模式 - Decorator模式
- 设计模式--Decorator 模式
- C++的Factory设计模式
- java Decorator设计模式 Decorator设计模式
- [GoF设计模式]Decorator模式和Observer模式的C++实现
- Decorator设计模式的一个实例
- [C#]我的实用设计模式之四-Simple Factory,Factory Method和Abs
- SharpDevelop设计模式(Decorator)
- 设计模式之Decorator
- Decorator设计模式 C#
- "冒泡排序"算法-图文代码教你轻松简单理解-java编程
- 使用_CrtSetDbgFlag检测内存泄露
- 有趣的Linux命令
- Fedora openjdk 中文方框乱码
- AllHookInOne 中的hook基本流程
- Decorator和Factory设计模式的混搭
- 数据库2
- arm指令备注
- docker-py 文件传输put_archive
- 如何提升代码的逼格之设计模式
- Sqlserver查询数据库文件大小和剩余空间
- Linux各种发行版本概述(Redhat系 | Debian系)
- 一些集合的循环输出,从控制器(C)到视图页面(V)
- Java中break与continue用法