最后
目前已经更新的部分资料:
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
return vegetarian;
}
}
菜单
PancakeHouseMenu
package dinermerger;
import java.util.ArrayList;
/**
-
煎饼屋菜单实现
-
@author wwj
*/
public class PancakeHouseMenu {
ArrayList menuItems;
public PancakeHouseMenu() {
menuItems = new ArrayList();
addItem(“K&B’s Pancake Breakfast”, “Pancakes with scrambled eggs, and toast”, true, 2.99);
addItem(“Regular Pancake Breakfast”, “Pancakes with fried eggs, sausage”, false, 2.99);
addItem(“Blueberry Pancakes”, “Pancakes made with fresh blueberries”, true, 3.49);
addItem(“Waffles”, “Waffles, with your choice of blueberries or strawberries”, true, 3.59);
}
/**
-
创建一个新的菜单项对象,传入每一个变量,然后将它加入到ArrayList中
-
@param name 菜名
-
@param description 叙述
-
@param vegetarian 是否为素食
-
@param price 价格
*/
public void addItem(String name, String description, boolean vegetarian, double price) {
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
menuItems.add(menuItem);
}
/* public ArrayList getMenuItems() {
return menuItems;
}*/
public Iterator createIterator() {
return new PancakeHouseMenuIterator(menuItems);
}
}
DinerMenu
package dinermerger;
/**
-
餐厅菜单
-
@author wwj
*/
public class DinerMenu {
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
MenuItem[] menuItems; //使用一个数组,所以可以控制菜单的长度,并且在取出菜单项的时候,不需要转型
public DinerMenu() {
menuItems = new MenuItem[MAX_ITEMS];
addItem(“Vegetarian BLT”,
“(Fakin’) Bacon with lettuce & tomato on Whole wheat”, true,
2.99);
addItem(“BLT”, “Bacon with lettuce & tomato on Whole wheat”, false,
2.99);
addItem(“Soup of the day”,
“Soup of the day, with a side of potato salad”, false, 3.29);
addItem(“Hotdog”,
“A hot dog, with saurkraut, relish, onions, topped with cheese”,
false, 3.05);
addItem(“Steamed Veggies and Brown Rice”,
“Steamed vegetables over brown rice”, true, 3.99);
addItem(“Pasta”,
“Spaghetti with Marinara Sauce, and a slice of sourdough bread”,
true, 3.89);
}
private void addItem(String name, String description, boolean vegetarian, double price) {
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
if(numberOfItems >= MAX_ITEMS) {
System.err.println(“Sorry, menu is full! Can’t add item to menu”);
} else {
menuItems[numberOfItems] = menuItem;
numberOfItems = numberOfItems + 1;
}
}
/**
-
增加了迭代器之后,这个方法就不需要了
-
@return
*/
/*public MenuItem[] getMenuItems() {
return menuItems;
}*/
/**
- 用来从菜单项数组创建一个DinerMenuIterator,并将它返回给客户
*/
public Iterator createIterator() {
return new DinerMenuIterator(menuItems);
}
}
女招待
package dinermerger;
/**
-
女招待类
-
@author wwj
*/
public class Waitress {
PancakeHouseMenu pancakeHouseMenu;
DinerMenu dinerMenu;
/**
*在这个构造器中,女招待照顾两个菜单
-
@param pancakeHouseMenu
-
@param dinerMenu
*/
public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {
this.pancakeHouseMenu = pancakeHouseMenu;
this.dinerMenu = dinerMenu;
}
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());
}
}
}
测试
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
----
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
例子2
———————————————————————————————————————————————————
利用java.util.Iterator来实现迭代器
提供一个Menu接口,让PancakeHouseMenu和DinerMenu都实现Menu接口,这样Waitress就不用依赖具体的菜单的问题,也不用依赖菜单项的具体实现了。
package dinermerger;
/**
-
这是一个简单的接口,让客户能够取得菜单项
-
@author Administrator
*/
public interface Menu {
public Iterator createIterator();
}
DinerMenu
package dinermerger;
import java.util.Iterator;
/**
-
餐厅菜单
-
@author wwj
*/
public class DinerMenu implements Menu{
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
MenuItem[] menuItems; //使用一个数组,所以可以控制菜单的长度,并且在取出菜单项的时候,不需要转型
public DinerMenu() {
menuItems = new MenuItem[MAX_ITEMS];
addItem(“Vegetarian BLT”,
“(Fakin’) Bacon with lettuce & tomato on Whole wheat”, true,
2.99);
addItem(“BLT”, “Bacon with lettuce & tomato on Whole wheat”, false,
2.99);
addItem(“Soup of the day”,
“Soup of the day, with a side of potato salad”, false, 3.29);
addItem(“Hotdog”,
“A hot dog, with saurkraut, relish, onions, topped with cheese”,
false, 3.05);
addItem(“Steamed Veggies and Brown Rice”,
“Steamed vegetables over brown rice”, true, 3.99);
addItem(“Pasta”,
“Spaghetti with Marinara Sauce, and a slice of sourdough bread”,
true, 3.89);
}
private void addItem(String name, String description, boolean vegetarian, double price) {
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
if(numberOfItems >= MAX_ITEMS) {
System.err.println(“Sorry, menu is full! Can’t add item to menu”);
} else {
menuItems[numberOfItems] = menuItem;
numberOfItems = numberOfItems + 1;
}
}
/**
-
增加了迭代器之后,这个方法就不需要了
-
@return
*/
/*public MenuItem[] getMenuItems() {
return menuItems;
}*/
/**
- 用来从菜单项数组创建一个DinerMenuIterator,并将它返回给客户
*/
public Iterator createIterator() {
return new DinerMenuIterator(menuItems);
}
}
PancakeHouseMenu
不创建自己的迭代器,而是调用ArrayList的iterator()方法
package dinermerger;
import java.util.ArrayList;
import java.util.Iterator;
/**
-
煎饼屋菜单实现
-
@author wwj
*/
public class PancakeHouseMenu implements Menu{
ArrayList menuItems;
public PancakeHouseMenu() {
menuItems = new ArrayList();
addItem(“K&B’s Pancake Breakfast”, “Pancakes with scrambled eggs, and toast”, true, 2.99);
addItem(“Regular Pancake Breakfast”, “Pancakes with fried eggs, sausage”, false, 2.99);
addItem(“Blueberry Pancakes”, “Pancakes made with fresh blueberries”, true, 3.49);
addItem(“Waffles”, “Waffles, with your choice of blueberries or strawberries”, true, 3.59);
}
/**
-
创建一个新的菜单项对象,传入每一个变量,然后将它加入到ArrayList中
-
@param name 菜名
-
@param description 叙述
-
@param vegetarian 是否为素食
-
@param price 价格
*/
public void addItem(String name, String description, boolean vegetarian, double price) {
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
menuItems.add(menuItem);
}
/* public ArrayList getMenuItems() {
return menuItems;
}*/
public Iterator createIterator() {
return menuItems.iterator();
}
}
DinerMenuIterator
package dinermerger;
import java.util.Iterator;
/**
-
实现一个具体的迭代器,为餐厅菜单服务
-
@author Administrator
*/
public class DinerMenuIterator implements Iterator {
MenuItem[] items;
int position = 0; //记录当前数组遍历的位置
public DinerMenuIterator(MenuItem[] items) {
this.items = items;
}
@Override
public boolean hasNext() {
if(position >= items.length || items[position] == null) {
return false;
} else {
return true;
}
}
@Override
public Object next() {
MenuItem menuItem = items[position];
position = position + 1;
return menuItem;
}
@Override
public void remove() {
if(position <= 0) {
throw new IllegalStateException(“You can’t remove an item until you’ve done at least on next()”);
}
if(items[position - 1] != null) {
for(int i = position - 1; i < (items.length - 1); i++) {
items[i] = items[i + 1];
}
items[items.length - 1] = null;
}
}
}
Waitress
package dinermerger;
import java.util.Iterator;
/**
-
女招待类
-
@author wwj
*/
public class Waitress {
// PancakeHouseMenu pancakeHouseMenu;
// DinerMenu dinerMenu;
Menu pancakeHouseMenu;
Menu dinerMenu;
/**
*在这个构造器中,女招待照顾两个菜单
-
@param pancakeHouseMenu
-
@param dinerMenu
*/
public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) {
this.pancakeHouseMenu = pancakeHouseMenu;
this.dinerMenu = dinerMenu;
}
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();
最后
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的24套腾讯、字节跳动、阿里、百度2019-2021BAT 面试真题解析,我把大厂面试中常被问到的技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。
还有 高级架构技术进阶脑图 帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
Android 基础知识点
Java 基础知识点
Android 源码相关分析
常见的一些原理性问题
希望大家在今年一切顺利,进到自己想进的公司,共勉!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
s.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();
最后
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的24套腾讯、字节跳动、阿里、百度2019-2021BAT 面试真题解析,我把大厂面试中常被问到的技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。
还有 高级架构技术进阶脑图 帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
Android 基础知识点
Java 基础知识点
Android 源码相关分析
常见的一些原理性问题
[外链图片转存中…(img-oqD01Dfd-1715472466778)]
希望大家在今年一切顺利,进到自己想进的公司,共勉!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!