دسترسی نامحدود
برای کاربرانی که ثبت نام کرده اند
برای ارتباط با ما می توانید از طریق شماره موبایل زیر از طریق تماس و پیامک با ما در ارتباط باشید
در صورت عدم پاسخ گویی از طریق پیامک با پشتیبان در ارتباط باشید
برای کاربرانی که ثبت نام کرده اند
درصورت عدم همخوانی توضیحات با کتاب
از ساعت 7 صبح تا 10 شب
ویرایش:
نویسندگان: Amit K
سری:
ISBN (شابک) : 3455234523, 9155341234
ناشر: Independently Published
سال نشر: 2024
تعداد صفحات: 320
زبان: English
فرمت فایل : EPUB (درصورت درخواست کاربر به PDF، EPUB یا AZW3 تبدیل می شود)
حجم فایل: 3 Mb
در صورت تبدیل فایل کتاب Beginning Java: Beginning Java , Java Design Pattern , Spring Hibernate به فرمت های PDF، EPUB، AZW3، MOBI و یا DJVU می توانید به پشتیبان اطلاع دهید تا فایل مورد نظر را تبدیل نمایند.
توجه داشته باشید کتاب شروع جاوا: شروع جاوا، الگوی طراحی جاوا، بهار Hibernate نسخه زبان اصلی می باشد و کتاب ترجمه شده به فارسی نمی باشد. وبسایت اینترنشنال لایبرری ارائه دهنده کتاب های زبان اصلی می باشد و هیچ گونه کتاب ترجمه شده یا نوشته شده به فارسی را ارائه نمی دهد.
Java Design Pattern In This Book You Will Learn Explanation with Diagram and Working Exercises with Successful Execution of Exercises with Output Readymade Solution With Step By Step Explanation AMIT K Contents Learn Java Design Pattern Programming Step by Step Introduction Chapter 1 : Introduction Design Pattern Introduction Chapter 2 : Creational Design Pattern Factory Pattern ( Lab - 1 ) Abstract Factory Pattern ( Lab - 2 ) Singleton Pattern ( Lab - 3 ) Prototype Pattern ( Lab - 4 ) Builder Pattern ( Lab - 5 ) Chapter 3 : Structural Design Pattern Adapter Pattern ( Lab - 6 ) Bridge Pattern ( Lab - 7 ) Composite Pattern ( Lab - 8 ) Decorator Pattern ( Lab - 9 ) Facade Pattern ( Lab - 10 ) Filter/Criteria Pattern ( Lab - 11 ) Proxy Pattern ( Lab - 12 ) Chapter 4 : Behavioral Design Pattern Chain Of Responsibility Pattern ( Lab - 13 ) Command Pattern ( Lab - 14 ) Interpreter Pattern ( Lab - 15 ) Iterator Pattern ( Lab - 16 ) Mediator Pattern ( Lab - 17 ) Null Object Pattern ( Lab - 18 ) Observer Pattern ( Lab - 19 ) State Pattern ( Lab - 20 ) Strategy Pattern ( Lab - 21 ) Template Pattern ( Lab - 22 ) Visitor Pattern ( Lab - 23 ) MVC Pattern ( Lab - 24 ) Chapter 5 : JEE Design Pattern Data Access Object Pattern ( Lab - 25 ) Introduction Design patterns are programming language independent, it is for solving the common object-oriented design problems. A design pattern represents an idea, not an implementation. By using the design patterns we can make our code more flexible, reusable and maintainable. Java itself internally follows design patterns. In core java, there are mainly three types of design patterns, which are further divided into their sub-parts: Creational Design Pattern 1. Factory Pattern 2. Abstract Factory Pattern 3. Singleton Pattern 4. Prototype Pattern 5. Builder Pattern. Structural Design Pattern 1. Adapter Pattern 2. Bridge Pattern 3. Composite Pattern 4. Decorator Pattern 5. Facade Pattern 6. Flyweight Pattern 7. Proxy Pattern Behavioral Design Pattern 1. Chain Of Responsibility Pattern 2. Command Pattern 3. Interpreter Pattern 4. Iterator Pattern 5. Mediator Pattern 6. Memento Pattern 7. Observer Pattern 8. State Pattern 9. Strategy Pattern 10. Template Pattern 11. Visitor Pattern Creational design patterns are used when creating objects. Chapter 1 : Introduction A design pattern is a well-proved solution for solving the specific problem/task. For example, to create a class for which only a single instance should be created and that single object can be used by all other classes, use Singleton design pattern. Design patterns are programming language independent, it is for solving the common object-oriented design problems. A design pattern represents an idea, not an implementation. By using the design patterns we can make our code more flexible, reusable and maintainable. Java itself internally follows design patterns. In core java, there are mainly three types of design patterns, which are further divided into their sub-parts: Creational Design Pattern 1. Factory Pattern 2. Abstract Factory Pattern 3. Singleton Pattern 4. Prototype Pattern 5. Builder Pattern. Structural Design Pattern 1. Adapter Pattern 2. Bridge Pattern 3. Composite Pattern 4. Decorator Pattern 5. Facade Pattern 6. Flyweight Pattern 7. Proxy Pattern Behavioral Design Pattern 1. Chain Of Responsibility Pattern 2. Command Pattern 3. Interpreter Pattern 4. Iterator Pattern 5. Mediator Pattern 6. Memento Pattern 7. Observer Pattern 8. State Pattern 9. Strategy Pattern 10. Template Pattern 11. Visitor Pattern 12. MVC Pattern JEE Design Pattern 1. Data Access Object Pattern Creational design patterns are used when creating objects What is Gang of Four (GOF)? Elements of Reusable Object-Oriented Software. Design patterns are based on the following principles of object orientated design. Program to an interface not an implementation Favor object composition over inheritance Chapter 2 : Creational Design Pattern 1. Factory Pattern 2. Abstract Factory Pattern 3. Singleton Pattern 4. Prototype Pattern 1. Factory Pattern Factory pattern is a creational pattern as this pattern provides better ways to create an object. In Factory pattern, we create object without exposing the creation logic to the client. Lab - 1 In the following sections we will show how to use Factory Pattern to create objects. The objects created by the factory pattern would be shape objects, such as Circle, Rectangle. First we design an interface to represent Shape. The following code is for Shape.java public interface Shape { void draw(); } Then we create concrete classes implementing the interface. The following code is for Rectangle.java public class Rectangle implements Shape { @Override public void draw() { System.out.println('Inside Rectangle::draw() method.'); } } Square.java public class Square implements Shape { @Override public void draw() { System.out.println('Inside Square::draw() method.'); } } Circle.java public class Circle implements Shape { @Override public void draw() { System.out.println('Inside Circle::draw() method.'); } } The core factory pattern is a Factory class. The following code shows how to create a Factory class for Shape objects. The ShapeFactory class creates Shape object based on the String value passed in to the getShape() method. If the String value is CIRCLE, it will create a Circle object. ShapeFactory.java public class ShapeFactory { //use getShape method to get object of type shape public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase('CIRCLE')){ return new Circle(); } else if(shapeType.equalsIgnoreCase('RECTANGLE')){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase('SQUARE')){ return new Square(); } return null; } } The following code has main method and it uses the Factory class to get object of concrete class by passing an information such as type. Main.java public class Main { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory(); //get an object of Circle and call its draw method. Shape shape1 = shapeFactory.getShape('CIRCLE'); //call draw method of Circle shape1.draw(); //get an object of Rectangle and call its draw method. Shape shape2 = shapeFactory.getShape('RECTANGLE'); //call draw method of Rectangle shape2.draw(); //get an object of Square and call its draw method. Shape shape3 = shapeFactory.getShape('SQUARE'); //call draw method of circle shape3.draw(); } } The code above generates the following result Output 2. Abstract Factory Pattern Abstract Factory pattern is another creational pattern. Abstract Factory patterns, also called as Factory of factories, have a factory which creates other factories. When using Abstract Factory pattern, we first use super factory to create factory, then use the created factory to create objects. Lab - 2 The following code shows how to use abstract factory pattern. We are going to create shapes and Printers. For shapes, we would have circle, rectangle and square. For printers we will have paper Printer, Web Printer and screen Printer. For shape we will create Shape interface, as follows Shape.java interface Shape { void draw(); } Then we create concrete classes implementing the Shape interface. class Rectangle implements Shape { @Override public void draw() { System.out.println('Inside Rectangle::draw() method.'); } } class Square implements Shape { @Override public void draw() { System.out.println('Inside Square::draw() method.'); } } class Circle implements Shape { @Override public void draw() { System.out.println('Inside Circle::draw() method.'); } } After that we create an interface for Printers. Printer.java interface Printer{ void print(); } Then we create concrete classes implementing the Printer interface. class PaperPrinter implements Printer { @Override public void print() { System.out.println('paper'); } } class WebPrinter implements Printer { @Override public void print() { System.out.println('web'); } } class ScreenPrinter implements Printer { @Override public void print() { System.out.println('screen'); } } Finally we create an abstract class to get factories for Printer and Shape Objects. abstract class AbstractFactory { abstract Printer getPrinter(String type); abstract Shape getShape(String shape) ; } Finally we create Factory classes extending AbstractFactory to generate object of concrete class based on given information. class ShapeFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase('CIRCLE')){ return new Circle(); } else if(shapeType.equalsIgnoreCase('RECTANGLE')){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase('SQUARE')){ return new Square(); } return null; } @Override Printer getPrinter(String type) { return null; } } class PrinterFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ return null; } @Override Printer getPrinter(String type) { if(type == null){ return null; } if(type.equalsIgnoreCase('paper')){ return new PaperPrinter(); } else if(type.equalsIgnoreCase('web')){ return new WebPrinter(); } else if(type.equalsIgnoreCase('Screen')){ return new ScreenPrinter(); } return null; } } Create a Factory generator/producer class to get factories by passing an information such as Shape or Printer class FactoryProducer { public static AbstractFactory getFactory(String choice){ if(choice.equalsIgnoreCase('SHAPE')){ return new ShapeFactory(); } else if(choice.equalsIgnoreCase('Printer')){ return new PrinterFactory(); } return null; } } The following code shows how to use the abstract factory patterns. public class Main { public static void main(String[] args) { //get shape factory AbstractFactory shapeFactory= FactoryProducer.getFactory('SHAPE'); //get an object of Shape Circle Shape shape1 = shapeFactory.getShape('CIRCLE'); //call draw method of Shape Circle shape1.draw(); //get an object of Shape Rectangle Shape shape2 = shapeFactory.getShape('RECTANGLE'); //call draw method of Shape Rectangle shape2.draw(); //get an object of Shape Square Shape shape3 = shapeFactory.getShape('SQUARE'); //call draw method of Shape Square shape3.draw(); //get printer factory AbstractFactory printerFactory= FactoryProducer.getFactory('printer'); Printer printer1 = printerFactory.getPrinter('Paper'); printer1.print(); Printer printer2 = printerFactory.getPrinter('Web'); printer2.print(); Printer printer3 = printerFactory.getPrinter('Screen'); printer3.print(); } } The code above generates the following result. Output 3. Singleton Pattern Singleton pattern is a creational pattern. This pattern involves only a single class which is responsible to creates its own object. The class ensures that only single object get created. This class provides a way to access its only object. For example, when design a user interface, we may only have one main application windows. We can use the Singleton Pattern to ensure that there is only one instance of the MainApplicationWindow object. Lab – 3 The following code is going to create a MainWindow class. MainWindow class have its constructor as private and have a static instance of itself. MainWindow class provides a static method to get its static instance to outside world. Main, our demo class will use MainWindow class to get a MainWindow object. MainWindow.java class MainWindow { //create an object of MainWindow private static MainWindow instance = new MainWindow(); //make the constructor private so that this class cannot be //instantiated by other class private MainWindow(){} //Get the only object available public static MainWindow getInstance(){ return instance; } public void showMessage(){ System.out.println('Hello World!'); } } public class Main { public static void main(String[] args) { //Get the only object available MainWindow object = MainWindow.getInstance(); //show the message object.showMessage(); } } Output 4. Prototype Pattern Prototype pattern is one of the creational patterns. Prototype pattern helps create duplicate object with better performance. In Prototype Pattern a clone of an existing object is returned instead of creating new one. We use Prototype Design Pattern, if the cost of creating a new object is expensive and resource intensive. Lab – 4 The following code shows how to use Prototype Pattern to create objects. First it create a Shape abstract class which implements the Cloneable interface. Shape.java abstract class Shape implements Cloneable { private String id; protected String type; abstract void draw(); public String getType(){ return type; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Object clone() { Object clone = null; try { clone = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; } } Then it creates three concrete classes extending the Shape class. class Rectangle extends Shape { public Rectangle(){ type = 'Rectangle'; } @Override public void draw() { System.out.println('Inside Rectangle::draw() method.'); } } class Square extends Shape { public Square(){ type = 'Square'; } @Override public void draw() { System.out.println('Inside Square::draw() method.'); } } class Circle extends Shape { public Circle(){ type= 'Circle'; } @Override public void draw() { System.out.println('Inside Circle::draw() method.'); } } After that it creates a ShapeProtoType class to return the prototype of shapes. class ShapeProtoType{ private static HashtableshapeMap = new Hashtable (); public static Shape getShape(String shapeId) { Shape cachedShape= shapeMap.get(shapeId); return (Shape) cachedShape.clone(); } public static void loadCache() { Circle circle = new Circle(); circle.setId('1'); shapeMap.put(circle.getId(),circle); Square square = new Square(); square.setId('2'); shapeMap.put(square.getId(),square); Rectangle rectangle = new Rectangle(); rectangle.setId('3'); shapeMap.put(rectangle.getId(),rectangle); } } public class Main{ public static void main(String[] args) { ShapeProtoType.loadCache(); Shape clonedShape = (Shape) ShapeProtoType.getShape('1'); System.out.println('Shape : ' + clonedShape.getType()); Shape clonedShape2 = (Shape) ShapeProtoType. getShape('2'); System.out.println('Shape : ' + clonedShape2.getType()); Shape clonedShape3 = (Shape) ShapeProtoType. getShape('3'); System.out.println('Shape : ' + clonedShape3.getType()); } } The code above generates the following result. Output 5. Builder Pattern Builder pattern is used to create a complex object using simple objects. It creates the bigger object step by step from small and simple object. Builder pattern is another creational pattern. For example, when creating a window as our application's main window, we need to create a menu, a toolbar and then add the menu and toolbar. For each window we are going to create, we need to create an empty window, create a menu, create a toolbar, install the menu and toolbar to the window. We can use the builder pattern to hide the implementation of how to create a window. Lab – 5 MainWindow.java class Menu { } class ToolBar { } class MainWindow { Menu menu; ToolBar toolBar; public Menu getMenu() { return menu; } public void setMenu(Menu menu) { this.menu = menu; } public ToolBar getToolBar() { return toolBar; } public void setToolBar(ToolBar toolBar) { this.toolBar = toolBar; } } class WindowBuilder{ public static MainWindow createWindow(){ MainWindow window = new MainWindow(); Menu menu = new Menu(); ToolBar toolBar = new ToolBar(); window.setMenu(menu); window.setToolBar(toolBar); return window; } } public class Main{ public static void main(String[] args) { MainWindow object = WindowBuilder.createWindow(); } } Chapter 3 : Structural Design Pattern 1. Adapter Pattern 2. Bridge Pattern 3. Composite Pattern 4. Decorator Pattern 5. Facade Pattern 6. Flyweight Pattern 7. Proxy Pattern 1. Adapter Pattern We use the adapter in real life a lot. For example, we use a memory card adapter to connect a memory card and a computer since the computer only support one type of memory card and our card is not compatible with the computer. Adapter is a converter between two incompatible entities. Adapter pattern is a structural pattern. In Java design pattern, Adapter pattern works as a bridge between two incompatible interfaces. By using the adapter pattern we can unify the two incompatible interfaces. Lab – 6 First we create a Player interface to play any time of media files. MyPlayer is the adapter, it unifies the interface of playing media files. interface Player { public void play(String type, String fileName); } interface AudioPlayer { public void playAudio(String fileName); } interface VideoPlayer { public void playVideo(String fileName); } class MyAudioPlayer implements AudioPlayer { @Override public void playAudio(String fileName) { System.out.println('Playing. Name: '+ fileName); } } class MyVideoPlayer implements VideoPlayer { @Override public void playVideo(String fileName) { System.out.println('Playing. Name: '+ fileName); } } class MyPlayer implements Player { AudioPlayer audioPlayer = new MyAudioPlayer(); VideoPlayer videoPlayer = new MyVideoPlayer(); public MyPlayer(){ } @Override public void play(String audioType, String fileName) { if(audioType.equalsIgnoreCase('avi')){ videoPlayer.playVideo(fileName); }else if(audioType.equalsIgnoreCase('mp3')){ audioPlayer.playAudio(fileName); } } } public class Main{ public static void main(String[] args) { MyPlayer myPlayer = new MyPlayer(); myPlayer.play('mp3', 'h.mp3'); myPlayer.play('avi', 'me.avi'); } } The code above generates the following result. Output 2. Bridge Pattern Bridge pattern decouples an definition from its implementation. It is a structural pattern. This pattern involves an interface which acts as a bridge. The bridge makes the concrete classes independent from interface implementer classes. Both types of classes can be altered without affecting each other. Lab – 7 interface Printer { public void print(int radius, int x, int y); } class ColorPrinter implements Printer { @Override public void print(int radius, int x, int y) { System.out.println('Color: ' + radius +', x: ' +x+', '+ y +']'); } } class BlackPrinter implements Printer { @Override public void print(int radius, int x, int y) { System.out.println('Black: ' + radius +', x: ' +x+', '+ y +']'); } } abstract class Shape { protected Printer print; protected Shape(Printer p){ this.print = p; } public abstract void draw(); } class Circle extends Shape { private int x, y, radius; public Circle(int x, int y, int radius, Printer draw) { super(draw); this.x = x; this.y = y; this.radius = radius; } public void draw() { print.print(radius,x,y); } } public class Main { public static void main(String[] args) { Shape redCircle = new Circle(100,100, 10, new ColorPrinter()); Shape blackCircle = new Circle(100,100, 10, new BlackPrinter()); redCircle.draw(); blackCircle.draw(); } } The code above generates the following result. Output 3. Composite Pattern Composite pattern is structural pattern since it creates a tree structure of group of objects. Composite pattern treats a group of objects as a single object. Composite pattern uses one class to represent a tree structure. In Composite pattern we create a class contains group of its own objects. Lab – 8 The following code uses the Employee class to demonstrate the Composite pattern. Employee.java import java.util.ArrayList; import java.util.List; class Employee { private String name; private String title; private List subordinates; public Employee(String name,String title) { this.name = name; this.title = title; subordinates = new ArrayList (); } public void add(Employee e) { subordinates.add(e); } public void remove(Employee e) { subordinates.remove(e); } public List getSubordinates(){ return subordinates; } public String toString(){ return 'Employee :[ Name : '+ name +', dept : '+ title +subordinates +' ]'; } } public class Main { public static void main(String[] args) { Employee CEO = new Employee('John','CEO'); Employee headSales = new Employee('Rob','Sales'); Employee headMarketing = new Employee('Mike','Marketing'); Employee programmer1 = new Employee('Lili','Programmer'); Employee programmer2 = new Employee('Bob','Programmer'); Employee tester1 = new Employee('Jack','Tester'); Employee tester2 = new Employee('Tom','Tester'); CEO.add(headSales); CEO.add(headMarketing); headSales.add(tester1); headSales.add(tester2); headMarketing.add(programmer1); headMarketing.add(programmer2); //print all employees of the organization System.out.println(CEO); for (Employee headEmployee : CEO.getSubordinates()) { System.out.println(headEmployee); for (Employee employee : headEmployee.getSubordinates()) { System.out.println(employee); } } } } The code above generates the following result. Output 4. Decorator Pattern Decorator pattern adds new functionality an existing object without chaining its structure. It is a structural pattern as it acts as a wrapper to existing class. Decorator pattern creates a decorator class to wrap the original class and provides additional functionality. Lab – 9 interface Printer { void print(); } class PaperPrinter implements Printer { @Override public void print() { System.out.println('Paper Printer'); } } class PlasticPrinter implements Printer { @Override public void print() { System.out.println('Plastic Printer'); } } abstract class PrinterDecorator implements Printer { protected Printer decoratedPrinter; public PrinterDecorator(Printer d){ this.decoratedPrinter = d; } public void print(){ decoratedPrinter.print(); } } class Printer3D extends PrinterDecorator { public Printer3D(Printer decoratedShape) { super(decoratedShape); } @Override public void print() { System.out.println('3D.'); decoratedPrinter.print(); } } public class Main{ public static void main(String[] args) { Printer plasticPrinter = new PlasticPrinter(); Printer plastic3DPrinter = new Printer3D(new PlasticPrinter()); Printer paper3DPrinter = new Printer3D(new PaperPrinter()); plasticPrinter.print(); plastic3DPrinter.print(); paper3DPrinter.print(); } } The code above generates the following result. Output 5. Facade Pattern Facade pattern hides the complexities of a system. It provides a simple interface to the client and the client uses the interface to interact with the system. Facade pattern is a structural pattern. Lab – 10 class ShapeFacade { interface Shape { void draw(); } class Rectangle implements Shape { @Override public void draw() { System.out.println('Rectangle::draw()'); } } class Square implements Shape { @Override public void draw() { System.out.println('Square::draw()'); } } class Circle implements Shape { @Override public void draw() { System.out.println('Circle::draw()'); } } private Shape circle = new Circle(); private Shape rectangle = new Rectangle(); private Shape square = new Square(); public ShapeFacade() { } public void drawCircle() { circle.draw(); } public void drawRectangle() { rectangle.draw(); } public void drawSquare() { square.draw(); } } public class Main { public static void main(String[] args) { ShapeFacade shapeFacade = new ShapeFacade(); shapeFacade.drawCircle(); shapeFacade.drawRectangle(); shapeFacade.drawSquare(); } } The code above generates the following result. Output 6. Filter/Criteria Pattern Filter pattern filters objects using different criteria. The criteria can be chained together through logical operations. Filter pattern is a structural pattern. Lab – 11 Employee.java import java.util.List; import java.util.ArrayList; class Employee { private String name; private String gender; private String retireStatus; public Employee(String name, String gender, String r) { this.name = name; this.gender = gender; this.retireStatus = r; } public String getName() { return name; } public String getGender() { return gender; } public String getRetireStatus() { return retireStatus; } @Override public String toString() { return 'Employee [name=' + name + ', gender=' + gender