软构第三次实验

软构第三次实验

1 实验目标概述
2 实验环境配置
3 实验过程
3.1 待开发的三个应用场景
3.2 面向可复用性和可维护性的设计:PlanningEntry
3.2.1 PlanningEntry的共性操作
3.2.2 局部共性特征的设计方案
3.2.3 面向各应用的PlanningEntry子类型设计(个性化特征的设计方案)
3.3 面向复用的设计:R
3.4 面向复用的设计:Location
3.5 面向复用的设计:Timeslot
3.6 面向复用的设计:EntryState及State设计模式
3.7 面向应用的设计:Board
3.8 Board的可视化:外部API的复用
3.9 PlanningEntryCollection的设计
3.10 可复用API设计及Façade设计模式
3.10.1 检测一组计划项之间是否存在位置独占冲突
3.10.2 检测一组计划项之间是否存在资源独占冲突
3.10.3 提取面向特定资源的前序计划项
3.11 设计模式应用
3.11.1 Factory Method
3.11.2 Iterator
3.11.3 Strategy
3.12 应用设计与开发
3.12.1 航班应用
3.12.2 高铁应用
3.12.3 进程应用
3.12.4 课表应用
3.12.5 学习活动应用

1 实验目标概述
本次实验覆盖课程第 3、4、5 章的内容,目标是编写具有可复用性和可维护性的软件,主要使用以下软件构造技术:
子类型、泛型、多态、重写、重载
继承、代理、组合
常见的 OO 设计模式
语法驱动的编程、正则表达式
基于状态的编程
API 设计、API 复用

本次实验给定了五个具体应用(高铁车次管理、航班管理、操作系统进程管理、大学课表管理、学习活动日程管理),学生不是直接针对五个应用分别编程实现,而是通过 ADT 和泛型等抽象技术,开发一套可复用的 ADT 及其实现,充分考虑这些应用之间的相似性和差异性,使 ADT 有更大程度的复用(可复用性)和更容易面向各种变化(可维护性)。

2 实验环境配置

3 实验过程

3.1 待开发的三个应用场景
选定的三个应用。
1.航班管理
2.高铁管理
3.课程管理
异同点:
1.起止时间均在创建时确定,确定后不可更改
2.所用资源均为可区分资源,但使用资源数不同
3.地点数不同。且课程所用地点不可共享
4.仅有火车可阻塞

3.2 面向可复用性和可维护性的设计:PlanningEntry
计划项是一个状态可变的ADT,它保存有一个计划项的时间、地点、资源等有效信息。为了保证可复用性,将PlanningEntry设计为接口。

3.2.1 PlanningEntry的共性操作

/**
	 * To allocate resources to planning entry.
	 * @param resources
	 * @return true if succeed allocating.
	 */
	public boolean allocate(List<R> resources);
	
	/**
	 * To start the planning entry.
	 * @return true if success.
	 */
	public boolean start();
	
	/**
	 * To cancel the planning entry.
	 * @return true if success.
	 */
	public boolean cancel();
	
	/**
	 * To get the name of the planning entry.
	 * @return the name of the planning entry.
	 */
	public String getName();
	
	/**
	 * To get the state of the planning entry.
	 * @return the state of the planning entry.
	 */
	public String getState();
	
	/**
	 * To new a location.
	 * @param name the name of the location.
	 * @param longitute the longitute of the location.
	 * @param latitute the latitute of the location.
	 * @param isShare if the location can be share.
	 * @return a new location.
	 */
	public Location inputLocation(String name,float longitute,
			float latitute,boolean isShare);
	
	/**
	 * To new a time.
	 * @param year year of the time.
	 * @param month month of the time.
	 * @param day day of the time.
	 * @param hour hour of the time.
	 * @param min min of the time.
	 * @return a new time.
	 */
	public Time inputTime(int year,int month,int day,
			int hour,int min);
	
	/**
	 * To new a timeslot.
	 * @param start when the timeslot start.
	 * @param finish when the timeslot end.
	 * @return a new timeslot.
	 */
	public Timeslot inputTimeslot(Time start,Time finish);
	
	/**
	 * To get a list of locations.
	 * @return a list of locations.
	 */
	public List<Location> getLocations();
	
	/**
	 * To get a list of resources.
	 * @return a list of resources.
	 */
	public List<R> getResources();
	
	/**
	 * To get a list of timeslots.
	 * @return a list of timeslots.
	 */
	public List<Timeslot> getTimeslots();
	
	/**
	 * To change the locations of the planning entry.
	 * @param newloc the new locations.
	 * @return true if succeess.
	 */
	public boolean changeLoc(List<Location> newloc);
	
	/**
	 * To change the resources of the planning entry.
	 * @param newR the new resource.
	 * @return true if succeess.
	 */
	public boolean changeR(List<R> newR);

3.2.2 局部共性特征的设计方案
采取方案二。
局部共性的地方单独放入底层。

3.2.3 面向各应用的PlanningEntry子类型设计(个性化特征的设计方案)
三个子类型的不同主要在于两方面:Location、Timeslot、Resource等信息的存储模式和信息的修改。
为了使三个应用的可复用性尽量提升,对每一个属性都采用List的方式保存。在实例化的时候具体区分。
1.航班管理

public class FlightEntry extends CommonPlanningEntry<Plane>

/**
	 * Mutable.
	 * Abstraction function:
	 *   (name,resources,locations,timeslot)={FlightEntry| a flight entry}
	 * Representation invariant:
	 *   size of resources is 1.
	 *   size of locations is 2.
	 *   size of timeslot is 1.
	 * Safety from rep exposure:
	 *   All fields are private
	 */

public FlightEntry(String name,List<Plane> resources,
			    List<Location> locations,List<Timeslot> timeslot) throws Exception {
		
		this.name=name;
		this.resources=resources;
		this.locations=locations;
		this.timeslot=timeslot;
		this.state=new State();
		if(!checkRep())
		{
			throw new Exception("Wrong imformation");
		}
	}

boolean checkRep() {
		if (this.name == null)
			return false;
		if (locations.size() != 2)
			return false;
		if (timeslot.size() != 1)
			return false;
		return true;
	}

2.高铁管理

public class TrainEntry extends CommonPlanningEntry<Train>

	/**
	 * Mutable.
	 * Abstraction function:
	 *   (name,resources,locations,timeslot)={TrainEntry| a train entry}
	 * Representation invariant:
	 *   size of locations is at least 2.
	 *   size of timeslot is 1 less then size of locations.
	 * Safety from rep exposure:
	 *   All fields are private
	 */

public TrainEntry(String name,List<Train> resources,
			    List<Location> locations,List<Timeslot> timeslot) throws Exception {
		
		this.name=name;
		this.resources=resources;
		this.locations=locations;
		this.timeslot=timeslot;
		this.state=new BlockAbleState();
		if(!checkRep())
		{
			throw new Exception("Wrong imformation");
		}
	}

	boolean checkRep() {
		if (this.name == null)
			return false;
		if (locations.size() < 2 || locations.size() != timeslot.size()-1)
			return false;
		if (timeslot.size() < 1)
			return false;
		return true;
	}

3.课程管理

public class CourseEntry extends CommonPlanningEntry<Teacher>

/**
	 * Mutable.
	 * Abstraction function:
	 *   (name,resources,locations,timeslot)={CourseEntry| a course entry}
	 * Representation invariant:
	 *   size of resources is 1.
	 *   size of locations is 1.
	 *   size of timeslot is 1.
	 * Safety from rep exposure:
	 *   All fields are private
	 */

public CourseEntry(String name, List<Teacher> resources, List<Location> locations,
			List<Timeslot> timeslot) throws Exception {
		this.name = name;
		this.resources = resources;
		this.locations = locations;
		this.timeslot = timeslot;
		this.state = new State();
		if (!checkRep()) {
			throw new Exception("Wrong imformation");
		}
	}

boolean checkRep() {
		if (this.name == null)
			return false;
		if (this.locations.size() != 1)
			return false;
		if (this.timeslot.size() != 1 )
			return false;
		return true;
	}

3.3 面向复用的设计:R
Plane:
public class Plane

/**
* Immutable.
* Abstraction function:
* (company,type,capacity)={plane| a new plane}
* Representation invariant:
* capacity is more than 0.
* Safety from rep exposure:
* All fields are private
*/
构造器:
public Plane(String company,String type,int capacity)
功能:新建一个plane

boolean checkRep(){
	if (this.capacity < 0)
		return false;
	return true;
}

属性:
private String type;
private String company;
private int capacity;

方法:
public String getType()
功能:获取飞机的参数

public String getCompany()
功能:获取飞机的参数

public int getCapacity()
功能:获取飞机的参数

public String toString()
功能:打印飞机的数据

Train:
public class Train

/**
* Immutable.
* Abstraction function:
* (type,number,capacity)={train| a new train combine of cars}
* Representation invariant:
* capacity is more than 0.
* number is uniqe.
* Safety from rep exposure:
* All fields are private
*/

属性:
private String type;
private int number;
private int capacity;

构造器:
public Train(String type,int number,int capacity)

方法:
public String getType()
功能:获取火车的参数

public String getNumber()
功能:获取火车的参数

public int getCapacity()
功能:获取火车的参数

public String toString()
功能:打印火车的数据

Teacher:
public class Teacher

/**
 * Immutable.
 * Abstraction function:
 *   (name,id,gender,jobTitle)={teacher| a new teacher}
 * Representation invariant:
 *   id is not -1.
 *   gender is "male" or "female".
 * Safety from rep exposure:
 *   All fields are private
 */

属性:
private String name;
private int id;
private String gender;
private String jobTitle;

构造器:
public Teacher(String name,int id,String gender,String jobTitle)

方法:
public String getName()
功能:获取老师的信息

public String getId()
功能:获取老师的信息

public int getGender()
功能:获取老师的信息

public String getJobTitle()
功能:获取老师的信息

public String toString()
功能:打印老师的数据

3.4 面向复用的设计:Location
public class Location

/**
 * Immutable.
 * Abstraction function:
 *   (name,longitute,latitute,isShare)={location| a new location}
 * Representation invariant:
 *   name is not null.
 *   longitute is more than 0 and less than 180.
 *   latitute is more than 0 and less than 90.
 * Safety from rep exposure:
 *   All fields are private
 */

属性:
private String name;
private float longitute;
private float latitute;
private boolean isShare;

构造器:
public Location(String name,float longitute,
float latitute,boolean isShare)

方法:
public String getName()
功能:获得location的名字

3.5 面向复用的设计:Timeslot
public class Time

/**
* Imutable.
* Abstraction function:
* (year,month,day,hour,min)={time| a new time}
* Representation invariant:
* month is more than 0 and less than 13.
* day is more than 0 and less than 32.
* hour is more than 0 and less than 24.
* min is more than 0 and less than 60.
* Safety from rep exposure:
* All fields are private
*/

private int year=-1;
private int month=-1;
private int day=-1;
private int hour=-1;
private int min=-1;

public class Timeslot

/**
 * Imutable.
 * Abstraction function:
 *   (start,finish)={timeslot| a new timeslot}
 * Representation invariant:
 *   start is earlier than finish.
 * Safety from rep exposure:
 *   All fields are private
 */

属性:
private Time start;
private Time finish;

构造器:
public Timeslot(Time start,Time finish)

3.6 面向复用的设计:EntryState及State设计模式
State抽象类:
public interface EntryState {

public String getState();

public boolean cancel(State state);

public boolean next(State state);

}

具体state:
public class Waiting implements EntryState

public class Allocated implements EntryState

public class Running implements EntryState

public class Cancelled implements EntryState

public class Blocked implements EntryState

public class Ended implements EntryState

状态管理器:
public class State

属性:
private EntryState state=null;

构造器:
public State(){
this.state=new Waiting();
}

方法:
/**
* to get state now.
* @return
*/
public EntryState getState()

 /**
  * to set state
  * @param state
  */
 public void setState(EntryState state) 

 /**
  * to start the state
  * @return true if success.
  */
 public boolean run() 

特殊状态管理器(可block):
public class BlockAbleState extends State

新增方法:
public void block( )

3.7 面向应用的设计:Board
以FlightBoard为例:

public class FlightBoard {

private Time now=null;
private Location location =null;
private List<FlightEntry> entries= new ArrayList<>();


public FlightBoard(Time now,Location location,List<FlightEntry> allEntries) 

boolean checkRep() {
	if (location==null)
		return false;
	return true;
}

public void hashcode() 

public void visualize() 

3.8 Board的可视化:外部API的复用

3.9 PlanningEntryCollection的设计

3.10 可复用API设计及Façade设计模式

3.10.1 检测一组计划项之间是否存在位置独占冲突
要检测一组计划项之间是否存在位置冲突,主要应该检测每一个位置的若干计划项是否有时间冲突。
首先判断是否使用统一位置,并剔除不使用同一位置的计划项,挑选出使用同一位置的计划项进行下一步判断。
List check=entries;
for (;!check.isEmpty()😉 {
List find=new ArrayList<>();;
for(int j=1;j<check.size();j++) {
if (check.get(0).getLocations()==
check.get(j).getLocations()) {
find.add(check.get(j));
check.remove(j);
j–;
}
}
接下来通过判断find是否为空,来判断是否有使用同一地点的计划项,若find不为空,则有,接下来判断时间是否冲突
if (!find.isEmpty()) {
find.add(check.get(0));
for (;!find.isEmpty()😉 {
for(int j=1;j<find.size();j++) {
if (find.get(0).compareTime(find.get(j))1) {
flag=true;
break;
}
}
find.remove(0);
if(flag
true)
break;
}
}
check.remove(0);
if(flag==true)
break;
找到退出循环。找不到删除当前计划项后继续寻找。

3.10.2 检测一组计划项之间是否存在资源独占冲突
在Collection中,用一个List存储所有的计划项;因此在Board中,迭代器的方法该存储计划项的list.iterator()。
public Iterator<PlanningEntry> iterator() {
return planningEntryCollection.getAllPlanningEntries().iterator();
}
public int compare(PlanningEntry o1, PlanningEntry o2) {
return ((FlightSchedule) o1).getTimeLeaving()
.isBefore(((FlightSchedule) o2).getTimeArrival()) ? -1 : 1;
}

3.10.3 提取面向特定资源的前序计划项
/**

  • For Activity Calendar
  • check locations of planning entry in entries if they are conflicted
  • @param entries
  • @return true if there are locations conflict
    */
    public abstract boolean
    checkLocationConflict(List<PlanningEntry> entries);

public class PlanningEntryAPIsFirst extends PlanningEntryAPIs {
@Override
public boolean checkLocationConflict(List<PlanningEntry> entries) {
……
}

public class PlanningEntryAPIsSecond extends PlanningEntryAPIs {
@Override
public boolean checkLocationConflict(List<PlanningEntry> entries) {
……
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值