设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)(1)

}

public void printMenu() {

Iterator pancakeIterator = pancakeHouseMenu.createIterator();

Iterator dinerIterator = dinerMenu.createIterator();

System.out.println(“MENU\n----\nBREAKFAST”);

printMenu(pancakeIterator);

System.out.println(“\nLUNCH”);

printMenu(dinerIterator);

}

private void printMenu(Iterator iterator) {

while(iterator.hasNext()) { //测试是否还有其他项

MenuItem menuItem = (MenuItem) iterator.next();

System.out.print(menuItem.getName() + ", ");

System.out.print(menuItem.getPrice() + " – ");

System.out.println(menuItem.getDescription());

}

}

}

增加一个CafeMenu会变成以下这样

package dinermerger;

import java.util.Hashtable;

import java.util.Iterator;

/**

  • 咖啡厅菜单

  • @author wwj

*/

public class CafeMenu implements Menu{

Hashtable menuItems = new Hashtable();

public CafeMenu() {

addItem(“Veggie Burger and Air Fries”,

“Veggie burger on a whole wheat bun, lettuce, tomato, and fries”,

true, 3.99);

addItem(“Soup of the day”,

“A cup of the soup of the day, with a side salad”, false, 3.69);

addItem(“Burrito”,

“A large burrito, with whole pinto beans, salsa, guacamole”,

true, 4.29);

}

public void addItem(String name, String description, boolean vegetarian, double price) {

MenuItem menuItem = new MenuItem(name, description, vegetarian, price);

menuItems.put(menuItem.getName(), menuItem);

}

@Override

public Iterator createIterator() {

return menuItems.values().iterator();

}

}

package dinermerger;

import java.util.Iterator;

/**

  • 女招待类

  • @author wwj

*/

public class Waitress {

// PancakeHouseMenu pancakeHouseMenu;

// DinerMenu dinerMenu;

Menu pancakeHouseMenu;

Menu dinerMenu;

Menu cafeMenu;

/**

*在这个构造器中,女招待照顾两个菜单

  • @param pancakeHouseMenu

  • @param dinerMenu

*/

public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) {

this.pancakeHouseMenu = pancakeHouseMenu;

this.dinerMenu = dinerMenu;

}

/**

  • 新增一个cafeMenu

  • @param pancakeHouseMenu

  • @param dinerMenu

  • @param cafeMenu

*/

public Waitress(Menu pancakeHouseMenu, Menu dinerMenu, Menu cafeMenu) {

this.pancakeHouseMenu = pancakeHouseMenu;

this.dinerMenu = dinerMenu;

this.cafeMenu = cafeMenu;

}

public void printMenu() {

Iterator pancakeIterator = pancakeHouseMenu.createIterator();

Iterator dinerIterator = dinerMenu.createIterator();

Iterator cafeIterator = cafeMenu.createIterator();

System.out.println(“MENU\n----\nBREAKFAST”);

printMenu(pancakeIterator);

System.out.println(“\nLUNCH”);

printMenu(dinerIterator);

System.out.println(“\nDINNER”);

printMenu(cafeIterator);

}

private void printMenu(Iterator iterator) {

while(iterator.hasNext()) { //测试是否还有其他项

MenuItem menuItem = (MenuItem) iterator.next();

System.out.print(menuItem.getName() + ", ");

System.out.print(menuItem.getPrice() + " – ");

System.out.println(menuItem.getDescription());

}

}

}

Test

package dinermerger;

public class MenuTestDrive {

/**

  • @param args

*/

public static void main(String[] args) {

// PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();

// DinerMenu dinerMenu = new DinerMenu();

//

// Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);

//

// waitress.printMenu();

Menu pancakeHouseMenu = new PancakeHouseMenu();

Menu dinerMenu = new DinerMenu();

Menu cafeMenu = new CafeMenu();

Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu, cafeMenu);

waitress.printMenu();

}

}

结果:

MENU

----

BREAKFAST

K&B’s Pancake Breakfast, 2.99 – Pancakes with scrambled eggs, and toast

Regular Pancake Breakfast, 2.99 – Pancakes with fried eggs, sausage

Blueberry Pancakes, 3.49 – Pancakes made with fresh blueberries

Waffles, 3.59 – Waffles, with your choice of blueberries or strawberries

LUNCH

Vegetarian BLT, 2.99 – (Fakin’) Bacon with lettuce & tomato on Whole wheat

BLT, 2.99 – Bacon with lettuce & tomato on Whole wheat

Soup of the day, 3.29 – Soup of the day, with a side of potato salad

Hotdog, 3.05 – A hot dog, with saurkraut, relish, onions, topped with cheese

Steamed Veggies and Brown Rice, 3.99 – Steamed vegetables over brown rice

Pasta, 3.89 – Spaghetti with Marinara Sauce, and a slice of sourdough bread

DINNER

Soup of the day, 3.69 – A cup of the soup of the day, with a side salad

Burrito, 4.29 – A large burrito, with whole pinto beans, salsa, guacamole

Veggie Burger and Air Fries, 3.99 – Veggie burger on a whole wheat bun, lettuce, tomato, and fries

为了让女招待更加有扩展性,我们这样做

package dinermerger;

import java.util.ArrayList;

import java.util.Iterator;

/**

  • 女招待类

  • @author wwj

*/

public class Waitress {

// PancakeHouseMenu pancakeHouseMenu;

// DinerMenu dinerMenu;

// Menu pancakeHouseMenu;

// Menu dinerMenu;

// Menu cafeMenu;

ArrayList menus;

/* //*

*在这个构造器中,女招待照顾两个菜单

  • @param pancakeHouseMenu

  • @param dinerMenu

//

public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) {

this.pancakeHouseMenu = pancakeHouseMenu;

this.dinerMenu = dinerMenu;

}

//*

  • 新增一个cafeMenu

  • @param pancakeHouseMenu

  • @param dinerMenu

  • @param cafeMenu

//

public Waitress(Menu pancakeHouseMenu, Menu dinerMenu, Menu cafeMenu) {

this.pancakeHouseMenu = pancakeHouseMenu;

this.dinerMenu = dinerMenu;

this.cafeMenu = cafeMenu;

}*/

public Waitress(ArrayList menus) {

this.menus = menus;

}

public void printMenu() {

/* Iterator pancakeIterator = pancakeHouseMenu.createIterator();

Iterator dinerIterator = dinerMenu.createIterator();

Iterator cafeIterator = cafeMenu.createIterator();

System.out.println(“MENU\n----\nBREAKFAST”);

printMenu(pancakeIterator);

System.out.println(“\nLUNCH”);

printMenu(dinerIterator);

System.out.println(“\nDINNER”);

printMenu(cafeIterator);*/

Iterator menuIterator = menus.iterator();

while(menuIterator.hasNext()) {

Menu menu = (Menu)menuIterator.next();

printMenu(menu.createIterator());

}

}

private void printMenu(Iterator iterator) {

while(iterator.hasNext()) { //测试是否还有其他项

MenuItem menuItem = (MenuItem) iterator.next();

System.out.print(menuItem.getName() + ", ");

System.out.print(menuItem.getPrice() + " – ");

System.out.println(menuItem.getDescription());

}

}

}

测试的时候我们就可以这样

package dinermerger;

import java.util.ArrayList;

public class MenuTestDrive {

/**

  • @param args

*/

public static void main(String[] args) {

// PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();

// DinerMenu dinerMenu = new DinerMenu();

//

// Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);

//

// waitress.printMenu();

Menu pancakeHouseMenu = new PancakeHouseMenu();

Menu dinerMenu = new DinerMenu();

Menu cafeMenu = new CafeMenu();

// Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu, cafeMenu);

ArrayList menus = new ArrayList();

menus.add(pancakeHouseMenu);

menus.add(dinerMenu);

menus.add(cafeMenu);

Waitress waitress = new Waitress(menus);

waitress.printMenu();

}

}

总算是要讲到组合模式了,这个模式可以解决迭代器模式中不能解决的一个难题。比如我们需要一个新的餐单,菜单下又有子菜单,单单用迭代器就不能解决了。这时候可以考虑到用树形结构来展示整个菜单。

看下面的例子吧:

定义菜单组件

package composite;

/**

  • 定义菜单组件

  • @author Administrator

*/

public abstract class MenuComponent {

public void add(MenuComponent menuComponent) {

throw new UnsupportedOperationException();

}

public void remove(MenuComponent menuComponent) {

throw new UnsupportedOperationException();

}

public MenuComponent getChild(int i) {

throw new UnsupportedOperationException();

}

public String getName() {

throw new UnsupportedOperationException();

}

public String getDescription() {

throw new UnsupportedOperationException();

}

public double getPrice() {

throw new UnsupportedOperationException();

}

public boolean isVegetarian() {

throw new UnsupportedOperationException();

}

public void print() {

throw new UnsupportedOperationException();

}

}

实现菜单

package composite;

import java.util.ArrayList;

import java.util.Iterator;

public class Menu extends MenuComponent{

ArrayList menuComponents = new ArrayList();

String name;

String description;

public Menu(String name, String description) {

this.name = name;

this.description = description;

}

public void add(MenuComponent menuComponent) {

menuComponents.add(menuComponent);

}

public void remove(MenuComponent menuComponent) {

menuComponents.remove(menuComponent);

}

public MenuComponent getChild(int i) {

return (MenuComponent) menuComponents.get(i);

}

public String getName() {

return name;

}

public String getDescription() {

return description;

}

public void print(){

System.out.print(“\n” + getName());

System.out.println(", " + getDescription());

System.out.println(“------------------”);

Iterator iterator = menuComponents.iterator();

while(iterator.hasNext()) {

MenuComponent menuComponent =

(MenuComponent) iterator.next();

menuComponent.print();

}

}

}

**

实现菜单项**

package composite;

/**

  • 实现菜单项

  • @author Administrator

*/

public class MenuItem extends MenuComponent{

String name;

String description;

boolean vegetarian;

double price;

public MenuItem(String name, String description, boolean vegetarian,

double price) {

this.name = name;

this.description = description;

this.vegetarian = vegetarian;

this.price = price;

}

public String getName() {

return name;

}

public String getDescription() {

return description;

}

public boolean isVegetarian() {

return vegetarian;

}

public double getPrice() {

return price;

}

public void print(){

System.out.print(" " + getName());

if(isVegetarian()) {

System.out.print(“(v)”);

}

System.out.println(“,” + getPrice());

System.out.println(" --" + getDescription());

}

}

变得十分快乐的女招待

package composite;

import java.util.Iterator;

public class Waitress {

MenuComponent allMenus;

public Waitress(MenuComponent allMenus) {

this.allMenus = allMenus;

}

public void printMenu() {

allMenus.print();

}

}

我们这样测试

package composite;

import java.util.*;

public class MenuTestDrive {

public static void main(String args[]) {

MenuComponent pancakeHouseMenu =

new Menu(“PANCAKE HOUSE MENU”, “Breakfast”);

MenuComponent dinerMenu =

new Menu(“DINER MENU”, “Lunch”);

MenuComponent cafeMenu =

new Menu(“CAFE MENU”, “Dinner”);

MenuComponent dessertMenu =

new Menu(“DESSERT MENU”, “Dessert of course!”);

MenuComponent coffeeMenu = new Menu(“COFFEE MENU”, “Stuff to go with your afternoon coffee”);

MenuComponent allMenus = new Menu(“ALL MENUS”, “All menus combined”);

allMenus.add(pancakeHouseMenu);

allMenus.add(dinerMenu);

allMenus.add(cafeMenu);

pancakeHouseMenu.add(new MenuItem(

“K&B’s Pancake Breakfast”,

“Pancakes with scrambled eggs, and toast”,

true,

2.99));

pancakeHouseMenu.add(new MenuItem(

“Regular Pancake Breakfast”,

“Pancakes with fried eggs, sausage”,

false,

2.99));

pancakeHouseMenu.add(new MenuItem(

“Blueberry Pancakes”,

“Pancakes made with fresh blueberries, and blueberry syrup”,

true,

3.49));

pancakeHouseMenu.add(new MenuItem(

“Waffles”,

“Waffles, with your choice of blueberries or strawberries”,

true,

3.59));

dinerMenu.add(new MenuItem(

“Vegetarian BLT”,

“(Fakin’) Bacon with lettuce & tomato on whole wheat”,

true,

2.99));

dinerMenu.add(new MenuItem(

“BLT”,

“Bacon with lettuce & tomato on whole wheat”,

false,

2.99));

dinerMenu.add(new MenuItem(

“Soup of the day”,

“A bowl of the soup of the day, with a side of potato salad”,

false,

3.29));

dinerMenu.add(new MenuItem(

“Hotdog”,

“A hot dog, with saurkraut, relish, onions, topped with cheese”,

false,

3.05));

dinerMenu.add(new MenuItem(

“Steamed Veggies and Brown Rice”,

“Steamed vegetables over brown rice”,

true,

3.99));

dinerMenu.add(new MenuItem(

“Pasta”,

“Spaghetti with Marinara Sauce, and a slice of sourdough bread”,

true,

3.89));

dinerMenu.add(dessertMenu);

dessertMenu.add(new MenuItem(

“Apple Pie”,

“Apple pie with a flakey crust, topped with vanilla icecream”,

true,

1.59));

dessertMenu.add(new MenuItem(

“Cheesecake”,

“Creamy New York cheesecake, with a chocolate graham crust”,

true,

1.99));

dessertMenu.add(new MenuItem(

“Sorbet”,

“A scoop of raspberry and a scoop of lime”,

true,

1.89));

cafeMenu.add(new MenuItem(

“Veggie Burger and Air Fries”,

“Veggie burger on a whole wheat bun, lettuce, tomato, and fries”,

true,

3.99));

cafeMenu.add(new MenuItem(

“Soup of the day”,

“A cup of the soup of the day, with a side salad”,

false,

3.69));

cafeMenu.add(new MenuItem(

“Burrito”,

“A large burrito, with whole pinto beans, salsa, guacamole”,

true,

4.29));

cafeMenu.add(coffeeMenu);

coffeeMenu.add(new MenuItem(

“Coffee Cake”,

“Crumbly cake topped with cinnamon and walnuts”,

true,

1.59));

coffeeMenu.add(new MenuItem(

“Bagel”,

“Flavors include sesame, poppyseed, cinnamon raisin, pumpkin”,

false,

0.69));

coffeeMenu.add(new MenuItem(

“Biscotti”,

“Three almond or hazelnut biscotti cookies”,

true,

0.89));

Waitress waitress = new Waitress(allMenus);

waitress.printMenu();

}

}

测试结果:

ALL MENUS, All menus combined

------------------

PANCAKE HOUSE MENU, Breakfast

------------------

K&B’s Pancake Breakfast(v),2.99

–Pancakes with scrambled eggs, and toast

Regular Pancake Breakfast,2.99

–Pancakes with fried eggs, sausage

Blueberry Pancakes(v),3.49

–Pancakes made with fresh blueberries, and blueberry syrup

Waffles(v),3.59

–Waffles, with your choice of blueberries or strawberries

DINER MENU, Lunch

------------------

Vegetarian BLT(v),2.99

–(Fakin’) Bacon with lettuce & tomato on whole wheat

BLT,2.99

–Bacon with lettuce & tomato on whole wheat

Soup of the day,3.29

–A bowl of the soup of the day, with a side of potato salad

Hotdog,3.05

–A hot dog, with saurkraut, relish, onions, topped with cheese

Steamed Veggies and Brown Rice(v),3.99

–Steamed vegetables over brown rice

Pasta(v),3.89

–Spaghetti with Marinara Sauce, and a slice of sourdough bread

DESSERT MENU, Dessert of course!

------------------

Apple Pie(v),1.59

–Apple pie with a flakey crust, topped with vanilla icecream

Cheesecake(v),1.99

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

尾声

面试成功其实都是必然发生的事情,因为在此之前我做足了充分的准备工作,不单单是纯粹的刷题,更多的还会去刷一些Android核心架构进阶知识点,比如:JVM、高并发、多线程、缓存、热修复设计、插件化框架解读、组件化框架设计、图片加载框架、网络、设计模式、设计思想与代码质量优化、程序性能优化、开发效率优化、设计模式、负载均衡、算法、数据结构、高级UI晋升、Framework内核解析、Android组件内核等。

不仅有学习文档,视频+笔记提高学习效率,还能稳固你的知识,形成良好的系统的知识体系。这里,笔者分享一份从架构哲学的层面来剖析的视频及资料分享给大家梳理了多年的架构经验,筹备近6个月最新录制的,相信这份视频能给你带来不一样的启发、收获。

Android进阶学习资料库

一共十个专题,包括了Android进阶所有学习资料,Android进阶视频,Flutter,java基础,kotlin,NDK模块,计算机网络,数据结构与算法,微信小程序,面试题解析,framework源码!

image

大厂面试真题

PS:之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

《2017-2021字节跳动Android面试历年真题解析》

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img
链图片转存中…(img-FWoiHs31-1712761664075)]
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-YeExwgtq-1712761664076)]

尾声

面试成功其实都是必然发生的事情,因为在此之前我做足了充分的准备工作,不单单是纯粹的刷题,更多的还会去刷一些Android核心架构进阶知识点,比如:JVM、高并发、多线程、缓存、热修复设计、插件化框架解读、组件化框架设计、图片加载框架、网络、设计模式、设计思想与代码质量优化、程序性能优化、开发效率优化、设计模式、负载均衡、算法、数据结构、高级UI晋升、Framework内核解析、Android组件内核等。
[外链图片转存中…(img-4q8LqxvH-1712761664076)]

不仅有学习文档,视频+笔记提高学习效率,还能稳固你的知识,形成良好的系统的知识体系。这里,笔者分享一份从架构哲学的层面来剖析的视频及资料分享给大家梳理了多年的架构经验,筹备近6个月最新录制的,相信这份视频能给你带来不一样的启发、收获。

[外链图片转存中…(img-p9qIo23y-1712761664076)]

Android进阶学习资料库

一共十个专题,包括了Android进阶所有学习资料,Android进阶视频,Flutter,java基础,kotlin,NDK模块,计算机网络,数据结构与算法,微信小程序,面试题解析,framework源码!

[外链图片转存中…(img-AaLchRon-1712761664077)]

大厂面试真题

PS:之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

[外链图片转存中…(img-X3ohjIC5-1712761664077)]

《2017-2021字节跳动Android面试历年真题解析》

[外链图片转存中…(img-HiDHa4qi-1712761664077)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-7GvHcGs7-1712761664078)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值