阿里云【名师课堂】Java面向对象开发106:【第05个代码模型】综合案例:宠物商店

106:【第05个代码模型】综合案例:宠物商店

需求:

  • 建立一家宠物商店,店里可以进行宠物的上架、下架、关键字查询。只要求描述出程序的关系。
  • 宠物的属性:名字、年龄、毛色。

分析:

  • 一家宠物商店会有多种宠物,按照设计表关系来说属于一对多关系映射。
    • 宠物商店是,宠物是,那么问题来了,宠物也分很多种,怎么办?
    • 宠物应该有自己的标准(猫、狗、鱼等),不符合标准的不是宠物(盆栽、奶茶等)。因此宠物应该是一个接口,猫、狗、鱼等是具体实现、是子类。
    • 既然一家宠物商店需要描述多种宠物,而数量又不固定,要使用到链表类,通过链表存放多个信息。
  • 所谓下架操作,就是链表中的删除,而删除操作需要equals()的支持。
    在这里插入图片描述

0、编写Link类

Link类还是第105节讲解的简单Link类。详情参见:阿里云【名师课堂】Java面向对象开发97 ~ 105:链表

1、建立宠物标准(接口)

接口只关心如何取得数据。

interface IPet {  // 定义宠物标准
	public String getName() ;
	public int getAge() ;
	public String getColor() ;
}

2、定义宠物商店类

对于宠物商店,只关注宠物的标准,而不关心具体是哪种宠物

class Shop {
	private Link pets = new Link() ;  // 开辟一个链表,保存多个宠物
	public void add(IPet pet) {  // 不关心是哪个宠物(子类),只关心“宠物”这个概念
		this.pets.add(pet) ;  // 上架就是向链表中保存数据
	}
	public void delete(IPet pet) {
		this.pets.remove(pet) ;  // 下架就是从链表中删除数据
	}
	public Link getPets(IPet pet) {  // 取得全部宠物
		return this.pets ;  // 上架就是向链表中保存数据
	}
	public Link search(String keyWord) {  // 关键字查找
		Link results = new Link() ;  // 查询到的结果一定是多个宠物,所以返回值是Link类
		Object [] data = this.pets.toArray() ;
		for (int x = 0 ; x < data.length ; x++) {
			IPet pet = (IPet)data[x] ;  // 只有转型成Pet类才可以调用接口中的三个属性
			if (pet.getName().contains(keyWord) || pet.getColor().contains(keyWord)) {
				results.add(pet) ;
			}
		}
		return results ;
	}
}

3、定义宠物狗

class DogImpl implements IPet {
	private String name ;
	private int age ;
	private String color ;
	public DogImpl(String name, int age, String color) {  // 构造
		this.name = name ;
		this.age = age ;
		this.color = color ;
	}
	public String getName() {  // 接口方法覆写
		return this.name ;
	}
	public int getAge() {  // 接口方法覆写
		return this.age ;
	}
	public String getColor() {  // 接口方法覆写
		return this.color ;
	}
	public boolean equals(Object obj) {  // 覆写equals类
		if (obj == null) {
			return false ;
		}
		if (this == obj) {
			return true ;
		}
		if (!(obj instanceof DogImpl)) {
			return false ;
		}
		DogImpl dog = (DogImpl)obj ;
		return this.name.equals(dog.name) && this.age == dog.age && this.color.equals(dog.color) ;
	}
	public String toString() {
		return "【狗】名字:" + this.name + ",年龄:" + this.age + ",毛色:" + this.color ;
	}
}

3、定义宠物猫

class CatImpl implements IPet {
	private String name ;
	private int age ;
	private String color ;
	public CatImpl(String name, int age, String color) {  // 构造
		this.name = name ;
		this.age = age ;
		this.color = color ;
	}
	public String getName() {  // 接口方法覆写
		return this.name ;
	}
	public int getAge() {  // 接口方法覆写
		return this.age ;
	}
	public String getColor() {  // 接口方法覆写
		return this.color ;
	}
	public boolean equals(Object obj) {  // 覆写equals类
		if (obj == null) {
			return false ;
		}
		if (this == obj) {
			return true ;
		}
		if (!(obj instanceof CatImpl)) {
			return false ;
		}
		CatImpl cat = (CatImpl)obj ;
		return this.name.equals(cat.name) && this.age == cat.age && this.color.equals(cat.color) ;
	}
	public String toString() {
		return "【猫】名字:" + this.name + ",年龄:" + this.age + ",毛色:" + this.color ;
	}
}

4、编写测试程序

public class PetShop {
	public static void main(String args[]) throws Exception {
		Shop petshop = new Shop() ;
		petshop.add(new DogImpl("卡斯罗", 2, "黑色")) ;
		petshop.add(new DogImpl("金毛", 4, "黄色")) ;
		petshop.add(new DogImpl("阿拉斯加", 3, "黑白")) ;
		petshop.add(new DogImpl("柴犬", 3, "黄白")) ;
		petshop.add(new CatImpl("暹罗猫", 1, "小黑脸")) ;
		petshop.add(new CatImpl("斯芬克斯猫", 1, "无毛")) ;
		petshop.add(new CatImpl("加菲猫", 1, "黄色")) ;
		
		Link all = petshop.search("黑") ;
		Object[] result = all.toArray() ;
		for (int i = 0 ; i < result.length ; i++) {
			System.out.println(result[i]) ;
		}
		System.out.println("==========================") ;
		
		petshop.delete(new DogImpl("卡斯罗", 2, "黑色")) ;
		Link allA = petshop.search("黑") ;
		Object[] resultA = allA.toArray() ;
		for (int i = 0 ; i < resultA.length ; i++) {
			System.out.println(resultA[i]) ;
		}
	}
}

在这里插入图片描述
完成代码:https://github.com/colderThyKiss/LinkedList.git中的PetShop.java

总结

实际上这种形式的代码在生活中处处可见:

  • 公园里有多种绿植;
  • 动物园有多种动物;
  • 衣柜中有多种衣服;

类比于这个宠物商店的示例代码,记住:以后在进行代码开发的过程中一切都以接口设计为主。

posted @ 2020-07-04 01:17  溺水的情书  阅读(286)  评论(0编辑  收藏  举报