0% found this document useful (0 votes)
71 views16 pages

ASE Assignment 2 PDF

The document discusses three design patterns - Singleton, Factory, and Abstract Factory patterns. It provides code examples for each pattern in Java. The Singleton pattern ensures only one instance of a class can be created. The Factory pattern generates objects without revealing creation logic. The Abstract Factory pattern produces families of related objects without specifying their concrete classes.

Uploaded by

Neha Asim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views16 pages

ASE Assignment 2 PDF

The document discusses three design patterns - Singleton, Factory, and Abstract Factory patterns. It provides code examples for each pattern in Java. The Singleton pattern ensures only one instance of a class can be created. The Factory pattern generates objects without revealing creation logic. The Abstract Factory pattern produces families of related objects without specifying their concrete classes.

Uploaded by

Neha Asim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Neha Asim- 70109091- Assignment 2 (Section 6C)

Answer the following questions;

Q1:

1- Draw a composition of different short scenario to represent a precedence diagram

method?
2- Draw a Use case Scenario for at least three examples for system architectures for three

different systems?
3- What are the creational design patterns? Write down each pattern with java code

examples?

Creational design patterns are a set of design patterns that aim to handle object creation
mechanisms, allowing for the creation of objects in a way that suits the current situation. There
are five main creational design patterns available, which are:

1. Singleton Pattern
The Singleton Pattern makes sure that only one instance of a class can be created, and provides a
universal access point to that instance. This pattern is useful in cases where there is a
requirement to control access to a shared resource or limit the number of instances of a class.

Java Code:

public class Singleton {

private static Singleton instance;

private Singleton() {}
public static synchronized Singleton getInstance() {

if(instance == null) {

instance = new Singleton();

return instance;

2. Factory Pattern The Factory Pattern offers a way to generate objects without revealing

the creation logic to the client. This pattern is useful in cases where different objects need

to be created based on certain conditions or parameters.

public interface Shape {

void draw();

public class Rectangle implements Shape {

@Override

public void draw() {

System.out.println("Drawing a rectangle");

}
}

public class Circle implements Shape {

@Override

public void draw() {

System.out.println("Drawing a circle");

public class ShapeFactory {

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();


}

return null;

3. Abstract Factory Pattern The Abstract Factory Pattern defines an interface for producing

families of related or dependent objects without specifying their concrete classes. This

pattern is useful in cases where objects that are related or dependent on each other need to

be created.

public interface Shape {

void draw();

public class Rectangle implements Shape {

@Override

public void draw() {

System.out.println("Drawing a rectangle");

}
public class Circle implements Shape {

@Override

public void draw() {

System.out.println("Drawing a circle");

public interface Color {

void fill();

public class Red implements Color {

@Override

public void fill() {

System.out.println("Filling with red color");

}
public class Blue implements Color {

@Override

public void fill() {

System.out.println("Filling with blue color");

public abstract class AbstractFactory {

public abstract Shape getShape(String shapeType);

public abstract Color getColor(String colorType);

public 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();

return null;

@Override

public Color getColor(String colorType) {

return null;

public class ColorFactory extends AbstractFactory {

@Override

public Shape getShape(String shapeType) {

return null;
}

@Override

public Color getColor(String colorType) {

if(colorType == null) {

return null;

if(colorType.equalsIgnoreCase("RED")) {

return new Red();

} else if(colorType.equalsIgnoreCase("BLUE")) {

return new Blue();

Q2:

Write down short notes on each with of the following with code examples.

• Abstract Method

• Abstract class

• Constructors

• Concrete class

• Interface
Abstract Method: An abstract method is a method without implementation that must be

implemented in a subclass.

abstract class Shape {

public abstract void draw();

class Rectangle extends Shape {

public void draw() {

System.out.println("Drawing a rectangle");

class Circle extends Shape {

public void draw() {

System.out.println("Drawing a circle");

Abstract Class: An abstract class is a class that cannot be instantiated and may contain

abstract methods or concrete methods with implementation.

abstract class Animal {

protected String name;

public Animal(String name) {


this.name = name;

public abstract void makeSound();

public void sleep() {

System.out.println(name + " is sleeping");

class Cat extends Animal {

public Cat(String name) {

super(name);

public void makeSound() {

System.out.println(name + " says meow");

class Dog extends Animal {

public Dog(String name) {

super(name);
}

public void makeSound() {

System.out.println(name + " says woof");

Constructors: A constructor is a special method that is called when an object is

instantiated. It is used to initialize the object's state.

class Person {

private String name;

private int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

public String getName() {

return name;

public int getAge() {

return age;
}

Person person = new Person("John", 30);

System.out.println("Name: " + person.getName() + ", Age: " + person.getAge());

Concrete Class: A concrete class is a class that can be instantiated and may or may not

have abstract methods.

class Car {

private String make;

private String model;

public Car(String make, String model) {

this.make = make;

this.model = model;

public String getMake() {

return make;

public String getModel() {

return model;

}
}

Car car = new Car("Toyota", "Corolla");

System.out.println("Make: " + car.getMake() + ", Model: " + car.getModel());

Interface: An interface is a collection of abstract methods and constant fields. It defines

a contract that implementing classes must adhere to.

interface Shape {

double getArea();

class Rectangle implements Shape {

private double width;

private double height;

public Rectangle(double width, double height) {

this.width = width;

this.height = height;

public double getArea() {

return width * height;

}
class Circle implements Shape {

private double radius;

public Circle(double radius) {

this.radius = radius;

public double getArea() {

return Math.PI * radius * radius;

You might also like