java公车系统_今天早上公交车上想到的系统的实现模式

今天早上公交车上想到的系统的实现模式

Author

:zhyiwww

Date

:2007-1-16

转载请注明出处

(copyright by @ zhangyi)

今天早上去上班的时候,在公交车上,我想了一下我们的一部分的实现方案。

我们有一个对象

User

,定义如下:

public class User{

name;

password;

set***

set***

get***

get***

}

这是一个单独的

bean

因为我们再传递密码的时候是需要加密和解密的。

但是,我们的加密和解密方案可能是会改变的。

那么我们如何来实现呢?

所以我就想,我可以用一个灵活的,可扩展的方案来实现。

我声明了一个接口:

Public interface Security{

//

加密

Void Encrypt();

//

解密

Void crack();

}

我让上面的

bean

来实现此接口:

public class User

implements Security

{

name;

password;

set***

set***

get***

get***

//

加密

Void Encrypt(){

}

//

解密

Void crack(){

}

}

在此,我们可以看到,加密解密的部分,我们是要自己去实现的。

但是,实现对数据的加密和解密还是要去调用这个方法。

所以我们在其初始化的时候就实现加密:

public class Userimplements Security{

name;

password;

public User(String username,String password){

this.name=username;

this.password=password;

//

password

进行加密

encrypt();

}

set***

set***

get***

get***

//

加密

Void Encrypt(){

}

//

解密

Void crack(){

}

}

但是加密如何实现呢?我们加密可能会采用不同的方案。

那么一种方案是

,我们可以给没有一个方案单独的创建类,然后去实现加密和解密的方法。

这种方案,我们在实现的时候,使用哪一种方案都要去修改所有的方案,去实现对应的代码。可能工作量很大。

另一种方案就是

,使用工厂。

我们使用工厂来决定和实现使用哪种方案。然后我们重建不同的方案来实现其功能。

可以通过配置,来决定使用哪种方案。这样,我们的系统的自由度就大了很多了。

至于工厂详细如何实现,在此我就不多说了。

|----------------------------------------------------------------------------------------|

版权声明  版权所有 @zhyiwww

引用请注明来源 http://www.blogjava.net/zhyiwww

|----------------------------------------------------------------------------------------|

posted on 2007-01-16 11:35 zhyiwww 阅读(1352) 评论(0)  编辑  收藏 所属分类: j2ee

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的Java实现。首先,我们需要定义一个带权有向图类来表示区域的公交线路: ```java import java.util.*; class Graph { private Map<String, List<Edge>> graph; public Graph() { this.graph = new HashMap<>(); } public void addVertex(String vertex) { this.graph.put(vertex, new ArrayList<>()); } public void addEdge(String source, String destination, int weight) { List<Edge> edges = this.graph.get(source); edges.add(new Edge(source, destination, weight)); } public List<Edge> getEdges(String source) { return this.graph.get(source); } public Set<String> getVertices() { return this.graph.keySet(); } public static class Edge { public String source; public String destination; public int weight; public Edge(String source, String destination, int weight) { this.source = source; this.destination = destination; this.weight = weight; } } } ``` 然后我们可以定义一个重要场所类来表示区域中的重要场所: ```java class Place { private String name; private List<String> busRoutes; public Place(String name) { this.name = name; this.busRoutes = new ArrayList<>(); } public void addBusRoute(String route) { this.busRoutes.add(route); } public List<String> getBusRoutes() { return this.busRoutes; } public String getName() { return this.name; } } ``` 接下来,我们可以定义一个公交线路咨询类来实现咨询功能和查询重要场所信息的功能: ```java class BusRouteConsultant { private Graph graph; private Map<String, Place> places; public BusRouteConsultant() { this.graph = new Graph(); this.places = new HashMap<>(); } public void addPlace(String name) { Place place = new Place(name); this.places.put(name, place); } public void addBusRoute(String source, String destination, int weight) { this.graph.addVertex(source); this.graph.addVertex(destination); this.graph.addEdge(source, destination, weight); } public void addBusRouteToPlace(String source, String destination, int weight, String placeName) { addBusRoute(source, destination, weight); Place place = this.places.get(placeName); place.addBusRoute(source + "->" + destination); } public List<Graph.Edge> getBusRoutes(String source, String destination) { // 使用 Dijkstra 算法求最短路径 Map<String, Integer> distances = new HashMap<>(); Map<String, Graph.Edge> previous = new HashMap<>(); PriorityQueue<String> queue = new PriorityQueue<>(Comparator.comparingInt(distances::get)); for (String vertex : this.graph.getVertices()) { if (vertex.equals(source)) { distances.put(vertex, 0); } else { distances.put(vertex, Integer.MAX_VALUE); } previous.put(vertex, null); queue.add(vertex); } while (!queue.isEmpty()) { String current = queue.poll(); if (current.equals(destination)) { break; } List<Graph.Edge> edges = this.graph.getEdges(current); for (Graph.Edge edge : edges) { int distance = distances.get(current) + edge.weight; if (distance < distances.get(edge.destination)) { distances.put(edge.destination, distance); previous.put(edge.destination, edge); queue.remove(edge.destination); queue.add(edge.destination); } } } // 构造最短路径 List<Graph.Edge> path = new ArrayList<>(); Graph.Edge edge = previous.get(destination); while (edge != null) { path.add(0, edge); edge = previous.get(edge.source); } return path; } public void printPlaceInfo(String placeName) { Place place = this.places.get(placeName); System.out.println("场所名称:" + place.getName()); System.out.println("经过的公交线路:"); for (String route : place.getBusRoutes()) { System.out.println(route); } } } ``` 最后,我们可以使用公交线路咨询类来构建区域的公交线路和重要场所信息: ```java public static void main(String[] args) { BusRouteConsultant consultant = new BusRouteConsultant(); // 添加场所 consultant.addPlace("学校"); consultant.addPlace("医院"); consultant.addPlace("商场"); consultant.addPlace("公园"); consultant.addPlace("火车站"); consultant.addPlace("机场"); consultant.addPlace("体育馆"); consultant.addPlace("博物馆"); consultant.addPlace("图书馆"); consultant.addPlace("电影院"); // 添加公交线路 consultant.addBusRouteToPlace("学校", "医院", 5, "医院"); consultant.addBusRouteToPlace("学校", "商场", 10, "商场"); consultant.addBusRouteToPlace("学校", "公园", 15, "公园"); consultant.addBusRouteToPlace("学校", "火车站", 20, "火车站"); consultant.addBusRouteToPlace("学校", "机场", 25, "机场"); consultant.addBusRouteToPlace("医院", "体育馆", 10, "体育馆"); consultant.addBusRouteToPlace("商场", "博物馆", 15, "博物馆"); consultant.addBusRouteToPlace("公园", "图书馆", 20, "图书馆"); consultant.addBusRouteToPlace("火车站", "电影院", 25, "电影院"); // 查询公交线路 List<Graph.Edge> path = consultant.getBusRoutes("学校", "博物馆"); for (Graph.Edge edge : path) { System.out.println(edge.source + " -> " + edge.destination); } // 查询重要场所信息 consultant.printPlaceInfo("医院"); } ``` 这段代码将会输出以下内容: ``` 学校 -> 医院 医院 -> 体育馆 商场 -> 博物馆 场所名称:医院 经过的公交线路: 学校->医院 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值