Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3e9587

Browse files
committed
Added few answers to design patterns questions. Minor changes in other files
1 parent 3fed295 commit b3e9587

File tree

3 files changed

+86
-15
lines changed

3 files changed

+86
-15
lines changed

Design Patterns.txt

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,95 @@
11
------------------------------------------------------------------------------------------
2-
Software Design and Design Patterns
2+
Design Patterns
33
------------------------------------------------------------------------------------------
44

55
1. What are some alternatives to inheritance?
6+
7+
One of general recommendations (or some call it OOP design principles) is to favor composition over inheritance.
8+
For example in strategy pattern the client object is composed with a behavior object (strategy) and this behavior can be changed in runtime. Favoring composition over inheritance leads to a more flexible design. (Of course in each case a separate decision whether to subclass or to compose with an object has to be made. There is no universal approach)
9+
610
2. Give an example where you prefer abstract class over interface ?
11+
12+
One good example comes from java.awt package. There is an abstract class MouseAdapter that implements several interfaces MouseListener, MouseWheelListener, MouseMotionListener. In general there is no need to write a class that implements all the methods from e.g. MouseListener interface. Instead we can subclass from MouseAdapter and override only the methods that we're interested in.
13+
14+
In general abstract class should be used when there is some default behavior that should be reused. The motivation behind this is changes that are expected to be done in future. If you change some method implementation in an abstract class, all its subclasses will automatically have this updated behavior. If you change an interface -- all classes that implement it will be broken. Another good way to think about abstract class vs interface is thinking about "is a" vs "can be" relation
15+
16+
Example:
17+
18+
TODO: find a good example
19+
720
3. What are wrapper classes? Why do we need wrapper classes?
8-
4. Can you please explain “Strategy” design pattern?
9-
5. Can you please explain “Observer” design pattern? Observer and Observable in Java.
21+
22+
23+
24+
4. Can you please explain Strategy design pattern?
25+
26+
27+
28+
5. Can you please explain Observer design pattern? Observer and Observable in Java
29+
30+
31+
.
1032
6. Give example of decorator design pattern in Java ? Does it operate on object level or class level ?
33+
34+
35+
1136
7. What is Adapter design pattern ? Give examples of adapter design pattern in Java?
37+
38+
39+
1240
8. What is Chain of Responsibility design pattern ?
13-
9. What is Singleton design pattern in Java ? write code for thread-safe singleton in Java
41+
42+
43+
44+
9. What is Singleton design pattern in Java ? Write code for thread-safe singleton in Java
45+
46+
47+
1448
10. Give an insight into such patterns as Façade/Proxy/Decorator/Strategy/Observer (selectively)
49+
50+
51+
1552
11. What is main benefit of using factory pattern ? Where do you use it?
16-
12. What is MVC design pattern ? Give one example of MVC design pattern ?
53+
54+
55+
56+
12. What is MVC design pattern ? Give one example of MVC design pattern?
57+
58+
59+
1760
13. Which patterns do you use in a daily basis. Explain their principles.
61+
62+
63+
1864
14. Can you name few design patterns used in standard JDK library? What major patterns do the Java APIs utilize?
19-
15. Design a Concurrent Rule pipeline in Java?
20-
16. Design a Vending Machine which can accept different coins, deliver different products?
21-
17. You have a Smartphone class and will have derived classes like IPhone, AndroidPhone,WindowsMobilePhone can be even phone names with brand, how would you design this system of Classes.
22-
18. Design ATM Machine ?
23-
19. You are writing classes to provide Market Data and you know that you can switch to different vendors overtime like Reuters, wombat and may be even to direct exchange feed , how do you design your Market Data system.
24-
20. What is FrontController design pattern in Java ? Give an example of front controller pattern ?
65+
66+
Decorator design pattern is widely used in Java IO and to provide unmodifiable and/or synchronized views for collections
67+
68+
TODO: example
69+
70+
Factory methods and factories
71+
72+
TODO: example
73+
74+
Observers are used in java.awt to process keyboard and mouse events
75+
76+
TODO: example
77+
78+
Prototype: clone() method
79+
80+
Strategy: interface Comparable and compare() method
81+
82+
TODO: example
83+
84+
MVC in Swing libraries
85+
86+
15. What is FrontController design pattern in Java ? Give an example of front controller pattern ?
87+
88+
------------------------------------------------------------------------------------------
89+
Software Design questions
90+
------------------------------------------------------------------------------------------
91+
1. Design a Concurrent Rule pipeline in Java?
92+
2. Design a Vending Machine which can accept different coins, deliver different products?
93+
3. You have a Smartphone class and will have derived classes like IPhone, AndroidPhone,WindowsMobilePhone can be even phone names with brand, how would you design this system of Classes.
94+
4. Design ATM Machine ?
95+
5. You are writing classes to provide Market Data and you know that you can switch to different vendors overtime like Reuters, wombat and may be even to direct exchange feed , how do you design your Market Data system.

Java Concurrency.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ Concurrency
132132
String s1 = "S1";
133133
String s2 = "S2";
134134

135-
Thread t1 = new Thread(new deadlockCause(s1, s2));
136-
Thread t2 = new Thread(new deadlockCause(s2, s1));
135+
Thread t1 = new Thread(new DeadlockCause(s1, s2));
136+
Thread t2 = new Thread(new DeadlockCause(s2, s1));
137137

138138
t1.start();
139139
t2.start();
@@ -158,7 +158,7 @@ Concurrency
158158
try {
159159
Thread.sleep(1000);
160160
} catch (InterruptedException ex) {
161-
Logger.getLogger(deadlockCause.class.getName()).log(Level.SEVERE, null, ex);
161+
Logger.getLogger(DeadlockCause.class.getName()).log(Level.SEVERE, null, ex);
162162
}
163163

164164
System.out.println(Thread.currentThread().getName() + " tries to get the lock on " + secondLock);

Java Exceptions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Exceptions
6868
16. Pros and cons of using exceptions vs. returning "error value"
6969

7070
Exceptions provide clearer understanding and better code
71-
Exceptions can be grouped together to be hanlded
71+
Exceptions can be grouped together to be handled
7272
Exceptions can be propagated up in the call stack to be handled where they are supposed to be handled.
7373
Exception handling could be slow.
7474
In some sense Exceptions are unignorable error codes.

0 commit comments

Comments
 (0)