Skip to content

Commit ffbfaf2

Browse files
committed
updated
1 parent 49dd95f commit ffbfaf2

29 files changed

+672
-59
lines changed

.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package design.design_patterns.adapter.PaymentSys;
22

3+
4+
35
public interface PayD {
6+
47

58
public String getCustCardNo();
69

@@ -12,13 +15,11 @@ public interface PayD {
1215

1316
public Double getTotalAmount();
1417

15-
public void setCustCardNo(String custCardNo);
1618

17-
public void setCardOwnerName(String cardOwnerName);
1819

20+
public void setCustCardNo(String custCardNo);
21+
public void setCardOwnerName(String cardOwnerName);
1922
public void setCardExpMonthDate(String cardExpMonthDate);
20-
2123
public void setCVVNo(Integer cVVNo);
22-
2324
public void setTotalAmount(Double totalAmount);
2425
}
Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
11
package design.design_patterns.adapter.PaymentSys;
22

33

4+
5+
/*
6+
A software developer, Max, has worked on an e-commerce website. The website allows users
7+
to shop and pay online. The site is integrated with a 3rd party payment gateway, through
8+
which users can pay their bills using their credit card. Everything was going well, until
9+
his manager called him for a change in the project.
10+
11+
The manager told him that they are planning to change the payment gateway vendor, and he
12+
has to implement that in the code.
13+
14+
The problem that arises here is that the site is attached to the Xpay payment gateway which
15+
takes an Xpay type of object. The new vendor, PayD, only allows the PayD type of objects
16+
to allow the process. Max doesn’t want to change the whole set of 100 of classes which have
17+
reference to an object of type XPay. This also raises the risk on the project, which is
18+
already running on the production. Neither he can change the 3rd party tool of the payment
19+
gateway. The problem has occurred due to the incompatible interfaces between the two different
20+
parts of the code. In order to get the process work, Max needs to find a way to make the
21+
code compatible with the vendor’s provided API.
22+
*/
23+
424
public class RunAdapterExample {
525

26+
27+
private static void testPayD(PayD payD){
28+
29+
System.out.println(payD.getCardOwnerName());
30+
System.out.println(payD.getCustCardNo());
31+
System.out.println(payD.getCardExpMonthDate());
32+
System.out.println(payD.getCVVNo());
33+
System.out.println(payD.getTotalAmount());
34+
}
35+
636
public static void main(String[] args) {
737

838
// Object for Xpay
939
Xpay xpay = new XpayImpl();
40+
1041
xpay.setCreditCardNo("4789565874102365");
1142
xpay.setCustomerName("Max Warner");
1243
xpay.setCardExpMonth("09");
@@ -17,14 +48,4 @@ public static void main(String[] args) {
1748
PayD payD = new XpayToPayDAdapter(xpay);
1849
testPayD(payD);
1950
}
20-
21-
private static void testPayD(PayD payD){
22-
23-
System.out.println(payD.getCardOwnerName());
24-
System.out.println(payD.getCustCardNo());
25-
System.out.println(payD.getCardExpMonthDate());
26-
System.out.println(payD.getCVVNo());
27-
System.out.println(payD.getTotalAmount());
28-
}
29-
3051
}

Design_Patterns/Adapter/PaymentSystem/XpayImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public class XpayImpl implements Xpay{
66

7+
78
private String creditCardNo;
89
private String customerName;
910
private String cardExpMonth;
@@ -70,5 +71,4 @@ public void setCardCVVNo(Short cardCVVNo) {
7071
public void setAmount(Double amount) {
7172
this.amount = amount;
7273
}
73-
7474
}

Design_Patterns/Adapter/PaymentSystem/XpayToPayDAdapter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ public XpayToPayDAdapter(Xpay xpay){
1616
setProp();
1717
}
1818

19-
19+
// public void setCustCardNo(String custCardNo);
20+
// public void setCardOwnerName(String cardOwnerName);
21+
// public void setCardExpMonthDate(String cardExpMonthDate);
22+
// public void setCVVNo(Integer cVVNo);
23+
// public void setTotalAmount(Double totalAmount);
24+
2025
private void setProp(){
21-
setCardOwnerName(this.xpay.getCustomerName());
26+
2227
setCustCardNo(this.xpay.getCreditCardNo());
28+
setCardOwnerName(this.xpay.getCustomerName());
2329
setCardExpMonthDate(this.xpay.getCardExpMonth()+"/"+this.xpay.getCardExpYear());
2430
setCVVNo(this.xpay.getCardCVVNo().intValue());
2531
setTotalAmount(this.xpay.getAmount());

Design_Patterns/Bridge/CarAccessories/App.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,50 @@
11
package com.car;
22

3+
4+
/*
5+
Sec Security System is a security and electronic company which produces and assembles
6+
products for cars. It delivers any car electronic or security system you want, from
7+
air bags to GPS tracking system, reverse parking system etc. Big car companies use
8+
its products in their cars. The company uses a well defined object oriented approach
9+
to keep track of their products using software which is developed and maintained by
10+
them only. They get the car, produce the system for it and assemble it into the car.
11+
12+
Recently, they got new orders from BigWheel (a car company) to produce central locking
13+
and gear lock system for their new xz model. To maintain this, they are creating a new
14+
software system. They started by creating a new abstract class CarProductSecurity, in
15+
which they kept some car specific methods and some of the features which they thought
16+
are common to all security products. Then they extended the class and created two
17+
different sub classes named them BigWheelXZCentralLocking, and BigWheelXZGearLocking.
18+
19+
20+
After a while, another car company Motoren asked them to produce a new system of central
21+
locking and gear lock for their lm model. Since, the same security system cannot be used
22+
in both models of different cars, the Sec Security System has produced the new system for
23+
them, and also has created to new classes MotorenLMCentralLocking, and MotorenLMGearLocking
24+
which also extend the CarProductSecurity class.
25+
26+
27+
28+
Use of Bridge Pattern
29+
---------------------
30+
31+
You want to avoid a permanent binding between an abstraction and its implementation. This might
32+
be the case, for example, when the implementation must be selected or switched at run-time.
33+
34+
Both the abstractions and their implementations should be extensible by sub-classing. In this
35+
case, the Bridge pattern lets you combine the different abstractions and implementations and
36+
extend them independently.
37+
38+
Changes in the implementation of an abstraction should have no impact on clients; that is, their
39+
code should not have to be recompiled.
40+
41+
You want to share an implementation among multiple objects (perhaps using reference counting),
42+
and this fact should be hidden from the client.
43+
*/
344
public class App{
445

46+
47+
548
public static void main( String[] args ){
649

750
System.out.println();

Design_Patterns/Bridge/CarAccessories/Car.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public Car(Product product,String carType){
1414
this.carType = carType;
1515
}
1616

17-
public abstract void assemble();
1817
public abstract void produceProduct();
18+
public abstract void assemble();
1919

2020
public void printDetails(){
2121
System.out.println("Car: "+carType+", Product:"+product.productName());

Design_Patterns/Composite/HtmlTags/HtmlElement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
public class HtmlElement extends HtmlTag {
55

6-
76
private String tagName;
87
private String startTag;
98
private String endTag;
@@ -40,4 +39,5 @@ public void setTagBody(String tagBody) {
4039
public void generateHtml() {
4140
System.out.println(startTag + "" + tagBody + "" + endTag);
4241
}
43-
}
42+
}
43+

Design_Patterns/Composite/HtmlTags/HtmlParentElement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66

77
public class HtmlParentElement extends HtmlTag {
88

9+
910
private String tagName;
1011
private String startTag;
1112
private String endTag;
13+
1214
private List<HtmlTag> childrenTag;
1315

16+
1417
public HtmlParentElement(String tagName){
18+
1519
this.tagName = tagName;
1620
this.startTag = "";
1721
this.endTag = "";
@@ -50,11 +54,13 @@ public void removeChildTag(HtmlTag htmlTag){
5054

5155
@Override
5256
public void generateHtml() {
57+
5358
System.out.println(startTag);
59+
5460
for(HtmlTag tag : childrenTag){
5561
tag.generateHtml();
5662
}
57-
System.out.println(endTag);
5863

64+
System.out.println(endTag);
5965
}
6066
}

Design_Patterns/Composite/HtmlTags/HtmlTag.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package design.design_patterns.composite.htmltags;
22

3-
4-
/*The Composite Pattern has four participants:
5-
---------------------------------------------
6-
1. Component
7-
2. Leaf
8-
3. Composite
9-
4. Client
10-
--------------------------------------------*/
11-
123
import java.util.List;
134

5+
146
public abstract class HtmlTag {
157

168
public abstract String getTagName();

Design_Patterns/Composite/HtmlTags/TestCompositePattern.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,52 @@
11
package design.design_patterns.composite.htmltags;
22

3+
/*
4+
Composite Pattern
5+
-----------------
6+
7+
The English meaning of the word Composite is something that is made up of complicated and related
8+
parts. The composite means “putting together” and this is what this design pattern is all about.
9+
10+
There are times when you feel a need of a tree data structure in your code. There are many variations
11+
to the tree data structure, but sometimes there is a need of a tree in which both branches as well
12+
as leafs of the tree should be treated as uniformly.
13+
14+
The Composite Pattern allows you to compose objects into a tree structure to represent the part-whole
15+
hierarchy which means you can create a tree of objects that is made of different parts, but that
16+
can be treated as a whole one big thing. Composite lets clients to treat individual objects and
17+
compositions of objects uniformly, that’s the intent of the Composite Pattern.
18+
19+
There can be lots of practical examples of the Composite Pattern. A file directory system, an html
20+
representation in java, an XML parser all are well managed composites and all can easily be represented
21+
using the Composite Pattern. But before digging into the details of an example, let’s see some
22+
more details regarding the Composite Pattern.
23+
24+
25+
The formal definition of the Composite Pattern says that it allows you to compose objects into tree
26+
structures to represent part-whole hierarchies. Composite lets clients to treat individual objects
27+
and compositions of objects uniformly.
328
29+
If you are familiar with a tree data structure, you will know a tree has parents and their children.
30+
There can be multiple children to a parent, but only one parent per child. In Composite Pattern,
31+
elements with children are called as Nodes, and elements without children are called as Leafs.
32+
33+
The Composite Pattern allows us to build structures of objects in the form of trees that contains
34+
both composition of objects and individual objects as nodes. Using a composite structure, we can
35+
apply the same operations over both composites and individual objects. In other words, in most
36+
cases we can ignore the differences between compositions of objects and individual objects.
37+
38+
39+
40+
The Composite Pattern has four participants:
41+
42+
Component
43+
44+
Leaf
45+
46+
Composite
47+
48+
Client
49+
*/
450
public class TestCompositePattern {
551

652
/*
@@ -16,6 +62,7 @@ public class TestCompositePattern {
1662
</html>
1763
*/
1864

65+
1966
public static void main(String[] args) {
2067

2168
// parent elements

Design_Patterns/Facade/ScheduleServer/RunServer.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,78 @@
11
package com.javacodegeeks.patterns.facadepattern;
22

3+
4+
5+
/*
6+
7+
Your company is a product based company and it has launched a product in the market, named
8+
Schedule Server. It is a kind of server in itself, and it is used to manage jobs. The jobs
9+
could be any kind of jobs like sending a list of emails, sms, reading or writing files from
10+
a destination, or just simply transferring files from a source to the destination. The product
11+
is used by the developers to manage such kind of jobs and able to concentrate more towards
12+
their business goal. The server executes each job at their specified time and also manages
13+
all underline issues like concurrency issue and security by itself. As a developer, one just
14+
need to code only the relevant business requirements and a good amount of API calls is provided
15+
to schedule a job according to their needs.
16+
17+
Everything was going fine, until the clients started complaining about starting and stopping
18+
the process of the server. They said, although the server is working great, the initializing
19+
and the shutting down processes are very complex and they want an easy way to do that. The
20+
server has exposed a complex interface to the clients which looks a bit hectic to them.
21+
22+
We need to provide an easy way to start and stop the server.
23+
24+
A complex interface to the client is already considered as a fault in the design of the current
25+
system. But fortunately or unfortunately, we cannot start the designing and the coding from
26+
scratch. We need a way to resolve this problem and make the interface easy to access.
27+
28+
29+
30+
Facade Pattern
31+
--------------
32+
33+
The Facade Pattern makes a complex interface easier to use, using a Facade class. The Facade
34+
Pattern provides a unified interface to a set of interface in a subsystem. Facade defines a
35+
higher-level interface that makes the subsystem easier to use.
36+
37+
The Facade unifies the complex low-level interfaces of a subsystem in-order to provide a simple
38+
way to access that interface. It just provides a layer to the complex interfaces of the sub-system
39+
which makes it easier to use.
40+
41+
42+
A Facade is not just only able to simplify an interface, but it also decouples a client from a
43+
subsystem. It adheres to the Principle of Least Knowledge, which avoids tight coupling between
44+
the client and the subsystem. This provides flexibility: suppose in the above problem, the company
45+
wants to add some more steps to start or stop the Schedule Server, that have their own different
46+
interfaces. If you coded your client code to the facade rather than the subsystem, your client
47+
code doesn’t need to be change, just the facade required to be changed, that’s would be delivered
48+
with a new version to the client.
49+
50+
51+
52+
Usage of the Facade Pattern
53+
---------------------------
54+
55+
i. You want to provide a simple interface to a complex subsystem. Subsystems often get more
56+
complex as they evolve. Most patterns, when applied, result in more and smaller classes. This
57+
makes the subsystem more reusable and easier to customize, but it also becomes harder to use
58+
for clients that don’t need to customize it. A facade can provide a simple default view of the
59+
subsystem that is good enough for most clients. Only clients needing more customizability will
60+
need to look beyond the facade.
61+
62+
ii. There are many dependencies between clients and the implementation classes of an abstraction.
63+
Introduce a facade to decouple the subsystem from clients and other subsystems, thereby promoting
64+
subsystem independence and portability.
65+
66+
iii. You can layer your subsystems. Use a facade to define an entry point to each subsystem level.
67+
If subsystems are dependent, then you can simplify the dependencies between them by making them
68+
communicate with each other solely through their facades.
69+
*/
70+
71+
372
public class RunServer {
473

574

75+
676
public static void main(String[] args) {
777

878
ScheduleServer scheduleServer = new ScheduleServer();

0 commit comments

Comments
 (0)