设计模式NO.1

设计模式NO.1

根据作业要求完成下列题目:

题目1:

(1)要求:某系统日志记录器要求支持多种日志记录方式,如文件记录、数据库记录等;用户可以根据要求动态选择日志记录方式。使用Factory模式来设计;
类图如下所示:

实现代码如下:
a.抽象工厂

public abstract class LogFactory {
	abstract Log logCreate();

}

b. 抽象日志

public abstract class Log {
	abstract void add();
	abstract void delete();
	abstract void update();

}

c.文件日志工厂类

public class FileFactory extends LogFactory{

	@Override
	Log logCreate() {
		return new FileLog();
	}

}

d.数据库日志工厂类

public class DBFactory extends LogFactory{

	@Override
	Log logCreate() {
		return new DBLog();
	}

}

e.文件日志产品类

public class FileLog extends Log{

	@Override
	void add() {
		System。out.println("add by FileLog!");
		
	}

	@Override
	void delete() {
		System。out.println("delete by FileLog!");
		
	}

	@Override
	void update() {
		System。out.println("update by FileLog!");
		
	}

}

f.数据库日志产品类

public class DBLog extends Log{

	@Override
	void add() {
		System。out.println("add by DBLog!");
		
	}

	@Override
	void delete() {
		System。out.println("delete by DBLog!");
		
	}

	@Override
	void update() {
		System。out.println("update by DBLog!");
		
	}

}

(2)如果系统中增加一个日志记录方式—控制台日志记录(ConsoleLog),类图将如何改变?
改变后的类图如下:

题目2

某系统为了改进数据库操作的性能,自定义数据连接对象Connection和语句对象Statement,可针对不同类型的数据库提供不同的连接对象和语句对象;用户可以通过配置文件等方式根据实际需要动态更换系统数据库;使用Abstract Factory模式来设计。
类图设计如下:

实现代码如下:
a. 抽象Connection产品类

public abstract class Connection {
	abstract void connect();

}

b.MySqlConnection产品类

public class MySqlConnection extends Connection{

	@Override
	void connect() {
		System.out.println("this is MysqlConnection");
		
	}

}

c.OrcaleConnection产品类

public class OrcaleConnection extends Connection{

	@Override
	void connect() {
		System.out.println("this is OrcaleConnection");
	}

}

d.抽象Statement产品类

public abstract class Statement {
	abstract void statement();
}

e.MySqlStatement产品类

public class MySqlStatement extends Statement{

	@Override
	void statement() {
		System.out.println("this is MySqlStatement");
	}

}

f.OrcaleStatement产品类

public class OrcaleStatement extends Statement{

	@Override
	void statement() {
		System.out.println("this is OrcaleStatement");
	}

}

g.抽象工厂类DBFactory

public abstract class DBFactory {
	abstract Connection getConnect();
	abstract Statement getStatement();
}

h.MySql工厂类

public class MySqlFactory extends DBFactory{

	@Override
	Connection getConnect() {
		return new MySqlConnection();
	}

	@Override
	Statement getStatement() {
		return new MySqlStatement();
	}

}

i.Orcale工厂类

public class OrcaleFactory extends DBFactory{

	@Override
	Connection getConnect() {
		return new OrcaleConnection();
	}

	@Override
	Statement getStatement() {
		return new OrcaleStatement();
	}

}

题目3

KFC套餐是一个复杂对象,一般包括主食(如汉堡、鸡肉卷等)和饮料(如果汁、可乐等)组成部分,不同套餐有不同组成部分;KFC服务员要根据顾客要求,装配这些组成部分,构造一个完整套餐,返回给顾客;使用Builder模式来设计。
设计的类图如下:

实现代码如下:
a.套餐类

public class KFCPackage {
	private String food="";
	private String drink="";
	public void addFood(String Food)
	{
		food+=Food;
	}
	public void addDrink(String Drink)
	{
		drink+=Drink;
	}
	public void show()
	{
		System.out.println("your order is:\n food:"+food+"\n drink:"+drink);
	}

}

b.抽象建造者类

public abstract class KFCBuilder {
	abstract void BuilderFood();
	abstract void BuilderDrink();
	abstract KFCPackage getPackage();

}

c.建造者A类

public class KFCBuilderA extends KFCBuilder{
	private KFCPackage m_package;

	public KFCBuilderA() {
		m_package=new KFCPackage();
	}

	@Override
	void BuilderFood() {
		m_package.addFood("牛肉海鲜堡");
		
	}

	@Override
	void BuilderDrink() {
		m_package.addDrink("冰霜柠檬汁");
		
	}

	@Override
	KFCPackage getPackage() {
		return m_package;
	}

}

d.建造者B类

public class KFCBuilderB extends KFCBuilder{

	private KFCPackage m_package;

	public KFCBuilderB() {
		m_package=new KFCPackage();
	}

	@Override
	void BuilderFood() {
		m_package.addFood("鸡腿鲤鱼堡");
		
	}

	@Override
	void BuilderDrink() {
		m_package.addDrink("极地冰霜鲜橙汁");
		
	}

	@Override
	KFCPackage getPackage() {
		return m_package;
	}
}

e.侍者类

public class KFCWaiter {
	private KFCBuilder br;
	public void setBuilder(KFCBuilder br)
	{
		this.br=br;
	}
	public KFCPackage construct()
	{
		br.BuilderFood();
		br.BuilderDrink();
		return br.getPackage();
	}
}

题目4

游戏中的地图:包括天空、地面、背景;人物包括人体、服装、装备等组成部分,如采用Builder模式如何设计?
类图设计如下:

代码实现如下:
a.Map类

public class Map {
	 private String sky;
	 private String ground;
	 private String background;
	 public void setSky(String sky) 
	 {
	       this.sky=sky;
	 }
	 public void setGround(String ground) 
	 {
	       this.ground=ground;
	 }
	 public void setBackground(String background) 
	 {
	       this.background=background;
	 }
	 public void show() 
	 {
	        System.out.println("Map:\n sky: "+sky+"\n ground: "+ground+"\n background: "+background);
	 }

}

b.Person类

public class Person {
	private String clothes;
	private String body;
	private String equipment;
	public void setClothes(String clothes) {
		this.clothes=clothes;
	}
	public void setBody(String body) {
	    this.body=body;
	 }
	public void setEquipment(String equipment) {
	    this.equipment=equipment;
	 }
	 public void show() {
		 System.out.println("Person:\n body: "+body+"\n clothes: "+clothes+"\n equipment: "+equipment);
	 }
}

c.抽象Map建造者类

public abstract class MapBuilder {

	abstract void BuilderSky();
	abstract void BuilderGround();
	abstract void BuilderBackGround();
	abstract Map getMap();

}

d.抽象Person建造者类

public abstract class PersonBuilder {

	abstract void BuliderClothes();
	abstract void BuiderBody();
	abstract void BuilderEquipment();
	abstract Person getPerson();

}

e.Map建造者A类

public class MapBuilderA extends MapBuilder{
	private Map m_map;
	public MapBuilderA() {
		m_map= new Map();
	}
	@Override
	void BuilderSky() {
		m_map.setSky("湛蓝");
		
	}
	@Override
	void BuilderGround() {
		m_map.setGround("广阔原野");
		
	}
	@Override
	void BuilderBackGround() {
		m_map.setBackground("无尽麦田");
		
	}
	@Override
	Map getMap() {
		return m_map;
	}

}

f.Person建造者A类

public class PersonBuilderA extends PersonBuilder{
	private Person m_person;
	public PersonBuilderA() {
		m_person= new Person();
	}
	@Override
	void BuliderClothes() {
		m_person.setClothes("粉饰红装");
		
	}
	@Override
	void BuiderBody() {
		m_person.setBody("婀娜多姿");
		
	}
	@Override
	void BuilderEquipment() {
		m_person.setEquipment("琵琶");
		
	}
	@Override
	Person getPerson() {
		
		return m_person;
	}

}

g.MapDirector类

public class MapDirector {
	private MapBuilder m_mb;
	public void setMapBuilder(MapBuilder m_mb)
	{
		this.m_mb=m_mb;
	}
	public Map construct()
	{
		m_mb.BuilderSky();
		m_mb.BuilderGround();
		m_mb.BuilderBackGround();
		return m_mb.getMap();
	}
}

h.PersonDirector类

public class PersonDirector {
	PersonBuilder m_pb;
	public void setBuilder(PersonBuilder m_pb)
	{
		this.m_pb=m_pb;
	}
	public Person construct()
	{
		m_pb.BuiderBody();
		m_pb.BuliderClothes();
		m_pb.BuilderEquipment();
		return m_pb.getPerson();
	}

}

题目5

某系统需要提供一个加密模块,将用户信息(如密码等)加密之后再存储在数据库中,系统已经定义好数据库操作类。为了提高开发效率,现要重用已有的加密算法,这些算法由第三方提供,没有源码。如采用Adapter模式如何设计?
类图设计如下:

实现代码如下:
a.加密方法调用接口

public interface Encryption {
	public String encrypt(String str);

}

b.适配器类

public class Adapter implements Encryption{
	NewEncrypt m_newEnc;
	public Adapter(NewEncrypt m_newEnc)
	{
		this.m_newEnc=m_newEnc;
	}
	public void setEncrypt(NewEncrypt m_newEnc)
	{
		this.m_newEnc=m_newEnc;
	}

	@Override
	public String encrypt(String str) {
		return m_newEnc.encrypt(str);
	}
}

c.原先的加密算法类

public class ActualEncrypt implements Encryption{

	@Override
	public String encrypt(String str) {
		str+="已被加密";
		return str;
	}

}

d.新的加密算法类

public class NewEncrypt {

	public String encrypt(String str)
	{
		return str+="已被新的加密方法加密";
	}
}
posted @ 2017-11-06 10:34  会煮面  阅读(373)  评论(0编辑  收藏  举报