0% found this document useful (0 votes)
58 views

Factory Pattern: //shape - Java

The document describes two design patterns: 1. The Factory Pattern defines a common interface for creating objects and allows subclasses to determine which objects are instantiated. It uses a factory class that returns different shapes based on a string parameter. 2. The Observer Pattern defines a one-to-many dependency between objects where a subject notifies its observers when its state changes. It has a Subject class that maintains a list of observers and notifies them of state changes. Observer subclasses like BinaryObserver and OctalObserver update themselves based on the new subject state.

Uploaded by

Rupesh Shrestha
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)
58 views

Factory Pattern: //shape - Java

The document describes two design patterns: 1. The Factory Pattern defines a common interface for creating objects and allows subclasses to determine which objects are instantiated. It uses a factory class that returns different shapes based on a string parameter. 2. The Observer Pattern defines a one-to-many dependency between objects where a subject notifies its observers when its state changes. It has a Subject class that maintains a list of observers and notifies them of state changes. Observer subclasses like BinaryObserver and OctalObserver update themselves based on the new subject state.

Uploaded by

Rupesh Shrestha
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/ 6

1.

Factory Pattern

//Shape.java
public interface Shape {
void draw();
}
//Rectangle.java
public class Rectangle implements Shape{
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method");
}
}
//Circle.java
public class Circle implements Shape{
@Override
public void draw() {
System.out.println("Inside Circle::draw() method");
}
}
//Square.java
public class Square implements Shape{
@Override
public void draw() {
System.out.println("Inside Square::draw() method");
}
}
//ShapeFactory.java
public class ShapeFactory {
public Shape getShape(String shapeType){
if(shapeType==null)
{
return null;
}
else if(shapeType.equalsIgnoreCase("Rectangle"))
{
return new Rectangle();
}
else if(shapeType.equalsIgnoreCase("Square"))
{
return new Square();
}
return null;
}
}
//FactoryPattern.java
public class FactoryPattern{
public static void main(String[] args) {
ShapeFactory shapeFactory=new ShapeFactory();
Shape shape1=shapeFactory.getShape("Square");
shape1.draw();
Shape shape1=shapeFactory.getShape("Circle");
shape1.draw();
Shape shape1=shapeFactory.getShape("Rectangle");
shape1.draw();
}
}
2. Observer Pattern

//Subject.java
import java.util.ArrayList;
import java.util.List;
public class Subject{
private List<Observer> objects = new ArrayList<Observer>();
private int state;
private int getState(){
return state;
}
public void setState(int state){
this.state = state;
notifyAllObserver();
}
public void attach(Observer observer){
observer.add(observer);
}
public void notifyAllObsserver(){
for(Observer observer:observers){
observer.update();
}
}
}
//Observer.java
public abstract class Observer{
protected Subject subject;
private abstract void update();
}
//BinaryObserver.java
public class BinaryObserver extends Observer{
Subject subject;
public BinaryObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
public void update(){
System.out.println(“Binary String:”+interger.toBinaryString(subject.getState());
}
}
//OctalObserver.java
public class OctalObserver extends Observer{
Subject subject;
public OctalObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
public void update(){
System.out.println(“Octal String:”+interger.toOctalString(subject.getState());
}
}
//ObserverPatternDemo.java
public class ObserverPatternDemo{
public static void main(String args[]){
Subject subject = new Subject();
new OctalObserver(subject);
new BinaryObserver(subject);
System.out.println(“1 st state change:”);
Subject.setState(15);
System.out.println(“2 nd state change”);
Subject.setState(16);
}
}

You might also like