1、 instance = new IdentityCardNo(); instance。setNo(No6000654321”); System.out.println(身份证号码为:” + instance。getNo(); else out。println(重复办理身份证,获取旧号码!); return instance; public String getNo() return no; public void setNo(String no) this.no = no;2. 每一麻将局都有两个骰子,因此骰子就应当是双例类。现使用多例模式模拟该场景。import java.util。Date
2、;import java。util。Random;public class Dice private static Dice die1 = new Dice(); private static Dice die2 = new Dice(); private Dice() public static Dice getInstance(int whichOne) if (whichOne = 1) return die1; else return die2; public synchronized int dice() Date d = new Date(); Random r = new Ran
3、dom(d.getTime(); int value = r.nextInt(); value = Math.abs(value); value = value 6; value += 1; return value;util.Random;public class DiceClient private static Dice die1, die2; public static void main(String args) die1 = Dice.getInstance(1); die2 = Dice.getInstance(2); System。第一骰子骰出: + die1.dice();p
4、rintln(”第二骰子骰出: ” + die2。dice();3. 某软件公司为某电影院开发了一套影院售票系统,在该系统中需要为不同类型的用户提供不同的电影票(MovieTicket)打折(Discount)方式,具体打折方案如下:学生凭学生证可享受票价8折优惠;年龄在10周岁及以下的儿童可享受每张票减免10元的优惠(原始票价需大于等于20元);影院VIP用户除享受票价半价优惠外还可进行积分,积分累计到一定额度可换取电影院赠送的奖品.该系统在将来可能还要根据需要引入新的打折方式.试使用策略模式设计并编程模拟实现该影院售票系统。public interface Discount public
5、double calculate(double price);public class MovieTicket private double price; private Discount discount; /维持一个对抽象折扣类的引用 public void setPrice(double price) this。price = price; /注入一个折扣类对象 public void setDiscount(Discount discount) discount = discount; public double getPrice() /调用折扣类的折扣价计算方法 return dis
6、count.calculate(this。price); /VIP会员票折扣类:具体策略类 public class VIPDiscount implements Discount public double calculate(double price) System。VIP票:”); System.out。println(”增加积分! return price * 0。5;/学生票折扣类:public class StudentDiscount implements Discount System.out.println(”学生票:);8; /儿童票折扣类: public class Ch
7、ildrenDiscount implements Discount println(”儿童票: return price - 10;public class MoviceClient public static void main(String args) MovieTicket mt = new MovieTicket(); double originalPrice = 60.0; double currentPrice; mt。setPrice(originalPrice); System.out.println(原始价为: + originalPrice);-”); Discount
8、discount =new VIPDiscount(); /vip 用户setDiscount(discount); /注入折扣对象 currentPrice = mt.getPrice();out.println(”折后价为:” + currentPrice); discount =new StudentDiscount(); /学生用户setDiscount(discount); currentPrice = mt。getPrice();折后价为: + currentPrice); discount =new ChildrenDiscount(); /儿童用户 mt.setDiscount
9、(discount);println(”折后价为:3)实现结果:4。 某软件公司欲开发一款飞机模拟系统,该系统主要模拟不同种类飞机的飞行特征与起飞特征,需要模拟的飞机种类及其特征如表1所示:表1 飞机种类及特征一览表飞机种类起飞特征飞行特征直升机(Helicopter)垂直起飞(VerticalTakeOff)亚音速飞行(SubSonicFly)客机(AirPlane)长距离起飞(LongDistanceTakeOff)歼击机(Fighter)超音速飞行(SuperSonicFly)鹞式战斗机(Harrier)为将来能够模拟更多种类的飞机,试采用策略模式设计并模拟实现该飞机模拟系统.publi
10、c class plane private state state;/状态 public void settakeoffFeatures(state tFeatures) this.state = tFeatures; public void setplanetype(String type) if(type=”直升机”) state=new Helicopter(); else if(type=客机) state=new AirPlane(); else if(type=”歼击机) state=new Fighter(); else if(type=”鹞式战斗机”) state=new Ha
11、rrier(); else state=null; public void takeoff() state。takeOff(); public void fly()fly();public class AirPlane implements state Override public String takeOff() println(”长距离起飞”); return ”长距离起飞”; public String fly() 亚音速飞行 return ;public class Fighter implements state public String takeOff() 长距离起飞”); O
12、verride public String fly() 超亚音速飞行超音速飞行”;public class Harrier implements state System.out.println(垂直起飞;println(”超亚音速飞行 return ”超音速飞行public class Helicopter implements state System.out。println(”垂直起飞 return ”垂直起飞”;亚音速飞行”); return ”亚音速飞行”;public interface state public String takeOff();/起飞 public String
13、 fly();/飞行public class Client public static void main(String args) plane plane = new plane(); plane.setplanetype(歼击机 plane.takeoff(); plane.fly();5。 儿子、妈妈、父亲三人合作画一幅画,儿子负责画出一朵花的轮廓,妈妈负责涂上颜色、父亲负责将画裱在画框里。现请使用装饰模式模拟这个过程。2)实现代码:public interface painting public String Draw();public class Son implements pai
14、nting public String Draw() 儿子用笔画出了花的轮廓 return ”儿子用笔画出了花的轮廓”;public class Father implements painting private painting painting;/被装饰者 public Father(painting painting) this.painting =painting; private Father() public void paint() /爸爸装饰者做的职责 System.out.println(”爸爸正在做上画框前的准备工作”); painting。Draw();/爸爸装饰者做职
15、责println(”父亲将画裱在画框里”);public String Draw() 父亲将画裱在画框里父亲将画裱在画框里”;public class Mother implements painting private painting painting; public Mother(painting painting) painting =painting; private Mother() public void paint() 妈妈正在做给画上颜色前的准备工作。 painting.Draw();/妈妈装饰者做的职责妈妈给画上好了颜色println(”妈妈给画上好了颜色 return ”
16、妈妈给画上好了颜色 public static void main(String args) painting painting =new Son(); painting。Draw(); painting = new Mother(painting); painting.Draw(); painting = new Father(painting);3)实现结果:6。 某公司想通过网络传输数据,但是担心文件被窃取。他们的所有数据都采用字符的方式传送。现在他们开发了一个数据加密模块,可以对字符串进行加密,以便数据更安全地传送.最简单的加密算法通过对字母向后移动6位来实现,同时还提供了稍复杂的逆向
17、输出加密,还提供了更为高级的求模加密,让每一位与6求模。用户先使用最简单的加密算法对字符串进行加密,再对加密之后的结果使用复杂加密算法进行二次加密,再对二次加密结果用高级加密算法进行第三次加密。public class ConcreteEncrypt implements EncryptComponet private EncryptComponet encryptComponet; public ConcreteEncrypt(EncryptComponet encryptComponet) super(); this.encryptComponet = encryptComponet;pu
18、blic void encrypt() encryptComponet.encrypt();public interface EncryptComponet public abstract void encrypt();public class RawData implements EncryptComponet public void encrypt() println(”这是要发送的数据public class ReversEncrypt implements EncryptComponet public ReversEncrypt(EncryptComponet encryptCompo
19、net) addReservesEncrypt(); private void addReservesEncrypt() 反向加密”); public void encrypt() public class SuperEncrypt implements EncryptComponet public SuperEncrypt(EncryptComponet encryptComponet) addSuperEncrypt(); private void addSuperEncrypt() 最高加密算法”); public void encrypt() public class Translat
20、eEncrypt implements EncryptComponet public TranslateEncrypt(EncryptComponet encryptComponet) addTranslateEncrypt(); private void addTranslateEncrypt() System.out.println(”移动加密 public void encrypt() public class Client public static void main(String args) EncryptComponet s0,s1,s2,s3; s0 = new RawData
21、(); s1 = new TranslateEncrypt(s0); s2 = new ReversEncrypt(s1); s3 = new SuperEncrypt(s2); s3.encrypt();7. 现需要设计一个可以模拟各种动物行为的机器人,在机器人中定义了一系列方法,如机器人叫喊方法cry()、机器人移动方法move()等。如果希望在不修改已有代码的基础上使得机器人能够像狗一样叫,像狗一样跑,使用适配器模式进行系统设计.2)实现代码public interface Robot public void cry(); public void move();public class Dog public void barks()println(”狗在叫”); public void run()狗在跑”);public class Dogadapter extends Dog implements Robot public void cry() barks(); public void move() run(); public static void main(String args) Dogadapte
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1