You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
7. Please list some typical situations where adapter is useful or necessary.
Use libraries to solve a problem while the interfaces aren’t completely compatible
Convert the interface of a Document Object Model of an XML document into a tree structure that can be displayed.
Multiple implementations are to be used. Make them use the same interface.
Creating different collection-save-structure like hash-map, rb-tree, and so on, a total Adapter could solve all the things. If the developer want to change the implementation. He could only modify one line or two.
8. Please provide an example to illustrate advantages of the OOP principle “Program to an interface, not an implementation”.
Noodles *orderNoodles(string type) {
Noodles *noodles = 0;
// changes
if (type == "cheese") {
noodles = new CheeseNoodles();
} else if (type == "veggie") {
noodles = new VeggieNoodles();
} //……
//no changes
//cut, box, and sell
noodles->sellNoodles();
return noodles;
}
Noodles is the interface class;
CheeseNoodles and VeggieNoodles are implementation classes;
Program to interface;
noodles->sellNoodles();
This avoids any negative effects of future modifications on implementation classes.
E.g., adding a new type of noodles,
Or removing a type of noodles that does not sell well.
9. Please provide an example to illustrate advantages of the open-closed principle.
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Conform with the incremental development requirement.
Example: Factory method satisfy the open-closed principle.
10. Please try to explain the relation between “Program to an interface” and the open-closed principle.
“Program to an interface”意味着我们首先要设计接口,然后我再去实现它。
“开放封闭原则”是指我们应该使代码具有可扩展性,最好不要修改代码框架。
11. Please plot a typical UML class diagram with “object adapter” design pattern.
Simple Factory
12. What is the basic function (功能) of simple factory?
Create pointer to objects of a children class of a basic class.
Avoid any negative effects of future modifications on implementation classes.
13. Please give an example using simple factory.
class Juice {
public:
virtual void sell_juice() = 0;
};
class Apple_juice {
public:
void sell_juice() { cout << “Sell apple juice.” << endl; }
};
class Banana_juice {
public:
void sell_juice() { cout << “Sell banana juice.” << endl; }
};
enum Juice_type {APPLE, BANANA};
class Juice_factory {
Juice* create_juice(Juice_type type) {
Juice* juice = 0;
switch(type) {
case APPLE:
juice = new Apple_juice();
break;
case BANANA:
juice = new Banana_juice();
break;
default:
break;
}
return juice;
}
};
14. Please list some typical situations where simple factory is useful or necessary.