123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Education >> View Article

Java Learning Center | Java Language Course | Onlineitguru

Profile Picture
By Author: divya
Total Articles: 3
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

What are Design patterns in Java?
For over 40 years now, beginning with Smalltalk, which was the first object-oriented language, we have been developing object-oriented applications.
A large number of problems have been found in the programming world, and a range of solutions have been suggested to tackle them.
A group of four people, famously called the "Gang-Of-Four" or GoF, attempted to come up with a collection of common problems and solutions in the given sense for them.
Why Java Patterns in Design?
The benefits of layout patterns are:
● Providing common terms that everybody knows.
What are Java Design Pattern Types?
From the viewpoint of an object-oriented environment, the design trends we are talking about here. Three different kinds of design trends are mainly available.
● Patterns of Creation
● Patterns of structures
● Conductual Trends
● Creative
● Creative patterns deal with object formation.
● Structural Areas
Structural patterns deal with object structure.
It deals with such issues as:
Conductual
...
... Conduct trends concentrate mostly on object behavior, or, most precisely, object-to-object interactions.
How does an object interact with a particular object?
Visual style trends
Exploring Trends in Creational Architecture
We investigate the following trends of innovative design:
Patterns in Creational Architecture
The Pattern for the Prototype
A prototype, to be replicated or cloned, represents a completely initialized example.
Let's take an instance:
Let's take a chess game's architecture into account. The king, queen, rook, bishop, knight, and pawns all have their unique positions. Let's say we want to develop chess game modeling software.
We need to build the initial board layout every time a new Chess game is played.
Instead of repeating the chessboard development every time,
We may build an object containing the initial configuration.
Clone from it, every time a new game of chess is played.
The entity with the initial chessboard configuration is the prototype. And, we use the pattern of the prototype.
Original layout in Java
You have a completely initialized instance in the prototype pattern. The original board layout is readily accessible here.
This initialized instance is simply copied or cloned whenever a new chess game is started, for instance, in any of the various online Chess portals.
A pattern for the Builder in Java
The Java Builder Pattern distinguishes the creation of objects from their representation. What is that meant to mean?
Assume that you are going out to a restaurant for a multi-course meal. There will be a lot of choices for such a meal, such as starters, main courses, and desserts. Out of the given options, you will likely select two or three. A specific client may only want to have dinner with the first two choices, leaving out the choice for desserts. Yet, the main course and desserts would choose another, skipping the starters entirely.
In developing software, similar circumstances may occur. You might need to create an object using a subset of the available options. Or, the object can need to be generated in many ways. This is where the pattern of the function Object() { [native code] } comes in handy.
Let 's look at a tiny piece of code to better comprehend it.
public class BuilderPattern {
static class Coffee {
private Coffee(Builder builder) {
this.type = builder.type;
this.sugar = builder.sugar;
this.milk = builder.milk;
this.size = builder.size;
}
private String type;
private boolean sugar;
private boolean milk;
private String size;
public static class Builder {
private String type;
private boolean sugar;
private boolean milk;
private String size;
public Builder(String type) {
this.type = type;
}
public Builder sugar(boolean value) {
this.sugar = value;
return this;
}
public Builder milk(boolean value) {
this.milk = value;
return this;
}
public Builder size(String value) {
this.size = value;
return this;
}
public Coffee build() {
return new Coffee(this);
}
}
@Override
public String toString() {
}
}
public static void main(String[] args) {
Coffee coffee = new BuilderPattern.Coffee.Builder("Mocha").milk(true).sugar(false).size("Large").build();
System.out.println(coffee);
}
}
Let's say you're writing a computer program that prepares coffee. Coffee, milk, and sugar are the primary coffee ingredients.
You chose whether you have sugar and milk or not, depending on which part of the world you are from.
In order to provide you with these coffee options, the function Object() { [native code] } pattern steps in.
Look at the principal () code within the code.
Java Code builder
A Builder is what we have inside the coffee, to whom we pass the mandatory form of coffee. We make other calls chained to that call, adding the other ingredients to our tastes.
It can easily be designed by someone else who wants a different coffee. In constructing objects, this contributes to a large amount of versatility.
There are several inherent problems with other approaches to solving this question, such as the use of setters. This approach leads to hard-to-read code that often acts erratically in multithreaded programs. All those problems are solved by the function Object() { [native code] } pattern.
The benefit of using the pattern of the function Object() { [native code] } is Simplifies the creation of artefacts.
The Java Pattern for Singleton
Of all the design patterns, the singleton pattern is the most common. What this pattern does is very clear from its name-it makes at any point in time only one instance of a class per JVM.
The President of a country will be a good real-world analogy.
There is a disclaimer here, however, that there can only be one instance of that class, per JVM. If you have a Java program that is running as part of an application server cluster, each server runs a separate instance of a JVM. Therefore, at any given time, you are permitted to have one instance of the singleton generated on each application server.
Whenever you create a singleton class, there are a couple of things to note.
● To avoid the risk of other objects making examples of your class, the builder needs to be private.
● Build a Singleton in Java using an Enum.
● JEE 7, along with other similar annotations, has a built-in annotation called @Singleton.
The primary drawback of using the singleton pattern is that it is difficult to unit test the resulting code. Make a straightforward decision as to when, and where, you need to use a singleton.
The objects that are handled are called beans in frameworks like Spring, and beans are singletons by design.
The Java Pattern of Factory Process
The purpose of the pattern of the factory method is to create a family of types of objects.
public class FactoryPattern {
public static class PersonFactory {
if(gender.equalsIgnoreCase("M")) {
return new Male(name);
} else if(gender.equalsIgnoreCase("F")) {
return new Female(name)
} // So on
return null;
}
}
}
static abstract class Person {
Person(String name) {
this.name = name;
}
private String name;
abstract String getSalutation();
String getNameAndSalutation() {
}
}
static class Male extends Person {
public Male(String name) {
super(name);
}
@Override
String getSalutation() {
return "Mr";
}
}
static class Female extends Person {
public Female(String name) {
super(name);
}
@Override
String getSalutation() {
return "Miss/Mrs";
}
}
public static void main(String[] args) {
Person male = PersonFactory.getPerson("Robinhood", "M");
System.out.println(male.getNameAndSalutation);
Person female = PersonFactory.getPerson("Mary", "F");
System.out.println(female.getNameAndSalutation);
}
Conclusion
I hope you reach to a conclusion about design patterns in java. You can learn more through Java online training where IT Guru offers the best learning.

Total Views: 169Word Count: 1842See All articles From Author

Add Comment

Education Articles

1. Decoding Project Management Certifications: Pmp Or Capm?
Author: Myank

2. Best School In Rohini
Author: Sanchit Arora

3. Exploring The Foundations Of Islamic Learning: From Qur'anic Studies To Jurisprudence
Author: faizalkhan1393

4. Jain University Online Bcom
Author: Collegetour

5. Mastering Digital Marketing: Unlocking The Power Of Salesforce Marketing Cloud With Proximsoft
Author: proximsoft

6. Horizon Scholar: Your Source For Personalized University Selection Guidance
Author: Horizon Scholar

7. Advancing Knowledge: Machine Learning Research Proposal Services In Kisumu, Kenya
Author: elaine

8. A Guide To Ms In Business Analytics In Canada
Author: Shilpa Arora

9. The Psychology Of Learning: Understanding Cognitive Processes And Educational Strategies
Author: anwarkhan45314

10. Data Analytics Online Training Hyderabad | Data Analytics Training
Author: Teja

11. Curricular Innovation In Education: Adapting To The Changing Needs Of Students
Author: amankhan99101

12. The Heartbeat Of Windsor Gardens Primary School
Author: Avenues College

13. Buy Research Proposal In Nairobi, Kenya: A Comprehensive Guide
Author: elaine

14. Empowering Minds: Exploring Counseling Techniques For Personal Growth And Healing
Author: abdulshaikh5253

15. Exploring Earth's Extremes: From Antarctica To The Sahara Desert
Author: Madhu Yadav

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: