The following class diagram represents a design in abstract factory pattern to query about the features of different types of buildings. See the source code for the implementation of the following class diagram.
(关于图片,不太好意思,上传的时候就调整了方向,上传后却变成了这样,调整了很长时间都没有旋转过来,有知道怎么办的可以评论哦)
3. When a user chooses semi-detatcher and click on search, what methods will be called? List all of them in correct order.
a) getFactory()
b) getSemiDetacher()
c) getSemiDetacherInfo()
4. After you add the required class SemiDetacher hierarchy,
a) What classes in the BuildingFactory class hierarchy have been affected?
b) What methods have you added to the class hierarchy?
a) Class MediumFactory, Class SuperFactory, Class BuildingFactory have been affected
b) An abstract method getSemiDetacher() has been added to abstract class BuildingFactory, class SuperFactory and class MediumFactory both add a method getSemiDetacher() and implement them at the same time.
5. Discuss the similarity and differences between factory method pattern and the abstract factory pattern in no more that 120 words.
Similarity:
They both encapsulate the process of object creation into a factory class, so that client class has no direct access to create the concrete class.
Differences:
Their production are different, factory method pattern produce a single product class hierarchy; however, abstract factory pattern produce a group of product class hierarchies.
The factory method pattern follows open-closed principle, while the abstract factory pattern only partially follows the open-closed principle.
6. Discuss about the method getFactory(String type) in the class
BuildingFactory.
a) What is the functionality of this class?
b) If we cancel this method from class BuildingFactory, then what do we need to do in the class AbstractFactoryGUI?
c) Point out advantages of the method getFactory(String type).
a) To create SuperFactory class or MediumFactory class.
b) We need to Create an instance of BulidingFactory class and transition it down to SuperFactory class or MediumFactory class.
c) ClientCUI class could create objects representing houses of different types and categories through different kinds of factory without having to know the actual concrete class that needs to be instantiated. Also, in the ClientGUI, we have no necessary to write a lot of conditional statements to choose which factory, so that it is benefit for us to extend and maintain.
The following design represents an agent information system. Agents’information should be encrypted and then saved into a text file or a database. There are two ways to encrypt agent’s name and code number, represented by classes EncryptedInfo1 and EncryptedInfo2.
EncryptedInfo1的加密算法:折叠算法
设计类图如下
Fig. 1 Design class diagram for agent information system
Your task:
You are required to write a class EncryptedInfo3 to encrypt agent’s last name, first name and code number.
a) The class should be in the same format as EncryptedInfo1 and EncryptedInfo2, and provide a different encryption method
b) EncryptedInfo3 should encrypt the last name, first name and code number as described below as CaesarCypher: To encrypt a word, we replace the letter a with b, b with c, and so on, up to z, which is replaced by a. This is called the rotate-1 Caesar cipher. When a digital number between 0 and 9 is encountered, the rule to rotate is 01, 12,…,89 and 90. For example:
Plain text: Mike
Encrypted text: Njlf
Plain text: Sun
Encrypted text: Tvo
Plain text: Shanben56
Encrypted text: Tibocfo67
c) Modify the class ClientGUI to allow choice EncryptedInfo3 to be shown in the GUI.
Note: 特工密码长度为12位,由英文字母与0,1,…,9阿拉伯数字组成。
The length for a code is exactly 12, formed by mixed characters and digital numbers.
Describe your finished homework, including
a) What classes have been added to the existing class hierarchy?
b) Explain the relationship between the existing classes and the newly added classes.
a) Answer: Add a new derived class inherited from the parent class Agent Info class, namely EncryptedInfo3 class.
b) Derived classes EncryptedInfo3 inherited from the parent class AgentInfo. Class EncryptedInfo3 aggregate class MessageWriter.
Draw your new class diagram here
Answer questions as below: after you add class EncryptedInfo3,
a) Do you need to recompile class EncryptedInfo1 and EncryptedInfo2?
b) Do you need to recompile class AgentInfo?
c) Do you need to recompile class FileWriter or DBWriter?
a) Answer: No, we needn’t, we haven’t changed the EncryptedInfo1 and EncryptedInfo2 classes, we just add a class named EncryptedInfo3 parallel to them.
b) Answer: No, we just call the AgentInfo class without modifying it.
c) Answer: No, we just call FileWriter and DBWriter without modifying them.
In class EncryptedInfo1, there is a constructor
public EncryptedInfo1(MessageWriter l){
writer = l;
}
Why do we use type MessageWriter as the parameter type and why don’t we use type FileWriter or DBWriter as the parameter type?
Answer: use type MessageWriter can decouple Abstraction and Implementor also eliminates compile-time dependencies on the implementation.We doesn’t need to recompile the Abstraction class and its clients when changing an implementation class . We don’t need to recompile any classes if we add a class parallel to FileWriter or DBWriter.
In class ClientGUI , we have the source code
MessageWriter writer;
…
if( logWay.compareTo(ClientGUI.DBWRITER)==0 )
writer = new DBWriter();
if( logWay.compareTo(ClientGUI.FILEWRITER)==0 )
writer = new FileWriter();
What is the advantage of writing code above?
Answer: The implementation of an abstraction can be configured at run-time.
If we add a class named ConsolWriter parallel to DBWriter, do we need to recompile any classes?
Answer: Except for GUI classes that need to recompile some additional conditional statements, other classes do not need to recompile.
Test your finished program. List your test input and output from running your program.
See the following class diagram for the State pattern implementation of Bank.
Fig 1. class diagram of Bank account in the State pattern
Fig 2. the state transition diagram for the Bank problem
Fig 3. the graphical user interface produced when class AccountManager is run
The class BankContext acts as the Context class in the State Pattern and class ClientGUI is the client class. Read the source code carefully and then do the following:
Add a new class called TaxState to subclass the State class. Use the algorithm as below:
When balance >= 100000, the program runs into TaxState. Under this state, every newly deposited amount of money will be charged 5% for the tax (the tax will be paid to the government, however, we ignore this part and just deduct 5% for newly deposit amount of money ) .
Describe your finished homework, including
a) What classes have been added to the existing class hierarchy?
b) Specify the relationship between the existing classes and the newly added classes.
a) A new class named TaxState has been added.
b) The class TaxState is the subclass of the abstract class State, the same as the other three subclasses.
Draw your new state diagram here
When you fill in “deposit” in the in the transaction type, and when you fill in the money amount like 2500, what statements in the class ClientGUI will be invoked. Also trace back all the statements called in class BankContext and class State and its subclasses.
if (e.getActionCommand().equals(EXIT)) {
// Won’t be invoked.
System.exit(1);
}
if (e.getActionCommand().equals(SUBMIT)) {
String type = getTransactionType();
String amount = getTransactionAmount();
if (type.equals(DEPOSIT)) {
accContext.deposit(new Double(amount).doubleValue());
}
if (type.equals(WITHDRAW)) {
// Won’t be invoked.
accContext.withdraw(new Double(amount).doubleValue());
}
searchResult = “Transaction Successful: \n” + “New Balance=” +
accContext.getBalance() +"\n" + "New State = "
public void setStateObj(State objState) {
this.objState = objState;
}
public void deposit(double amount) {
if (amount > 0) {
objState.setContext(this);
objState.deposit(amount);
} else {
// Won’t be invoked.
System.out.println(“Deposit amount cannot be 0 or negative”);
}
}
public double getBalance() {
return balance;
}
public void updateBalance(double balance) {
this.balance = balance;
}
public boolean isOverDrawnLimitHit() {
return objState.isOverDrawnLimitReached();
}
3) In State:
protected void changeState() {
balance = context.getBalance();
if (balance < 0 && balance >= BankContext.OVERDRAW_LIMIT)
// Won’t be onvoked.
state = OVERDRAWNSTATE;
else if (balance >= BankContext.MIN_BALANCE
&& balance < BankContext.TAX_BALANCE)
state = NOFEESTATE;
else if (balance >= 0 && balance < BankContext.MIN_BALANCE)
// Won’t be invoked.
state = FEESTATE;
else if (balance >= BankContext.TAX_BALANCE)
// Won’t be invoked.
state = TAXSTATE;
else
//Won’t be invoked.
state = ERRORSTATE;
passStateObjToContext();
}
public void passStateObjToContext() {
if (state.equals(OVERDRAWNSTATE))
// Won’t be invoked.
stateObj = new OverDrawnState();
else if (state.equals(NOFEESTATE))
stateObj = new NoTransactionFeeState();
else if (state.equals(FEESTATE))
// Won’t be invoked.
stateObj = new TransactionFeeState();
else if (state.equals(TAXSTATE))
// Won’t be invoked.
stateObj = new TaxState();
context.setStateObj(stateObj);
}
public String getState() {
return state;
}
public void setContext(BankContext context) {
this.context = context;
}
4) In TransactionFeeState:
@Override
public void deposit(double amount) {
if (amount > 0) {
balance = context.getBalance() - BankContext.TRANS_FEE_NORMAL;
balance = balance + amount;
context.updateBalance(balance);
changeState();
} else {
// Won’t be invoked.
System.out.println(“Deposit amount cannot be 0 or negative”);
}
}
@Override
public boolean isOverDrawnLimitReached() {
return overDrawnLimitFlag;
}
5) In NoTransactionFeeState:
public NoTransactionFeeState() {
state = NOFEESTATE;
}
4. What methods in what classes are used to keep the communication between the State class hierarchy and the context class BankContext
See the following class diagram for the Mediator pattern implementation of collaboration program for Hotel, Airline and Tour.
图1 利用中介者模式设计的信息共享程序
The three graphical user interfaces for the Hotel, Airline and Tour are as below.
图2 旅游公司输入客户信息
图3 宾馆界面同步获得旅游公司输入的客户信息
图4 机场界面同步获得旅游公司输入的客户信息
当在一个用户图形界面,例如Tour,中输入Mike Lee,1239852,选择USA,然后点击按钮“Submit”,则程序会通过BusinessMediator对象,将这些客户信息转发给Hotel和Airline对象,并且显示在相应的用户图形界面上。这些客户数据也被写到了以下的数据文件TourCustomer.xml,HotelPossibleCustomer.xml和AirportPossibleCustomer.xml中。
Homework requirement:
Describe your finished homework, including
a) What classes have been added to the existing class hierarchy?
b) The relationship between the existing classes and the newly added classes
a) Answer: We added class TouriststoreGUI to the existing class hierarchy.
b) Answer: Class TouriststoreGUI is like class HotelGUI, AirflightGUI, TourGUI and TouriststoreGUI, is a derived class of class ParticipantGUI.
Draw the final class diagram here
In HotelGUI object, when you enter name entry as “Rose Black”, id number entry as “555888999”, and choose the nationality as “USA”, and then click on button “Submit” to save the customer information into file HotelCustomer.xml, what methods in what class will have been invoked, list all of them in correct order.
Answer: When click on button “Submit”:
Firstly, the writeCusToXmlFile method is invoked in class HotelGUI.
Secondly, the askMedSaveCusInfo method is invoked in class HotelGUI.
Thirdly, the writePossibleCustoXmlFile method is invoked in the BusinessMediator class.
Forthly, the writeCandidateCustoXmlFile method is invoked in the airlineGUI class, HotelGUI class, TourGUI class and TouristStoreGUI class in turn.
Finally, the addCustomer method, askMedAddCus method and askMedUpdate method is invoked in the class Hotel.
Discussion
a) Describe the functionality of the BusinessMediator class.
b) Explain how the mediator pattern works?
c) How the method updateAllGuis(ParticipantGUI p, String text) in the BusinessMediator class works?
a) Answer: BusinessMediator class abstracting all object interaction details. BusinessMediator keep a reference of each of the interacting objects and provide methods to call the participating objects, such as HotelGUI, AirflightGUI and so on.
b) Answer: mediator pattern requests the interaction between any two different objects is routed through the Mediator class. All objects send their messages to the mediator by calling methods of Mediator. The mediator then sends messages to the appropriate objects to implement the application’s requirements. Finally, the mediator invokes the methods of all other actors participated.
c) Answer: method updateAllGuis through an iterator, the displayInfo method of each bound participant object is called in turn.
[Extendibility Issue] Discussion:
a) After you add the new class TouriststoreGUI, do you have to modify class ParticipantGUI?
b) After you add the new class TouriststoreGUI, do you have to modify class HotelGUI?
c) After you add the new class TouriststoreGUI, do you have to modify class AirlineGUI?
d) After you add the new class TouriststoreGUI, do you have to modify class TourGUI?
e) After you add the new class TouriststoreGUI, do you have to modify class BusinessMediator?
a) Answer: No, we don’t have to. Expanding a subclass does not need to modify the code of the parent class.
b) Answer: No, we don’t have to. Class TouriststoreGUI just calls class HotelGUI’s methods through the mediator class.
c) Answer: No, we don’t have to. Class TouriststoreGUI just calls class AirlineGUI’s methods through the mediator class.
d) Answer: No, we don’t have to. Class TouriststoreGUI just calls class TourGUI’s methods through the mediator class.
e) Answer: No, we don’t have to modify class BusinessMediator.
The purpose of this project is to design an agent information system by using three-layer layered architecture. The agent information is encrypted and then saved into some specific text files. In the design below, the graphical user interface includes a class nemed ClientGUI for sending user’s request. The application layer contains a class hierarchy named Encryption to actually encrypt agent info and calls class TxtFileWriter, which saves the encrypted data into files Folding.txt, and Group-swap.txt. The agent information to be encrypted includes agent’s last name, first name, and agent code. The agent code can be mixed characters with digital numbers. The length of the code is 12.
Fig 1. Software architecture for the agent information system
Note that we can think class hierarchy named encryption in the application layer forms a strategy design pattern (without the context class).
算法描述:
Class EncryptedInfo1 encrypts agent information using Encryption Algorithm1 (折叠算法) as below.
Precondition: a text file to be encrypted contains only English characters and digital numbers. The algorithm is:
az, by, …mn, yb, za
09, 18, 27, 36, 45, 54, 63, 72, 81, 90
Upper case letters are also encrypted the same way (Upper case letters are encrypted into Upper case letters).
Class EncryptedInfo2 encrypts agent information using Encryption Algorithm2 (分组互换算法).
Precondition: a text file to be encrypted contains only English characters and digital numbers. The algorithm is:
ab, ba; cd, dc; ef, fe;, …, yz, zy
01, 10; 23, 32; …89, 98
Upper case letters are also encrypted the same way (Upper case letters are encrypted into Upper case letters) .
Class diagram is as Fig 2.
The souce code for the above class diagram has been written in Java. The encrypted agent information from EncryptedInfo1 is saved into a file named Folding.txt and encrypted agent information from EncryptedInfo2 is saved into a file named Group-swap.txt. Both text files are in the same folder as the Java source files.
Fig 2. The class diagram for the agent information system
你的任务:
在以上已经存在的设计中,新增加第三种加密算法. Now you are required to write a class EncryptedInfo3 to encrypt agent’s last name, first name and code number.
a) The class should be in the same format as EncryptedInfo1 and EncryptedInfo2, and provides a different encryption method
b) EncryptedInfo3 should encrypt the last name, first name and code number as described below as CaesarCypher: To encrypt a word, we replace the letter a with b, b with c, and so on, up to z, which is replaced by a. This is called the rotate-1 Caesar cipher. When a digital number between 0 and 9 is encountered, the rule to rotate is 01, 12,…,89 and 90. For example:
Agent first name: Mike
Encrypted agent first name: Njlf
Agent last name: Sun
Encrypted agent last name: Tvo
Agent code: WildWolf7489
Encrypted agent code: XjmeXpmg8590
c) Modify the class ClientGUI to allow choice EncryptedInfo3 to be shown in the GUI.
d) The encrypted agent information should be saved into a text file Caesar.txt.
在应用层,增加相应的解密算法层次类Decryption,该类带有三个相应的解密子类。分别从三个加密文件中读出的agent加密信息,可以通过此层次类解密,然后显示在用户图形界面上。
在用户图形界面上增加解密按钮,以便对各个加密文件所存的内容进行解密,然后显示的用户图形界面上。
【注】为了方便起见,你也可以使用另外一个文件夹包含全部的解密程序和text文档。
Draw your software architecture for your new agent information encryption system
Draw class diagram for your new agent information encryption system
To implement all the required functionalities, what classes have you added to the application layer?
Answer: We add class EncryptedInfo3 to encrypt agent’s last name, first name and code number. We add class Decryption with three subclasses, named class DecryptedInfo1, DecryptedInfo2 and DecryptedInfo3, to decrypt the file and display it on the user graphical interface.
Which design pattern has been used in the application layer?
Answer: Strategy Pattern has been used in the application layer. When a new decryption algorithm is added, the existing class hierarchy doesn’t need to be changed and recompiled.
Answer question: When you add a new class in the application layer, will the data file access layer be affected or not?
Answer: It will not be affected. The application layer only calls the data of the data storage layer, and the specific implementation is in the application layer.
Answer question: When you add a new class in the application layer, will the user interface layer be affected or not?
Answer: It will not be affected. The user interface only needs to add a new Button to call the new services of the application layer, and the specific implementation is carried out in the application layer.
Answer question: When you add a new method in the TxtFileWriter class, will the user interface layer or the application layer be affected or not?
Answer: It will not be affected. The user interface layer and application layer call the services of the next layer, and do not care about the specific implementation of the next layer.
If you want the user interface layer to initialize a request to save some data into the data files, what classes need to call?
Answer: We need to call class Encryption and class TextWriter, the user interface layer calls the log method of class Encryption in the application layer, which calls textwriter’s write file method.
In your program,
Do text file encryption by using the pipes and filters architecture. Suppose that we have 3 encryption algorithms as following:
Encryption Algorithm 1 (折叠算法). Precondition: a text file to be encrypted contains only English characters and digital numbers. The algorithm is:
az, by, …mn, yb, za
09, 18, 27, 36, 45, 54, 63, 72, 81, 90
Upper case letters are also encrypted the same way (Upper case letters are encrypted into Upper case letters).
Encryption Algorithm 2(分组互换算法). Precondition: a text file to be encrypted contains only English characters and digital numbers. The algorithm is:
ab, ba; cd, dc; ef, fe;, …, yz, zy
01, 10; 23, 32; …89, 98
Upper case letters are also encrypted the same way (Upper case letters are encrypted into Upper case letters) .
Encryption Algorithm 3 (Caesar cipher, rotation 1): Precondition: a text file to be encrypted contains only English characters and digital numbers. The algorithm is: for the 26 English characters, you replace the letter a with b, b with c, and so on, up to z, which is replaced by a. When a digital number between 0 and 9 is encountered, the rule to rotate is 01, 12,…,89 and 90.
ab, bc; cd, de; ef, …,yz, za
01, 12; 23, 34; 45, 56,67,78, 89, 90
Upper case letters are also encrypted the same way (Upper case letters are encrypted into Upper case letters)
The program should be designed using pipes and filters architecture, which contains filters as below:
Fig 1. The logical design of the encryption system
The class diagram design is as below.
Fig 2. The design of encryption program using pipes and filters architecture
你的任务: