Software Design and Architecture - Chapter 01

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 18

Software Design and Architecture - Chapter 01

Difference between Forward Engineering and Reverse Engineering

Forward Engineering:
Forward Engineering is a method of creating or making an application with the help of the given
requirements. Forward engineering is also known as Renovation and Reclamation. Forward
engineering is required high proficiency skill. It takes more time to construct or develop an
application.

Reverse Engineering:
Reverse Engineering is also known as backward engineering, is the process of forward
engineering in reverse. In this, the information are collected from the given or exist application. It
takes less time than forward engineering to develop an application. In reverse engineering the
application are broken to extract knowledge or its architecture.

Difference between Forward Engineering and Reverse Engineering:


S.NOForward Engineering Reverse Engineering
In reverse engineering or backward
In forward engineering, the engineering, the information are
application are developed with the collected from the given
1. given requirements. application.

Forward Engineering is high Reverse Engineering or backward


2. proficiency skill. engineering is low proficiency skill.

3. Forward Engineering takes more While Reverse Engineering or


backward engineering takes less
time to develop an application. time to develop an application.

The nature of reverse engineering


The nature of forward engineering or backward engineering is
4. is Prescriptive. Adaptive.

In reverse engineering, production


In forward engineering, production is started by taking existing
5. is started with given requirements. product.

The example of forward


engineering are construction of The example of backward
electronic kit, construction of DC engineering are research on
6. MOTOR etc. Instruments etc.

Design Patterns

Design patterns represent the best practices used by experienced object-oriented software
developers. Design patterns are solutions to general problems that software developers faced
during software development. These solutions were obtained by trial and error by numerous
software developers over quite a substantial period of time.
This tutorial will take you through step by step approach and examples using Java while learning
Design Pattern concepts.

What is Gang of Four (GOF)?

In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published
a book titled Design Patterns - Elements of Reusable Object-Oriented Software which initiated
the concept of Design Pattern in Software development.
These authors are collectively known as Gang of Four (GOF). According to these authors design
patterns are primarily based on the following principles of object orientated design.
 Program to an interface not an implementation
 Favour object composition over inheritance

Usage of Design Pattern

Design Patterns have two main usages in software development.

Common platform for developers


Design patterns provide a standard terminology and are specific to particular scenario. For
example, a singleton design pattern signifies use of single object so all developers familiar with
single design pattern will make use of single object and they can tell each other that program is
following a singleton pattern.

Best Practices
Design patterns have been evolved over a long period of time and they provide best solutions to
certain problems faced during software development. Learning these patterns helps
inexperienced developers to learn software design in an easy and faster way.

Types of Design Patterns

As per the design pattern reference book Design Patterns - Elements of Reusable Object-
Oriented Software , there are 23 design patterns which can be classified in three categories:
Creational, Structural and Behavioural patterns. We'll also discuss another category of design
pattern: J2EE design patterns.

S.N. Pattern & Description

1 Creational Patterns
These design patterns provide a way to create objects while hiding the
creation logic, rather than instantiating objects directly using new operator.
This gives program more flexibility in deciding which objects need to be
created for a given use case.

2 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.

3 Behavioural Patterns
These design patterns are specifically concerned with communication
between objects.

4 J2EE Patterns
These design patterns are specifically concerned with the presentation tier.
These patterns are identified by Sun Java Centre.

Factory Pattern
Factory pattern is one of the most used design patterns in Java. This type of design pattern
comes under creational pattern as this pattern provides one of the best ways to create an object.
In Factory pattern, we create object without exposing the creation logic to the client and refer to
newly created object using a common interface.

Implementation

We're going to create a Shape interface and concrete classes implementing the Shape interface.


A factory class ShapeFactory is defined as a next step.
FactoryPatternDemo, our demo class will use ShapeFactory to get a Shape object. It will pass
information (CIRCLE / RECTANGLE / SQUARE) to ShapeFactory to get the type of object it
needs.

Step 1

Create an interface.
Shape.java
public interface Shape {
void draw();
}

Step 2

Create concrete classes implementing the same interface.


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.");
}
}

Step 3

Create a Factory to generate object of concrete class based on given information.


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;
}
}

Step 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 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 square


shape3.draw();
}
}

Step 5

Verify the output.


Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.

Singleton Pattern

Singleton pattern is one of the simplest design patterns in Java. This type of design pattern
comes under creational pattern as this pattern provides one of the best ways to create an object.
This pattern involves a single class which is responsible to create an object while making sure
that only single object gets created. This class provides a way to access its only object which can
be accessed directly without need to instantiate the object of the class.

Implementation

We're going to create a SingleObject class. SingleObject class have its constructor as private


and have a static instance of itself.
SingleObject class provides a static method to get its static instance to outside
world. SingletonPatternDemo, our demo class will use SingleObject class to get
a SingleObject object.
Step 1

Create a Singleton Class.


SingleObject.java
public class SingleObject {

//create an object of SingleObject


private static SingleObject instance = new SingleObject();

//make the constructor private so that this class cannot be


//instantiated
private SingleObject(){}

//Get the only object available


public static SingleObject getInstance(){
return instance;
}

public void showMessage(){


System.out.println("Hello World!");
}
}

Step 2

Get the only object from the singleton class.


SingletonPatternDemo.java
public class SingletonPatternDemo {
public static void main(String[] args) {

//illegal construct
//Compile Time Error: The constructor SingleObject() is not visible
//SingleObject object = new SingleObject();

//Get the only object available


SingleObject object = SingleObject.getInstance();

//show the message


object.showMessage();
}
}

Step 3

Verify the output.


Hello World!

Builder Pattern

Builder pattern builds a complex object using simple objects and using a step by step approach.
This type of design pattern comes under creational pattern as this pattern provides one of the
best ways to create an object.
A Builder class builds the final object step by step. This builder is independent of other objects.

Implementation

We have considered a business case of fast-food restaurant where a typical meal could be a
burger and a cold drink. Burger could be either a Veg Burger or Chicken Burger and will be
packed by a wrapper. Cold drink could be either a coke or pepsi and will be packed in a bottle.
We are going to create an Item interface representing food items such as burgers and cold drinks
and concrete classes implementing the Item interface and a Packing interface representing
packaging of food items and concrete classes implementing the Packing interface as burger
would be packed in wrapper and cold drink would be packed as bottle.
We then create a Meal class having ArrayList of Item and a MealBuilder to build different types
of Meal objects by combining Item. BuilderPatternDemo, our demo class will use MealBuilder to
build a Meal.
Step 1

Create an interface Item representing food item and packing.


Item.java
public interface Item {
public String name();
public Packing packing();
public float price();
}
Packing.java
public interface Packing {
public String pack();
}

Step 2

Create concrete classes implementing the Packing interface.


Wrapper.java
public class Wrapper implements Packing {

@Override
public String pack() {
return "Wrapper";
}
}
Bottle.java
public class Bottle implements Packing {
@Override
public String pack() {
return "Bottle";
}
}

Step 3

Create abstract classes implementing the item interface providing default functionalities.
Burger.java
public abstract class Burger implements Item {

@Override
public Packing packing() {
return new Wrapper();
}

@Override
public abstract float price();
}
ColdDrink.java
public abstract class ColdDrink implements Item {

@Override
public Packing packing() {
return new Bottle();
}

@Override
public abstract float price();
}

Step 4

Create concrete classes extending Burger and ColdDrink classes


VegBurger.java
public class VegBurger extends Burger {

@Override
public float price() {
return 25.0f;
}

@Override
public String name() {
return "Veg Burger";
}
}
ChickenBurger.java
public class ChickenBurger extends Burger {

@Override
public float price() {
return 50.5f;
}

@Override
public String name() {
return "Chicken Burger";
}
}
Coke.java
public class Coke extends ColdDrink {

@Override
public float price() {
return 30.0f;
}

@Override
public String name() {
return "Coke";
}
}
Pepsi.java
public class Pepsi extends ColdDrink {

@Override
public float price() {
return 35.0f;
}

@Override
public String name() {
return "Pepsi";
}
}

Step 5

Create a Meal class having Item objects defined above.


Meal.java
import java.util.ArrayList;
import java.util.List;

public class Meal {


private List<Item> items = new ArrayList<Item>();

public void addItem(Item item){


items.add(item);
}

public float getCost(){


float cost = 0.0f;

for (Item item : items) {


cost += item.price();
}
return cost;
}

public void showItems(){

for (Item item : items) {


System.out.print("Item : " + item.name());
System.out.print(", Packing : " + item.packing().pack());
System.out.println(", Price : " + item.price());
}
}
}

Step 6

Create a MealBuilder class, the actual builder class responsible to create Meal objects.
MealBuilder.java
public class MealBuilder {

public Meal prepareVegMeal (){


Meal meal = new Meal();
meal.addItem(new VegBurger());
meal.addItem(new Coke());
return meal;
}

public Meal prepareNonVegMeal (){


Meal meal = new Meal();
meal.addItem(new ChickenBurger());
meal.addItem(new Pepsi());
return meal;
}
}

Step 7
BuiderPatternDemo uses MealBuider to demonstrate builder pattern.
BuilderPatternDemo.java
public class BuilderPatternDemo {
public static void main(String[] args) {

MealBuilder mealBuilder = new MealBuilder();

Meal vegMeal = mealBuilder.prepareVegMeal();


System.out.println("Veg Meal");
vegMeal.showItems();
System.out.println("Total Cost: " + vegMeal.getCost());

Meal nonVegMeal = mealBuilder.prepareNonVegMeal();


System.out.println("\n\nNon-Veg Meal");
nonVegMeal.showItems();
System.out.println("Total Cost: " + nonVegMeal.getCost());
}
}

Step 8

Verify the output.


Veg Meal
Item : Veg Burger, Packing : Wrapper, Price : 25.0
Item : Coke, Packing : Bottle, Price : 30.0
Total Cost: 55.0

Non-Veg Meal
Item : Chicken Burger, Packing : Wrapper, Price : 50.5
Item : Pepsi, Packing : Bottle, Price : 35.0
Total Cost: 85.5

MVC Pattern

MVC Pattern stands for Model-View-Controller Pattern. This pattern is used to separate
application's concerns.
 Model - Model represents an object or JAVA POJO carrying data. It can also have logic to
update controller if its data changes.
 View - View represents the visualization of the data that model contains.
 Controller - Controller acts on both model and view. It controls the data flow into model
object and updates the view whenever data changes. It keeps view and model separate.

Implementation
We are going to create a Student object acting as a model.StudentView will be a view class
which can print student details on console and StudentController is the controller class
responsible to store data in Student object and update view StudentView accordingly.
MVCPatternDemo, our demo class, will use StudentController to demonstrate use of MVC
pattern.

Step 1

Create Model.
Student.java
public class Student {
private String rollNo;
private String name;

public String getRollNo() {


return rollNo;
}

public void setRollNo(String rollNo) {


this.rollNo = rollNo;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}
}

Step 2
Create View.
StudentView.java
public class StudentView {
public void printStudentDetails(String studentName, String studentRollNo){
System.out.println("Student: ");
System.out.println("Name: " + studentName);
System.out.println("Roll No: " + studentRollNo);
}
}

Step 3

Create Controller.
StudentController.java
public class StudentController {
private Student model;
private StudentView view;

public StudentController(Student model, StudentView view){


this.model = model;
this.view = view;
}

public void setStudentName(String name){


model.setName(name);
}

public String getStudentName(){


return model.getName();
}

public void setStudentRollNo(String rollNo){


model.setRollNo(rollNo);
}

public String getStudentRollNo(){


return model.getRollNo();
}

public void updateView(){


view.printStudentDetails(model.getName(), model.getRollNo());
}
}

Step 4

Use the StudentController methods to demonstrate MVC design pattern usage.


MVCPatternDemo.java
public class MVCPatternDemo {
public static void main(String[] args) {

//fetch student record based on his roll no from the database


Student model = retriveStudentFromDatabase();

//Create a view : to write student details on console


StudentView view = new StudentView();

StudentController controller = new StudentController(model, view);

controller.updateView();

//update model data


controller.setStudentName("John");

controller.updateView();
}

private static Student retriveStudentFromDatabase(){


Student student = new Student();
student.setName("Robert");
student.setRollNo("10");
return student;
}
}

Step 5

Verify the output.


Student:
Name: Robert
Roll No: 10
Student:
Name: John
Roll No: 10

Software Architect Job Description Template

We are seeking a highly skilled software architect to lead our development team in creating software
solutions that meet our clients' needs. You will be responsible for communicating with clients to
determine their requirements, creating comprehensive solution plans, and leading a team of software
engineers as they develop polished final products.

To be successful as a software architect, you should be an expert problem solver with a strong
understanding of the broad range of software technologies and platforms available. Top candidates
will also be excellent leaders and communicators.
Software Architect Responsibilities:

 Collaborating with various stakeholders to determine software requirements.


 Creating high-level product specifications and design documents.
 Providing the development team with architectural blueprints to follow.
 Guiding and assisting the development team throughout the process.
 Troubleshooting and resolving issues with coding or design.
 Ensuring that you and the team adhere to development schedules and deadlines.
 Presenting regular progress reports and setting goals.
 Testing the final product to ensure it is completely functional and meets requirements.
 Updating software solutions as required.

Software Architect Requirements:

 Master's degree in computer science.


 Extensive experience in software development and project management.
 Solid understanding of a variety of programming tools and development platforms.
 Excellent organizational and leadership abilities.
 Highly analytical mindset, with an ability to see both the big picture and the details.
 Strong communication and presentation skills.

Types of software architects

Architect System
Strategic thinking Communication Design
type interactions

enterprise across minimal,


across projects highly abstracted
architect organization high level

solutions
focused on solution very detailed multiple teams detailed
architect

application component re-use, centered on single


single project very detailed
architect maintainability application

The main characteristics of a software architect


 Broad and deep technical knowledge. This should be obvious since one cannot
become a software architect with a musical background. The architect usually has
knowledge in several technological stacks at a decent level and should have a good
understanding of a few other ones. The software architect should also be prepared to
compose a large number of technical documentation, reports, and diagrams.
 Responsibility. A software architect should understand those architect decisions are
usually the most expensive. A person in this position should take the most responsible
approach to his work and to the decisions made. If the developer’s error costs a couple
days of work of one person, then the architect’s mistake can cost person-years on
complex projects.
  Communicability. A good specialist should be able to talk with customers in the
language of business, managers of all levels, business analysts and developers in their
languages. To explain all the action correctly, a software architect has to grow a natural
charisma and ability to convince people.  Usually, architects are laconic, eloquent and
competent speakers. While software architects participate in discussions they should be
able to persuade the others.
 Management skills. This includes both organizational and leadership skills. The ability
to lead a team, which may be distributed and composed of very different specialists.
 Stress resistance. A software architect works with different people from different
areas, rapidly changing demands or even with changing business environments.
Therefore, it is necessary to be ready for stress and to look for some ways to escape
negative emotions. Work is always more pleasant when you’re happy.
 Analytic skills. One of the most important tasks is the ability to represent an abstract
problem in the form of some finite real object of the system, which can be evaluated,
designed and developed.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy