-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathsolutions.txt
781 lines (698 loc) · 15 KB
/
solutions.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
P6
*************************** interface typing
interface Tax{
double compute(double amount);
}
public class Invoice {
private Tax taxType;
Tax getTaxType() {
return taxType;
}
void setTaxType(Tax taxType) {
this.taxType = taxType;
}
public double getTotal()
{
double amount = getSubtotal();
amount += taxType.compute(amount);
return amount;
}
double getSubtotal()
{
double subtotal = 0;
...logic
return subtotal;
}
}
*************************** Duck typing
public class Invoice {
private object taxType;
void setTaxType(object taxType) {
this.taxType = taxType;
}
public double getTotal()
{
double amount = getSubtotal();
amount += taxType.compute(amount);
return amount;
}
double getSubtotal()
{
double subtotal = 0;
...logic
return subtotal;
}
}
P6 -2
public class Invoice {
private Lamda taxType;
Invoice(Lamda taxType) {
this.taxType = taxType;
}
public double getTotal(){
double amount = getSubtotal();
amount += taxType(amount);
return amount;
}
double getSubtotal(){
...
}
}
Invoice inv = new Invoice(new Gst()); //GST should belong to Tax family
Invoice inv = new Invoice((amount)=> amount*0.05);
Invoice inv = new Invoice((amount)=> {
Gst gst = new GST();
return gst.compute(amount);
});
P23
class Domain{
fun(){
... logic
if(cond)
throw Ex;
... logic
}
}
void DoJob()
{
Domain domain = new Domain();
domain.fun();
domain.fun2(100);
Repostory rep = new Repository();
Emp emp = rep.get(1);
...
}
P1
interface State{
start();
stop();
pause();
resume();
}
class Idle implement State{
start(){ invoke logic }
stop() { throw new InvalidState(); }
pause() { throw }
resume(){ throw }
}
class Running implement State{
start(){ throw }
stop() { invoke logic }
pause() { invoke logic }
resume(){ throw }
}
class Suspended implement State{
start(){ throw }
stop() { invoke logic }
pause() { throw }
resume(){ invoke logic }
}
public class StopWatch {
State flag = new Idle();
public void start(){ flag.start(); flag= new Running(); }
public boolean stop(){ flag.stop(); flag= new Idle(); }
public boolean pause(){ flag.pause(); flag= new Suspended(); }
public boolean resume(){ flag.resume(); flag= new Running(); }
}
P2
/*********************************************** flag
class AccountService
{
Stack<Operation> stack = new Stack<Operation>();
Account acc = new Account();
public void withdraw(double amount){
acc.withdraw(amount);
Operation op = new Operation(1,amount);
stack.push(op);
}
public void deposit(double amount){
acc.deposit(amount);
Operation op = new Operation(2,amount);
stack.push(op)
}
public void undo(){
Operation op = stack.pop();
if(op.Type == 1)
acc.deposit(op.amount);
if(op.Type == 2)
acc.withdraw(op.amount);
}
}
/*********************************************** inteface
interface Operation{
void undo(Account acc);
}
class DepositOperation{
double amount;
public DepositOperation( double amount){
this.amount = amount;
}
public void undo(Account acc){
acc.withdraw(amount);
}
}
class WithdrawOperation{
double amount;
public WithdrawOperation( double amount){
this.amount = amount;
}
public void undo(Account acc){
acc.deposit(amount);
}
}
class AccountService
{
Stack<Operation> stack = new Stack<Operation>();
Account acc = new Account();
public void withdraw(double amount){
acc.withdraw(amount);
Operation op = new WithdrawOperation(amount);
stack.push(op);
}
public void deposit(double amount){
acc.deposit(amount);
Operation op = new DepositOperation(amount);
stack.push(op)
}
public void undo(){
Operation op = stack.pop();
op.undo(acc);
}
}
/*********************************************** lambda
class AccountService
{
Stack<Lamda> stack = new Stack<Lamda>();
Account acc = new Account();
public void withdraw(double amount){
acc.withdraw(amount);
stack.push((acc)=> acc.deposit(amount));
}
public void deposit(double amount){
acc.deposit(amount);
stack.push((acc)=> acc.withdraw(amount));
}
public void undo(){
Lamda op = stack.pop();
op(acc);
}
}
P21
interface Encryption{
String encrypt(String message);
String decrypt(String message);
}
class AESEncrption implements Encryption{
String encrypt(String message){
//logic
}
String decrypt(String message){
//logic
}
}
class BlowFishEncrption implements Encryption{
String encrypt(String message){
//logic
}
String decrypt(String message){
//logic
}
}
public class Message {
private Encryption algorithmName;
private String text;
public Message(Encryption algorithmName){
this.algorithmName=algorithmName;
}
public string getText(){
return algorithmName.decrypt(text);
}
public void setText(String text){
text = algorithmName.encrypt(text);
}
}
P22
for(Vehicle vehicle : vehicles) {
vehicle.action();
vehicle.stop();
}
Interface Vehicle {
action();
stop();
}
Class CAR implements Vehicle {
action(){
lock();
go();
}
}
Class SHIP implements Vehicle {
action() {
balance();
swim();
}
}
Class AIRPLANE implements Vehicle {
action(){
go();
fly();
}
}
Class TANK implements Vehicle {
action() {
move();
stop();
fire();
}
}
P24
interface IX
{
void f1();
void f2();
}
public class CA implements IX
{
public void f1() {}
public void f2() {}
}
class IXFactory
{
public IX getInsatnce(){
return new CA();
}
}
//*******************************
class Client
{
public static void main()
{
IXFactory factory = new IXFactory();
IX obj = factory.getInsatnce();
obj.f1();
obj.f2();
}
}
P11
public class Stock {
double rate;
public void changeRate(double newRate, Broker broker) {
rate = newRate;
//if(condition)
broker.trade();
}
}
interface Broker {
void trade();
}
P3
package problem3;
public class Workflow{
public void execute(Factory factory){
Connection connection = factory.getConnection();
connection.open("mydb;scott;tiger");
Transaction transaction = factory.getTransaction();
transaction.begin(connection);
Command cmd1 = factory.getCommand();
cmd1.execute(transaction, "insert into emp values(10,'jack',2500')");
Command cmd2 = factory.getCommand();
cmd2.execute(transaction, "insert into emp values(20,'jill',4300')");
transaction.commit();
connection.close();
}
}
public class Entry {
public static void main() {
Workflow wf = new Workflow();
wf.execute(new SqlFactory());
}
}
P3 - 2
interface Connection{
open();
close();
}
class PostConnection{
connect();
disconnect();
}
class PostConnectionAdapter implements Connection{
PostConnection connection = new PostConnection();
open(){
connection.connect();
}
close();
}
class SqlConnection implements Connection{
open();
close();
}
class OraConnection implements Connection{
open();
close();
}
P25
class SurveyDataType:
public void SurveyDataType(string baseFileName, bool hideDataFile){
this.baseFileName = baseFileName;
this.hideDataFile = "c:/application/data/" + baseFileName + ".dat";
}
static SurveyDataType rawDataType = SurveyDataType("raw", True);
static SurveyDataType cleanedUpDataType = SurveyDataType("cleanedUp", True);
static SurveyDataType processedDataType = SurveyDataType("processed", True);
static SurveyDataType publicationDataType = SurveyDataType("publication", False);
}
v1 = SurveyDataType.processedDataType.baseFileName;
v2 = SurveyDataType.processedDataType.hideDataFile;
P20
interface Account {
void withdraw(double amount);
void deposit(double amount);
}
interface AccountDAO{
void persisit();
}
class AccountService{
void doTransaction(Account acc,AccountDAO dao)
{
acc.withdraw(3434);
acc.deposit(34);
...
dao.persist(acc);
...
}
}
abstarct class Account
{
public abstract withdraw(double amount);
public abstract deposit(double amount);
protected abstarct persisit();
public void Save()
{
... logic 1
persisit();
... logic 2
}
}
P8
class Collection{
... common code ...
public void add(index, item) {}
}
class Stack extends Collection{}
class Queue extends Collection{}
class List extends Collection{}
class Stack {
Collection ref = new Collection();
}
class Queue{
Collection ref = new Collection();
}
class List {
Collection ref = new Collection();
}
p7
interface AccountType {
boolean withDraw(double amount);
}
interface AccountPriv {
boolean withDraw(double amount);
}
class SavingAccount implements AccountType{
boolean withDraw(double amount){
if((balance - amount) < 5000){
return false;
}
}
}
class CurrentAccount implements AccountType{
boolean withDraw(double amount){
if((balance - amount) < 100000){
return false;
}
}
}
class SilverAccount implements AccountPriv{
boolean withDraw(double amount){
if(amount > 10000){
return false;
}
}
}
class PlatinumAccount implements AccountPriv{
boolean withDraw(double amount){
if(amount > 20000){
return false;
}
}
}
class AccountService{
AccountType type;
AccountPriv priv;
public AccountService( AccountType type,AccountPriv priv){
this.type = type;
this.priv = priv;
}
boolean withDraw(double amount){
if(!type.withdraw(amount))
return false;
if(!priv.withdraw(amount))
return false;
}
}
P4
package problem4;
interface Step {
void invoke(Plugin p);
}
Start implements Step {
void invoke(Plugin p) {
p.do(this);
}
}
interface Plugin{
void do (Start);
void do (Action);
void do (Branch);
void do (Stop);
}
Class TreeSerializer implements Plugin {
void do (Start start){
...logic...
start.next.invoke(this);
}
void do (Action action){
... logic ...
action.next.invoke(this);
}
void do (Branch branch){
... logic ...
branch.left.invoke(this);
... logic ...
branch.right.invoke(this);
}
void do (Stop stop){
... logic ...
}
}
Class TreePrinter implements Plugin {
void do (Start start){
System.out.println("start");
start.next.invoke(this);
}
void do (Action action){
System.out.println("Action");
action.next.invoke(this);
}
void do (Branch branch){
System.out.println("Branch");
System.out.println("--> left");
branch.left.invoke(this);
System.out.println("--> rigth");
branch.right.invoke(this);
}
void do (Stop stop){
System.out.println("stop");
}
}
public class Entry {
public static void main(String[] args) {
Factory factory = new Factory();
Step root = factory.Load("flow1");
TreePrinter treePrinter = new TreePrinter();
root.invoke(treePrinter);
TreeSerializer treeSerializer = new TreeSerializer();
root.invoke(treeSerializer);
}
}
P13
lookup
//************** s3 ********************************
interface Handler{
void collide(GO o1,GO o2);
}
class ShipStation implments Handler{
}
class ShipCommet implments Handler{
}
...
class HandlerFactory{
Dictionary<string,Handler> lookup = new Dictionary<string,Handler>();
public HandlerFactory(){
lookup.add("ShipStation",new ShipStation());
....
}
public Handler Get(GO o1,GO o2){
return lookup[o1.getName() + o2.getName()];
}
}
public void Collide(GameObject go1,GameObject go2)
{
HandlerFactory factory = new HandlerFactory();
Handler handler = factory.Get(go1,go2);
handler.collide(go1,go2);
}
lhs -> poly, rhs -> if
//************** s2 ********************************
interface GO{
void collide(Ship);
void collide(Station);
void collide(Commet);
void collide (Aestroid);
}
class Ship implements GO{
void collide(Ship) {} //ship - ship
void collide(Station) {} // ship - station
void collide(Commet){} // ship - commet
void collide (Aestroid){} // ship - Aestroid
}
class Handler
{
public void Collide(GameObject go1,GameObject go2)
{
if(type(go2) == type(Ship))
go1.collide((Ship) go2);
if(type(go2) == type(Station))
go1.collide((Station) go2);
if(type(go2) == type(Commet))
go1.collide((Commet) go2);
if(type(go2) == type(Aestroid))
go1.collide((Aestroid) go2);
}
}
lhs -> if, rhs -> if
//************** s1 ********************************
class Handler
{
public void Collide(GameObject go1,GameObject go2)
{
if(type(o1)==type(Ship) && type(o2)==type(Station))
{
//logic1
}
if(type(o1)==type(Station) && type(o2)==type(Aestroid))
{
//logic2
}
if(type(o1)==type(Ship) && type(o2)==type(Commet))
{
//logic3
}
}
}
P5
interface Rule
{
bool eval(HashMap<string,string> obj);
}
class GreaterRule implements Rule{
string property;
double value;
public GreaterRule(string property,double value){
this.property = property;
this.value = value;
}
bool eval(HashMap<string,object> obj){
cur_value = obj.get(property);
if(double.parse(cur_value) > value)
return true;
else
return false;
}
}
class LesserRule implements Rule{
string property;
double value;
public LesserRule(string property,double value){
this.property = property;
this.value = value;
}
bool eval(HashMap<string,object> obj){
cur_value = obj.get(property);
if(double.parse(cur_value) < value)
return true;
else
return false;
}
}
class EqualStringRule implements Rule{
string property;
string value;
public EqualStringRule(string property,string value){
this.property = property;
this.value = value;
}
bool eval(HashMap<string,object> obj){
cur_value = obj.get(property);
if(cur_value.equals(value))
return true;
else
return false;
}
}
class AndRule implements Rule{
Rule lhs;
Rule rhs;
public AndRule(Rule lhs,Rule rhs){
this.lhs = lhs;
this.rhs = rhs;
}
bool eval(HashMap<string,object> obj){
return lhs.eval(obj) && rhs.eval(obj);
}
}
HashMap<string,string> object = new HashMap<>();
object.put("Salary", 3500);
object.put("Age", 45);
object.put("Department", "Sales");
//Composite Pattern
Rule r1 = new GreaterRule("Salary",5000.00);
Rule r2 = new LesserRule("Age",50);
Rule r3 = new EqualStringRule("Department","Purchase");
Rule r4 = new AndRule(r1,r2);
Rule r5 = new OrRule(r4,r3);
res = r5.eval(object);
//"((Salary > 5000 && Age < 50) || (Department == 'Purchase'))"
System.out.print(res);
P10
interface Account{
void withDraw(double Amount);
}
class SA implements Account{
public void withDraw(double Amount){
balance-=amount;
}
}
class Otp implements Account{
Account a;
public Otp(Account a){
this.a = a;
}
public void withDraw(double Amount){
... pre otp logic ..
a.withdraw(amount);
... post otp logic ..
}
}
Account a = new Sms(new Otp(new SA()));
a.withdraw(340);