Top Design Patterns in Java | OnGraph

Views:
 
     
 

Presentation Description

Design patterns are the most crucial things in every programming language. These are all about the best practices adopted by experienced professionals in developing applications. If you are into java development, make sure you are familiar with the top design patterns in java, such as the Factory design pattern, Singleton design pattern, Adapter design pattern, and more.

Comments

Presentation Transcript

slide 1:

Famous Java Design Patterns Prerequisites :- If you are here and looking for a ready made dish to test. Please be aware of a giant. What I meant in my last sentence is OOP’s. Before we head into this topic I assume you have a basic idea of OOP’s concept and Java. Design Pattern :- Design pattern comes in existance while you start developing complext and real world solutions. Most of us do follow these patterns in general but the difference is that we don’t even know if we are doing so. Design patterns are solutions to general problems that software developers faced during software development.

slide 2:

Types of Design Pattern :- A.Creational Patterns These design patterns provide a way to create objects while hiding the creation logic rather than instantiating objects directly using new opreator. This gives program more flexibility in deciding which objects need to be created for a given use case. B. Structural Patterns These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities. C. Behavioral Patterns These design patterns are specifically concerned with communication between objects. Scope :- We will discuss only six popular design patterns here :- 1. Factory 2. Singleton 3. Adpter 4. Decorator 5. Facade 6. Observer Let’s now discuss these one by one.

slide 3:

1. ​ ​Factory Design Pattern :- This pattern comes under creational pattern as this pattern deals with best ways of creating objects. In Factory pattern we create an object with hiding the original complexities or creational logic to client and share a common interface. We’re going to create a Color interface and concrete classes implementing the Color interface. A factory class ColorFactory is defined as a next Move. FactoryPatternSample our sample class will use ColorFactory to get a Color object. It will pass information RED / YELLOW / GREEN to ColorFactory to get the type of object it needs. Move 1:- Create an interface named Color. Yes we are creating an interface not the class since it has no default property. Color.java public interface Color void paint ​Move 2:- Create concrete classes implementing the same interface. You could think it as giving a defination to color class with specific color. Red.java

slide 4:

public class Red implements Color Override public void paint System.out.println"Inside Red::paint method." ​Yellow.java public class Yellow implements Color Override public void paint System.out.println"Inside Yellow::paint method." ​Green.java public class Green implements Color Override public void paint System.out.println"Inside Green::paint method." ​Move 3:- Create a Factory to generate object of concrete class based on given information.

slide 5:

ColorFactory.java public class ColorFactory //use getColor method to get object of type Color public Color getColorString ColorType ifColorType null return null ifColorType.equalsIgnoreCase"Yellow" return new Yellow else ifColorType.equalsIgnoreCase"Red" return new Red else ifColorType.equalsIgnoreCase"Green" return new Green return null ​Move 4:- Use the Factory to get object of concrete class by passing an information such as type. FactoryPatternDemo.java public class FactoryPatternDemo public static void mainString args ColorFactory ColorFactory new ColorFactory

slide 6:

//get an object of Yellow and call its paint method. Color Color1 ColorFactory.getColor"Yellow" //call paint method of Yellow Color1.paint //get an object of Red and call its paint method. Color Color2 ColorFactory.getColor"Red" //call paint method of Red Color2.paint //get an object of Green and call its paint method. Color Color3 ColorFactory.getColor"Green" //call paint method of Yellow Color3.paint ​Move 5:- Verify the output. Inside Yellow::paint method. Inside Red::paint method. Inside Green::paint method. ​2. ​ Singleton Design Pattern :- This pattern comes under creational design pattern. It provides a more handy and good way to create an object with it’s life cylce being extended to Application.

slide 7:

This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class. An example of this is creating a connection to database with a single and only object to avoid any conflicts and maintain it throughout the application. Implementation:- We’re going to create a MySingleton class. MySingleton class have its constructor as private and have a static instance of itself. MySingleton class provides a static method to get its static instance to outside world.SingletonPatternSample our sample class will use MySinleton class to get a MySingletonObject. Move 1:- Create a Singleton Class. MySingleton.java public class MySingleton //create an object of MySingleton private static volatile MySingleton instance new MySingleton //make the constructor private so that this class cannot be //instantiated private MySingleton //Get the only object available public static MySingleton getInstance

slide 8:

return instance public void displayIt System.out.println"It is global class" Move 2:- Get the only object from the singleton class. SingletonPatternDemo.java public class SingletonPatternDemo public static void mainString args //illegal construct //Compile Time Error: The constructor MySingleton is not visible //MySingleton object new MySingleton //Get the only object available MySingleton object MySingleton.getInstance //show the message object.displayIt ​Move 3:- Verify the output.

slide 9:

It is global class 3 ​. ​ Adapter Design Pattern :- Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces. This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. As a real life example we can think of a mobile charger as an adapter because mobile battery needs 3 volts to charge but the normal socket produces either 120V US or 240V India. So the mobile charger works as an adapter between mobile charging socket and the wall socket. We will try to implement multi-adapter using adapter design pattern in this tutorial. So first of all we will have two classes – Volt to measure volts and Socket producing constant volts of 120V. Implementation:- We’re going to create a MySingleton class. MySingleton class have its constructor as private and have a static instance of itself. MySingleton class provides a static method to get its static instance to outside world.SingletonPatternSample our sample class will use MySinleton class to get a MySingletonObject.

slide 10:

Move 1:- Create a volt class bean. Volt.java public class Volt private int volts public Voltint v this.voltsv public int getVolts return volts public void setVoltsint volts this.volts volts ​Move 2:- Create supper class Socket. Socket.java public class Socket public Volt getVolt return new Volt120

slide 11:

Move 3:- Create SocketAdapter interface that would work as a bridge B/W mobile socket and wall socket. SocketAdapter.java public interface SocketAdapter public Volt get120Volt public Volt get12Volt public Volt get3Volt ​Move 4:- Create SocketAdapterImp that extends the behaviour of 120 voltsSocket and supplies 312 and 120 volts by implementing SocketAdapter interface. SocketObjectAdapterImpl.java //Using inheritance for adapter pattern public class SocketAdapterImpl extends Socket implements SocketAdapter Override public Volt get120Volt return getVolt Override public Volt get12Volt Volt v getVolt

slide 12:

return convertVoltv10 Override public Volt get3Volt Volt v getVolt return convertVoltv40 private Volt convertVoltVolt v int i new Voltv.getVolts/i ​Move 5:- Create a test class to test our adapter implementation. AdapterPatternTest.java public class AdapterPatternTest public static void mainString args testClassAdapter private static void testClassAdapter SocketAdapter sockAdapter new SocketAdapterImpl Volt v3 getVoltsockAdapter3 Volt v12 getVoltsockAdapter12 Volt v120 getVoltsockAdapter120 System.out.println"v3 volts using Class Adapter"+v3.getVolts

slide 13:

System.out.println"v12 volts using Class Adapter"+v12.getVolts System.out.println"v120 volts using Class Adapter"+v120.getVolts private static Volt getVoltSocketAdapter sockAdapter int i switch i case 3: return sockAdapter.get3Volt case 12: return sockAdapter.get12Volt case 120: return sockAdapter.get120Volt default: return sockAdapter.get120Volt ​Move 6:- Verify the output. Iv3 volts using Class Adapter3 v12 volts using Class Adapter12 v120 volts using Class Adapter120 4. ​ ​Decorator Design Pattern:- Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class. This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.

slide 14:

We are demonstrating the use of decorator pattern via following example in which we will decorate a Color with some mixer of colors without alter Color class. Implementation:- Move 1:- Create an interface. Color.java public interface Color void paint ​Move 2:- Create concrete classes implementing the same interface. Red.java public class Red implements Color Override public void paint System.out.println"Color: Red" ​Green.java public class Green implements Color Override public void paint System.out.println"Color: Green"

slide 15:

Move 3:- Create abstract decorator class implementing the Color interface. ColorDecorator.java public abstract class ColorDecorator implements Color protected Color decoratedColor public ColorDecoratorColor decoratedColor this.decoratedColor decoratedColor public void paint decoratedColor.paint ​Move 4:- Create concrete decorator class extending the ColorDecorator class. BlueColorDecorator.java public class BlueColorDecorator extends ColorDecorator public BlueColorDecoratorColor decoratedColor superdecoratedColor Override public void paint decoratedColor.paint

slide 16:

paintBluedecoratedColor private void paintBlueColor decoratedColor System.out.println"paint one more Color: Blue" ​Move 5:- Use the RedColorDecorator to decorate Color objects. DecoratorPatternDemo.java public class DecoratorPatternDemo public static void mainString args Color Green new Green Color blueGreen new RedColorDecoratornew Green Color blueRed new RedColorDecoratornew Red System.out.println"Simple Green" Green.paint System.out.println"\nGreen with Blue mixer" redGreen.paint System.out.println"\nRed with blue mixer" redBlue.paint

slide 17:

Move 6:- Verify the output. Simple Green Color: Green Green with Blue mixer Color: Green paint one more Color: Blue Red with blue mixer Color: Red paint one more Color: Blue 5. ​ Facade Design Pattern :- Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system. This type of design pattern comes under structural pattern as this pattern adds an interface to existing system to hide its complexities. This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.

slide 18:

Implementation:- Move 1:- Create an interface. Color.java public interface Color void paint ​Move 2:- Create concrete classes implementing the same interface. Red.java public class Red implements Color Override public void paint System.out.println"Red::paint" ​Yellow.java public class Yellow implements Color Override public void paint System.out.println"Yellow::paint"

slide 19:

Green.java public class Green implements Color Override public void paint System.out.println"Green::paint" ​Move 3:- Create a facade class. ColorMaker.java public class ColorMaker private Color Red private Color Yellow private Color Green public ColorMaker Red new Red Yellow new Yellow Green new Green public void paintRed Red.paint

slide 20:

public void paintYellow Yellow.paint public void paintGreen Green.paint ​Move 4:- Use the facade to paint various types of Colors. FacadePatternSample.java public class FacadePatternSample public static void mainString args ColorMaker ColorMaker new ColorMaker ColorMaker.paintRed ColorMaker.paintYellow ColorMaker.paintGreen ​Move 5:- Verify the output.

slide 21:

Red::paint Yellow::paint Green::paint 6. ​ ​Observer Design Pattern :- Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified its depenedent objects are to be notified automatically. Observer pattern falls under behavioral pattern category. Implementation:- Observer pattern uses three actor classes. Subject Observer and Client. Subject is an object having methods to attach and detach observers to a client object. We have created an abstract class Observer and a concrete class Subject that is extending class Observer. ObserverPatternSample our sample class will use Subject and concrete class object to show observer pattern in action. ​Move 1:- Create Subject class. Subject.java public class Subject private ListObserver observers new ArrayListObserver private int state public int getState

slide 22:

return state public void setStateint state this.state state notifyAllObservers public void attachObserver observer observers.addobserver public void notifyAllObservers for Observer observer : observers observer.update ​Move 2:- Create Observer class. Observer.java public abstract class Observer protected Subject subject public abstract void update

slide 23:

Move 3:- Create concrete observer classes BinaryObserver.java public class BinaryObserver extends Observer Subject subject public BinaryObserverSubject subject this.subject subject this.subject.attachthis Override public void update System.out.println "Binary String: " + Integer.toBinaryString subject.getState OctalObserver.java public class OctalObserver extends Observer Subject subject public OctalObserverSubject subject this.subject subject this.subject.attachthis Override public void update

slide 24:

System.out.println "Octal String: " + Integer.toOctalString subject.getState ​HexaObserver.java public class HexaObserver extends Observer Subject subject public HexaObserverSubject subject this.subject subject this.subject.attachthis Override public void update System.out.println "Hex String: " + Integer.toHexString subject.getState .toUpperCase ​Move 4:- Use Subject and concrete observer objects. ObserverPatternDemo.java public class ObserverPatternDemo public static void mainString args Subject subject new Subject new HexaObserversubject new OctalObserversubject

slide 25:

new BinaryObserversubject System.out.println"First state change: 15" subject.setState15 System.out.println"Second state change: 10" subject.setState10 Move 5:- Verify the output. First state change: 15 Hex String: F Octal String: 17 Binary String: 1111 Second state change: 10 Hex String: A Octal String: 12 Binary String: 1010 That’s all about ‘Famous Java Design Patterns’. If you have any question please write in comment.

authorStream Live Help