1、要求:某系统日志记录器要求支持多种日志记录方式,如文件记录、数据库记录等;用户可以根据要求动态选择日志记录方式。使用Factory模式来设计;如果系统中增加一个日志记录方式—控制台日志记录(ConsoleLog),类图将如何改变?
public abstract class LogType {
public abstract void Write();
}
public class FileLogType extends LogType {
public void Write(){
System.out.println("文件记录");
}
}
public class DataBaseLogType extends LogType {
public void Write(){
System.out.println("数据库记录");
}
}
public abstract class TypeFactory {
public abstract LogType create();
}
public class FileLogTypeFactory extends TypeFactory {
public LogType create(){
return new FileLogType();
}
}
public class DataBaseLogTypeFactory extends TypeFactory {
public LogType create(){
return new DataBaseLogType();
}
}
public class client {
public static void main(String[] args){
TypeFactory factory = new FileLogTypeFactory();//创建工厂
LogType log = factory.create();
log.Write();
}
}
结果截图
新增一个类
修改之后:给每个类设置一个对应的工厂类
public class LogType{
public void Write();
}
public class FileLogType extends LogType {
public void Write();
}
public class DataBaseLogType extends LogType {
public void Write();
}
public class ConsoleLogType extends LogType {
public void Write();
}
public abstract class TypeFactory{ //多工厂模式的抽象工厂类
public abstract LogType getType();
}
public class FileLogTypeFactory extends TypeFactory{
public TypeFactory getType(){
return new FileLogType();
}
}public class ConsoleLogTypeFactory extends TypeFactory{
public TypeFactory getType(){
return new FileLogType();
}
}
public class DataBaseLogType extends TypeFactory{
public TypeFactory getType(){
return new DataBaseLogType();
}
}
要求:某系统为了改进数据库操作的性能,自定义数据连接对象Connection和语句对象Statement,可针对不同类型的数据库提供不同的连接对象和语句对象;用户可以通过配置文件等方式根据实际需要动态更换系统数据库;使用Abstract Factory模式来设计。
public abstract class UserDefinedFactory {
public abstract Connection createConnecttion();
public abstract Statement createStatement();
}
public class DatabaseA extends UserDefinedFactory {
public Connection createConnecttion(){
return new ConnectionA();
}
public Statement createStatement(){
return new StatementA();
}
}
public class DatabaseB extends UserDefinedFactory{
public Connection createConnecttion(){
return new ConnectionB();
}
public Statement createStatement(){
return new StatementB();
}
}
public abstract class Connection {
public abstract void setConnection();
}
public class ConnectionA extends Connection {
public void setConnection(){
System.out.println("创建连接方式A");
}
}
public class ConnectionB extends Connection {
public void setConnection(){
System.out.println("创建连接方式B");
}
}
public abstract class Statement {
public abstract void setStatement();
}
public class StatementA extends Statement {
public void setStatement(){
System.out.println("创建语句A");
}
}
public class StatementB extends Statement {
public void setStatement(){
System.out.println("创建语句B");
}
}
//测试类
public class Client {
public static void main(String[] args){
//生产数据库A
UserDefinedFactory databaseA = new DatabaseA();
//生产数据库B
UserDefinedFactory databaseB = new DatabaseB();
Connection conA = databaseA.createConnecttion();
Statement stateA = databaseA.createStatement();
conA.setConnection();
stateA.setStatement();
Connection conB = databaseB.createConnecttion();
Statement stateB = databaseB.createStatement();
conB.setConnection();
stateB.setStatement();
}
}
要求:KFC套餐是一个复杂对象,一般包括主食(如汉堡、鸡肉卷等)和饮料(如果汁、可乐等)组成部分,不同套餐有不同组成部分;KFC服务员要根据顾客要求,装配这些组成部分,构造一个完整套餐,返回给顾客;使用Builder模式来设计。
//创建者
public abstract class Meal {
public abstract void setPackage(String food,String drink);
public abstract Product getMeal();
}
//具体创建类
public class setMeal extends Meal {
private Product product = new Product();
public Product getMeal(){
return product;
}
public void setPackage(String food,String drink){
product.setDrink(drink);
product.setFood(food);
}
}
//导演类
public class Director {
private Meal meal = new setMeal();
public Product getAMeal(){
meal.setPackage("Hamburg","Cola");
return meal.getMeal();
}
public Product getBMeal(){
meal.setPackage("Chicken","Juice");
return meal.getMeal();
}
}
//产品类
public class Product {
private String food;
private String drink;
public void getMeal(){
System.out.println(food+" "+drink);
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public String getDrink() {
return drink;
}
public void setDrink(String drink) {
this.drink = drink;
}
}
//测试类
public class Client {
public static void main(String[] args){
Director director = new Director();
Product product1 = director.getAMeal();
product1.getMeal();
Product product2 = director.getBMeal();
product2.getMeal();
}
}
要求:游戏中的地图:包括天空、地面、背景;人物包括人体、服装、装备等组成部分,如采用Builder模式如何设计?
//创建对象类代码
public class Role {
private String body;
private String cloth;
private String equip;
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getCloth() {
return cloth;
}
public void setCloth(String cloth) {
this.cloth = cloth;
}
public String getEquip() {
return equip;
}
public void setEquip(String equip) {
this.equip = equip;
}
}
public class Map {
private String sky;
private String floor;
private String background;
public String getSky() {
return sky;
}
public void setSky(String sky) {
this.sky = sky;
}
public String getFloor() {
return floor;
}
public void setFloor(String floor) {
this.floor = floor;
}
public String getBackground() {
return background;
}
public void setBackground(String background) {
this.background = background;
}
}
//定义产品的创建方法和返回方法
public abstract class RoleBuilder {
protected Role role = new Role();
public abstract void buildBody();
public abstract void buildCloth();
public abstract void buildEquip();
public Role getRole(){
return role;
}
}
public abstract class MapBuilder {
protected Map map = new Map();
public abstract void buildSky();
public abstract void buildFloor();
public abstract void buildBackground();
public Map getMap(){
return map;
}
}
public class ConcreteRoleBuilder extends RoleBuilder{
public void buildBody(){
role.setBody("人体");
}
public void buildCloth(){
role.setCloth("服装");
}
public void buildEquip(){
role.setEquip("装备");
}
}
public class ConcreteMapBuilder extends MapBuilder {
public void buildSky(){
map.setSky("天空");
}
public void buildFloor(){
map.setFloor("地面");
}
public void buildBackground(){
map.setBackground("背景");
}
}
//指挥者类的代码
public class Director {
private RoleBuilder roleBuilder;
private MapBuilder mapBuilder;
public Director(RoleBuilder roleBuilder,MapBuilder mapBuilder){
this.roleBuilder = roleBuilder;
this.mapBuilder = mapBuilder;
}
public Role construct(){
roleBuilder.buildBody();
roleBuilder.buildCloth();
roleBuilder.buildEquip();
return roleBuilder.getRole();
}
public Map comprise(){
mapBuilder.buildSky();
mapBuilder.buildFloor();
mapBuilder.buildBackground();
return mapBuilder.getMap();
}
}
//客户端类
public class Client {
public static void main(String[] args){
RoleBuilder roleBuilder = new ConcreteRoleBuilder();
MapBuilder mapBuilder = new ConcreteMapBuilder();
Director director = new Director(roleBuilder,mapBuilder);
Map map = director.comprise();
Role role = director.construct();
System.out.println(map.getBackground()+map.getFloor()+map.getSky());
System.out.println(role.getBody()+role.getCloth()+role.getEquip());
}
}
输出结果:
某系统需要提供一个加密模块,将用户信息(如密码等)加密之后再存储在数据库中,系统已经定义好数据库操作类。为了提高开发效率,现要重用已有的加密算法,这些算法由第三方提供,没有源码。如采用Adapter模式如何设计?
public interface IEncrypt {
public String getEncrypt();
}
public class Encrypt implements IEncrypt {
public String getEncrypt(){
System.out.println("旧加密方式");
return null;
}
}
public interface Inew {
public String getNewEncrypt();
}
public class New implements Inew{
public String getNewEncrypt(){
System.out.println("新加密方式");
return null;
}
}
//中转类
public class NewEncrypt extends New implements Inew{
// public String encrypt = super.getNewEncrypt();
}
//测试类
public class Client {
public static void main(String[] args){
IEncrypt iEncrypt = new Encrypt();
iEncrypt.getEncrypt();
Inew inew = new NewEncrypt();
inew.getNewEncrypt();
}
}