Professional Documents
Culture Documents
But remember one-thing, design patterns are programming language independent strategies for solving the
common object-oriented design problems. That means, a design pattern represents an idea, not a particular
implementation.
By using the design patterns you can make your code more flexible, reusable and maintainable. It is the most
important part because java internally follows design patterns.
To become a professional software developer, you must know at least some popular solutions (i.e. design patterns)
to the coding problems.
2. They provide the solutions that help to define the system architecture.
5. They are well-proved and testified solutions since they have been built upon the knowledge and
experience of expert software developers.
6. Design patterns don?t guarantee an absolute solution to a problem. They provide clarity to the system
architecture and the possibility of building a better system.
But everyone knows an object is created by using new keyword in java. For example:
StudentRecord s1=new StudentRecord();
Hard-Coded code is not the good programming approach. Here, we are creating the instance by using the new
keyword. Sometimes, the nature of the object must be changed according to the nature of the program. In such
cases, we must get the help of creational design patterns to provide more general and flexible approach.
1. Singleton Pattern
2. Factory Pattern
3. Abstract Factory Pattern
4. Builder Pattern
5. Prototype Pattern
Structural design patterns
Structural design patterns are concerned with how classes and objects can be composed, to form larger
structures.
These patterns focus on, how the classes inherit from each other and how they are composed from other classes.
1. Adapter Pattern
2. Composite Pattern
3. Proxy Pattern
4. Flyweight Pattern
5. Facade Pattern
6. Bridge Pattern
7. Decorator Pattern
Behavioral Design Patterns
Behavioral design patterns are concerned with the interaction and responsibility of objects.
In these design patterns, the interaction between the objects should be in such a way that they can easily
talk to each other and still should be loosely coupled.
That means the implementation and the client should be loosely coupled in order to avoid hard coding and
dependencies.
A singleton is a class that is instantiated only once. This is typically accomplished by creating
a static field in the class representing the class. A static method exists on the class to obtain
the instance of the class and is typically named something such as getInstance(). The creation
of the object referenced by the static field can be done either when the class is initialized or
the first time that getInstance() is called. The singleton class typically has a private
constructor to prevent the singleton class from being instantiated via a constructor. Rather,
the instance of the singleton is obtained via the static getInstance() method.
SingletonExample.java
package com.cakes;
private SingletonExample() {
}
Demo.java
package com.cakes;
singletonExample.sayHello();
}
}
Singleton classes are a useful way of concentrating access to particular resources into a single class instance.
Factory Pattern
The factory pattern (also known as the factory method pattern) is a creational design pattern. A
factory is a JavaSW class that is used to encapsulate object creation code. A factory class instantiates
and returns a particular type of object based on data passed to the factory. The different types of
objects that are returned from a factory typically are subclasses of a common parent class.
The data passed from the calling code to the factory can be passed either when the factory is created
or when the method on the factory is called to create an object. This creational method is often called
something such as getInstance or getClass
Animal.java
package com.cakes;
Dog.java
package com.cakes;
@Override
public String makeSound() {
return "Woof";
}
}
The Cat class is a subclass of Animal. It implements makeSound() to return "Meow".
Cat.java
package com.cakes;
@Override
public String makeSound() {
return "Meow";
}
}
Now, let's implement our factory. We will call our factory's object creation method getAnimal. This method takes a String
as a parameter. If the String is "canine", it returns a Dog object. Otherwise, it returns a Cat object.
AnimalFactory.java
package com.cakes;
}
The Demo class demonstrates the use of our factory. It creates an AnimalFactory factory. The factory creates an Animal
object and then another Animal object. The first object is a Cat and the second object is a Dog. The output of each
object's makeSound() method is displayed.
Demo.java
package com.cakes;
Animal a1 = animalFactory.getAnimal("feline");
System.out.println("a1 sound: " + a1.makeSound());
Animal a2 = animalFactory.getAnimal("canine");
System.out.println("a2 sound: " + a2.makeSound());
}
}
The console output is shown here.
Console Output
a1 sound: Meow
a2 sound: Woof