设计模式第一次作业
1.用户可以根据要求动态选择日志记录方式。使用Factory模式来设计;如果系统中增加一个日志记录方式—控制台日志记录(ConsoleLog),类图将如何改变?
抽象工厂
public abstract class RecordFactory {
abstract Record createRec();
}
文件工厂
public class DocFactory extends RecordFactory {
@override
Record createRec() {
return new DocRecord();
}
}
数据库工厂
public class DbFactory extends RecordFactory {
@override
Record createRec() {
return new DbRecord();
}
}
抽象的产品
abstract class Record {
abstract void operate();
}
文件产品
public class DocRecord extends Record{
@override
void operate() {
System.out.println("DocRecordOperate");
}
}
数据库产品
public class DbRecord extends Record{
@override
void operate() {
System.out.println("DbRecordOperate");
}
}
增加了控制台记录的类图
2.某系统为了改进数据库操作的性能,自定义数据连接对象Connection和语句对象Statement,可针对不同类型的数据库提供不同的连接对象和语句对象;用户可以通过配置文件等方式根据实际需要动态更换系统数据库;使用Abstract Factory模式来设计。
抽象工厂类
public abstract class DbFactory {
abstract Statement createStatement();
abstract Connection createConnection();
}
MySql工厂类
public class MySqlFactory extends DbFactory{
@override
Statement createStatement() {
return new MySqlStatement;
}
Connection createConnection() {
return new MySqlConnection;
}
}
Oracle工厂类
public class OracleFactory{
@override
Statement createStatement() {
return new OracleStatement;
}
Connection createConnection() {
return new OracleConnection;
}
}
SqlServer工厂类
public class SqlServerFactory{
@override
Statement createStatement() {
return new SqlServerStatement;
}
Connection createConnection() {
return new SqlServerConnection;
}
}
Statement语句对象类
public abstract class Statement {
abstract void operate();
}
Connection连接对象类
public abstract class Connection {
abstract void connect();
}
MySql语句对象
public class MySqlStatement extends Statement{
@override
public void operate() {
System.out.println("MySqlStatement");
}
}
MySql连接对象
public class MySqlConnection extends Connection{
@override
public void connect() {
System.out.println("MySqlConnection");
}
}
3.KFC套餐是一个复杂对象,一般包括主食(如汉堡、鸡肉卷等)和饮料(如果汁、可乐等)组成部分,不同套餐有不同组成部分;KFC服务员要根据顾客要求,装配这些组成部分,构造一个完整套餐,返回给顾客;使用Builder模式来设计。
抽象建造类
public abstract class CombinationBuilder {
private Combination com;
public abstract void buildFood();
public abstract void buildDrink();
public Combination() {
return com;
}
}
到眼类
public class Director{
public void build(CombinationBuilder comBuilder) {
comBuilder.buildDrink();
comBuilder.buildFood();
}
}
普通套餐类
public class NormalCombination extends CombinationBuilder{
@override
void buildFood() {
com.add("whateverFood");
}
void buildDrink() {
com.add("whateverDrink");
}
}
套餐类
public class Combination{
private List<String> list = new LinkedList<String>();
public void add(String thing) {
list.add(thing);
}
public void show() {
System.out.println("combination");
}
}
4.游戏中的地图:包括天空、地面、背景;人物包括人体、服装、装备等组成部分,如采用Builder模式如何设计?
导演类
public class LevelDirector{
public void buildMap(MapBuilder mapBuilder) {
mapBuilder.buildSky();
mapBuilder.buildTerrain();
mapBuilder.buildBackground();
}
public void buildRole(RoleBuilder roleBuilder) {
roleBuilder.buildBody();
roleBuilder.buildOutfit();
roleBuilder.buildArmer();
}
}
地图构建类
public abstract class MapBuilder {
private Map map;
public abstract void buildSky();
public abstract void buildTerrain();
public abstract void buildBackground();
public Map getMap() {
return map;
}
}
角色构建类
public abstract class RoleBuilder {
private Player role;
public abstract void buildBody();
public abstract void buildOutfit();
public abstract void buildArmer();
public Map getRole() {
return role;
}
}
菜鸟角色构建类
public class NoobRole extends RoleBuilder{
@override
void buildBody() {
Role.add("WeakBody");
}
void buildOutfit() {
Role.add("Original-Outfit");
}
void buildArmer() {
Role.add("Original-Armer");
}
}
玩家类
public class Player{
List<String> list = new LinkedList<String>();
public void add(String thing) {
list.add(thing);
}
public void show() {
System.out.println("Player");
}
}
5.某系统需要提供一个加密模块,将用户信息(如密码等)加密之后再存储在数据库中,系统已经定义好数据库操作类。为了提高开发效率,现要重用已有的加密算法,这些算法由第三方提供,没有源码。如采用Adapter模式如何设计?
原有接口
public interface NoEncryption {
void encrypt();
}
数据库类
public class DataBase{
private NoEncryption noEncryption;
public DataBase() {}
public DataBase(NoEncryption noEncryption) {
this.noEncryption=noEncryption;
}
public void setEncryption(NoEncryption noEncryption) {
this.noEncryption=noEncryption;
}
public void encrypt() {
System.out.println("noEncryption");
}
}
适配体类
public class Adaptee{
public void encrypt() {
System.out.println("Encryption");
}
}
适配器类
public class EncryptionAdapter implements NoEncryption{
private Adaptee adaptee;
public EncryptionAdapter(Adaptee adaptee) {
this.adaptee=adaptee;
}
public void encrypt() {
System.out.println("Encryption");
}
}