日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

《研磨设计模式》chap14 迭代器模式(2)算工资举例

發(fā)布時間:2025/3/21 asp.net 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《研磨设计模式》chap14 迭代器模式(2)算工资举例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一個公司:數(shù)組工資
一個公司:list工資
如何統(tǒng)一?

1. 正常編碼

public class PayModel { private String userName;//支付工資的人員 private double pay;//支付的工資數(shù)額 }public class SalaryManager{//用數(shù)組管理 private PayModel[] pms = null;//獲取工資列表 public PayModel[] getPays(){return pms;}//計(jì)算工資 public void calcSalary(){ PayModel pm1 = new PayModel();pm1.setPay(2200);pm1.setUserName("王五");PayModel pm2 = new PayModel();pm2.setPay(3600);pm2.setUserName("趙六");pms = new PayModel[2];pms[0] = pm1;pms[1] = pm2;} }public class PayManager{//聚合對象,這里是Java的集合對象 private List list = new ArrayList();//獲取工資列表 public List getPayList(){return list;}//計(jì)算工資,其實(shí)應(yīng)該有很多參數(shù),為了演示從簡 public void calcPay(){ PayModel pm1 = new PayModel();pm1.setPay(3800);pm1.setUserName("張三");PayModel pm2 = new PayModel();pm2.setPay(5800);pm2.setUserName("李四");list.add(pm1);list.add(pm2);} }public static void main(String[] args) {//訪問集團(tuán)的工資列表PayManager payManager= new PayManager();//先計(jì)算再獲取payManager.calcPay();Collection payList = payManager.getPayList();Iterator it = payList.iterator();System.out.println("集團(tuán)工資列表:");while(it.hasNext()){PayModel pm = (PayModel)it.next();System.out.println(pm);}//訪問新收購公司的工資列表SalaryManager salaryManager = new SalaryManager();//先計(jì)算再獲取salaryManager.calcSalary();PayModel[] pms = salaryManager.getPays();System.out.println("新收購的公司工資列表:");for(int i=0;i<pms.length;i++){System.out.println(pms[i]);}}

2. 統(tǒng)一訪問接口

public class ArrayIteratorImpl implements Iterator{//用來存放被迭代的聚合對象 private SalaryManager aggregate = null;//用來記錄當(dāng)前迭代到的位置索引 private int index = -1;public ArrayIteratorImpl(SalaryManager aggregate){this.aggregate = aggregate;} public void first(){index = 0;}public void next(){if(index < this.aggregate.size()){index = index + 1;}}public boolean isDone(){if(index == this.aggregate.size()){return true;}return false;}public Object currentItem(){return this.aggregate.get(index);} }public class SalaryManager extends Aggregate{//用數(shù)組管理 private PayModel[] pms = null; public PayModel[] getPays(){return pms;}//計(jì)算工資,其實(shí)應(yīng)該有很多參數(shù),為了演示從簡 public void calcSalary(){ PayModel pm1 = new PayModel();pm1.setPay(2200);pm1.setUserName("王五");PayModel pm2 = new PayModel();pm2.setPay(3600);pm2.setUserName("趙六");pms = new PayModel[2];pms[0] = pm1;pms[1] = pm2;}public Iterator createIterator(){return new ArrayIteratorImpl(this);}public Object get(int index){Object retObj = null;if(index < pms.length){retObj = pms[index];}return retObj;}public int size(){return this.pms.length;} }public static void main(String[] args) {//訪問集團(tuán)的工資列表PayManager payManager= new PayManager();//先計(jì)算再獲取payManager.calcPay();System.out.println("集團(tuán)工資列表:");test(payManager.createIterator());//訪問新收購公司的工資列表SalaryManager salaryManager = new SalaryManager();//先計(jì)算再獲取salaryManager.calcSalary();System.out.println("新收購的公司工資列表:");test(salaryManager.createIterator());}//聚合對象的迭代器 private static void test(Iterator it){//循環(huán)輸出聚合對象中的值//首先設(shè)置迭代器到第一個元素it.first();while(!it.isDone()){//取出當(dāng)前的元素來Object obj = it.currentItem();System.out.println("the obj=="+obj);//如果還沒有迭代到最后,那么就向下迭代一個it.next();}}

總結(jié)

以上是生活随笔為你收集整理的《研磨设计模式》chap14 迭代器模式(2)算工资举例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。