1、要求:某系统日志记录器要求支持多种日志记录方式,如文件记录、数据库记录等;用户可以根据要求动态选择日志记录方式。使用Factory模式来设计;如果系统中增加一个日志记录方式—控制台日志记录(ConsoleLog),类图将如何改变?
类图:
代码:
public class Client {
public static void main(String[] args) {
try {
Log log;
LogFactory factory2;
factory2 = new FileLogFactory();
log = factory2.createLog();
log.writeLog();
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
public class DatabaseFile implements Log{
public void writeLog() {
System.out.println("数据库日志写入中。。。");
}
}
public class DatabaseLogFactory implements LogFactory{
public Log createLog() {
System.out.println("数据库日志工厂生产数据库日志。");
return new DatabaseFile();
}
}
public class FileLog implements Log{
public void writeLog() {
System.out.println("文件日志写入中。。。");
}
}
public class FileLogFactory implements LogFactory{
public Log createLog() {
System.out.println("文件日志工厂生产文件日志。");
return new FileLog();
}
}
public interface Log {
public void writeLog();
}
public interface LogFactory {
public Log createLog();
}
添加控制台日志记录(ConsoleLog)的类图:
3、要求:KFC套餐是一个复杂对象,一般包括主食(如汉堡、鸡肉卷等)和饮料(如果汁、可乐等)组成部分,不同套餐有不同组成部分;KFC服务员要根据顾客要求,装配这些组成部分,构造一个完整套餐,返回给顾客;使用Builder模式来设计。
类图:
代码:
public class Meal()
{
private string food;
private string drink;
public void setFood(string food)
{
this.food = food;
}
public string getFood()
{
return food;
}
public void setDrink(string drink)
{
this.drink = drink;
}
public string getDrink()
{
return drink;
}
}
public abstract class MealBulider()
{
private Meal meal = new Meal();
abstract void buildFood();
abstract void buildDrink();
public Meal getMael()
{
return meal;
}
}
public class MealABuilder extends MealBulider()
{
pubilc void buildFood()
{
super.meal.setFood("一个汉堡");
}
public void buildFrink()
{
super.meal.setDrink("一杯可乐");
}
}
public class MealBBuilder extends MealBulider()
{
pubilc void buildFood()
{
super.meal.setFood("一个鸡肉卷");
}
public void buildFrink()
{
super.meal.setDrink("一杯柠檬汁");
}
}
public class KFCWaiter()
{
private MealBulider mealBuilder;
public void setMealBuilder(MealBuilder mealBuilder)
{
this.mealBuilder = mealBuilder;
}
public Meal construct()
{
mealBuilder.buildFood();
mealBuilder.buildDrink();
return mealBuilder.getMeal();
}
}
public class Client
{
public static void main(String[] args)
{
KFCWaiter waiter = new KFCWaiter();
MealBuilder a = new MealABuilder();
waiter.setMealBuilder(a);
Meal mealA = waiter.constuct();
System.out.println("套餐组合:");
System.out.println(mealA.getFood()+"--"+mealA.getDrink());
}
}
4.要求:游戏中的地图:包括天空、地面、背景;人物包括人体、服装、装备等组成部分,如采用Builder模式如何设计?
类图:![](http://images2017.cnblogs.com/blog/1249633/201711/1249633-20171108191253841-1783343668.png)
代码:
public class Map()
{
private string sky;
private string ground;
private string background;
public void setSky(string sky)
{
this.sky = sky;
}
public string getSky()
{
return sky;
}
public void setGround(string ground)
{
this.ground = ground;
}
public string getGround()
{
return ground;
}
public void setBackGround(string background)
{
this.background = background;
}
public string getBackGround()
{
return background;
}
}
public abstract class MapBulider()
{
private Map map = new Map();
abstract void buildSky();
abstract void buildGround();
abstract void buildBackGround();
public Map getMap()
{
return map;
}
}
public class MapABuilder extends MapBulider()
{
pubilc void buildSky()
{
super.map.setSky("blue");
}
public void buildGround()
{
super.map.setGround("rough");
}
public void buildBackGround()
{
super.map.setBackGround("nothing");
}
}
public class MapBBuilder extends MapBulider()
{
pubilc void buildSky()
{
super.map.setSky("black");
}
public void buildGround()
{
super.map.setGround("smooth");
}
public void buildBackGround()
{
super.map.setBackGround("something");
}
}
public class Role()
{
private string body;
private string clothing;
private string equip;
public void setBody(string body)
{
this.body = body;
}
public string getBody()
{
return body;
}
public void setClothing(string clothing)
{
this.clothing = clothing;
}
public string getClothing()
{
return clothing;
}
public void setEquip(string equip)
{
this.equip = equip;
}
public string getEquip()
{
return equip;
}
}
public abstract class RoleBuilder()
{
private Role role = new Role();
abstract void buildBody();
abstract void buildClothing();
abstract void buildEquip();
public Role getRole()
{
return role;
}
}
public class RoleABuilder extends RoleBulider()
{
pubilc void buildBody()
{
super.role.setBody("high");
}
public void buildClothing()
{
super.role.setclothing("coat");
}
public void buildEquip()
{
super.role.setEquip("expensive");
}
}
public class RoleBBuilder extends RoleBulider()
{
pubilc void buildBody()
{
super.role.setBody("short");
}
public void buildClothing()
{
super.role.setclothing("T-shirt");
}
public void buildEquip()
{
super.role.setEquip("cheap");
}
}
public class Game()
{
private MapBulider mapBuilder;
private RoleBuilder roleBuilder;
public void setMapBuilder(MapBuilder mapBuilder)
{
this.mapBuilder = mapBuilder;
}
public Map constructMap()
{
mapBuilder.buildSky();
mapBuilder.buildGround();
mapBuilder.buildBackGround();
return mapBuilder.getMap();
}
public void setRoleBuilder(RoleBuilder roleBuilder)
{
this.roleBuilder = roleBuilder;
}
public Role constructRole()
{
roleBuilder.buildBody();
roleBuilder.buildClothing();
roleBuilder.buildEquip();
return roleBuilder.getRole();
}
}
public class Client
{
public static void main(String[] args)
{
Game game = new Game();
MapBuilder a = new MapABuilder();
RoleBuilder b = new RoleABuilder();
game.setMapBuilder(a);
game.setRoleBuilder(b);
Map mapA = game.constuctMap();
Role roleA = game.constuctRole();
System.out.println("游戏组合:");
System.out.printf("the map sky %s ground %s background %s
", mapA.getSky(), mapA.getGround(), mapA.getBackground());
System.out.printf("the role body %s clothing %s equip %s
", roleA.getBody(), roleA.getColthing(), roleA.getEquip());
}
}
5、某系统需要提供一个加密模块,将用户信息(如密码等)加密之后再存储在数据库中,系统已经定义好数据库操作类。为了提高开发效率,现要重用已有的加密算法,这些算法由第三方提供,没有源码。如采用Adapter模式如何设计?