Skip to content

Flyweight Design Pattern #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# DesignPatternsJava9
This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns.
# What is Flyweight Design Pattern
Flyweight pattern tries to reuse already existing similar kind objects by storing them and creates new object when no matching object is found. Flyweight pattern is used to reduce the number of objects created and to decrease memory footprint and increase performance.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/flyweight-pattern/diagrams/Flyweight%20Pattern%20class%20diagram.jpeg "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/flyweight-pattern/diagrams/Milky_Way-100_billion_stars.jpg "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/flyweight-pattern/diagrams/Flyweight%20pattern%20sequence%20diagram.png "Diagram")

### When to use Flyweight Design Pattern
Flyweight pattern is used when we need to create a large number of similar objects (which are immutable). This reduces memory foot print and keeps app awary from java.lang.OutOfMemoryError.

### Learn Design Patterns with Java by Aseem Jain
This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain".

### Course link:
https://www.packtpub.com/application-development/learn-design-patterns-java-9-video

### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem

### Authors blog on design patterns:
https://premaseem.wordpress.com/category/computers/design-patterns/

### Software Design pattern community face book page:
https://www.facebook.com/DesignPatternGuru/

### Note:
* This code base will work on Java 9 and above versions.
* `diagrams` folders carry UML diagrams.
* `pattern` folder has code of primary example.
* `patternBonus` folder has code of secondary or bonus example.
Binary file added diagrams/Flyweight Pattern class diagram.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/Flyweight pattern sequence diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/Milky_Way-100_billion_stars.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 67 additions & 2 deletions pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,75 @@
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
System.out.println("Flyweight design pattern using ** STAR WARS ** example");

// Create Landscape object which is responsible to display stars
Landscape landscape = new Landscape();

// Factory will provide star based on param
// Note: Factory will cache objects for reuse
// and will create new object only when it does not exist in cache.
Star star = StarFactory.getStar("dull");

landscape.displayStar(star,65,87);

// No new object needs to be created, Factory provides reusable object
// from cache which landscape displays
landscape.displayStar(StarFactory.getStar("bright"),34,43);
landscape.displayStar(StarFactory.getStar("bright"),36,47);
landscape.displayStar(StarFactory.getStar("dull"),34,43);
landscape.displayStar(StarFactory.getStar("dim"),34,43);
landscape.displayStar(StarFactory.getStar("dim"),34,43);




/* OLD Scrapped code
// Created 1000 stars with brightness level bright
Star brightStar1 = new Star("bright");
Star brightStar2 = new Star("bright");
Star brightStar3 = new Star("bright");
Star brightStar4 = new Star("bright");
Star brightStar5 = new Star("bright");
Star brightStar1000 = new Star("bright");

// Created 1000 stars with brightness level dim
Star dimStar1 = new Star("dim");
Star dimStar2 = new Star("dim");
Star dimStar3 = new Star("dim");
Star dimStar4 = new Star("dim");
Star dimStar5 = new Star("dim");
Star dimStar1000 = new Star("dim");

// Created 1000 stars with brightness level dull
Star dullStar1 = new Star("dull");
Star dullStar2 = new Star("dull");
Star dullStar3 = new Star("dull");
Star dullStar4 = new Star("dull");
Star dullStar5 = new Star("dull");
Star dullStar1000 = new Star("dull");

// Create Landscape object which is responsible to display stars
Landscape landscape = new Landscape();

landscape.displayStar(brightStar1,34,45);
landscape.displayStar(brightStar2,34,45);
landscape.displayStar(brightStar3,23,65);
landscape.displayStar(brightStar1000,34,45);

landscape.displayStar(dimStar1,34,45);
landscape.displayStar(dimStar2,54,45);
landscape.displayStar(dimStar3,34,45);
landscape.displayStar(dimStar4,34,45);
landscape.displayStar(dimStar1000,34,45);

landscape.displayStar(dullStar1,87,90);
landscape.displayStar(dullStar2,34,45);
landscape.displayStar(dullStar3,23,55);
landscape.displayStar(dullStar1000,34,45);
*/
}
}
13 changes: 13 additions & 0 deletions pattern/src/com/premaseem/Landscape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class Landscape {

public void displayStar (Star star, int xCoord, int yCoord) {
System.out.println("logic to display a "+ star +" at X / Y coordinates ");
}
}
67 changes: 67 additions & 0 deletions pattern/src/com/premaseem/Star.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public interface Star {
void displayBrightness ();
}

class BrightStar implements Star {

private String brightness;

public BrightStar () {
this.brightness = "bright";
}

@Override
public void displayBrightness () {
System.out.println("Star with brightness :" + brightness);
}

@Override
public String toString () {
return "Star with brightness :" + brightness;
}
}

class DimStar implements Star {

private String brightness;

public DimStar () {
this.brightness = "dim";
}

@Override
public void displayBrightness () {
System.out.println("Star with brightness :" + brightness);
}

@Override
public String toString () {
return "Star with brightness :" + brightness;
}
}

class DullStar implements Star {

private String brightness;

public DullStar () {
this.brightness = "dull";
}

@Override
public void displayBrightness () {
System.out.println("Star with brightness :" + brightness);
}

@Override
public String toString () {
return "Star with brightness :" + brightness;
}
}
41 changes: 41 additions & 0 deletions pattern/src/com/premaseem/StarFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.premaseem;

import java.util.HashMap;
import java.util.Map;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class StarFactory {
static Map<String,Star> starCache = new HashMap<>();

public static Star getStar (String brightnessLevel) {

// First try to retrieve object from cache
Star star = starCache.get(brightnessLevel);

// if star does not exist in cache then factory will create one and store it in cache
if (star == null) {
if (brightnessLevel.equalsIgnoreCase("bright")) {
star = new BrightStar();
starCache.put("bright",star);
}
if (brightnessLevel.equalsIgnoreCase("dim")) {
star = new DimStar();
starCache.put("dim",star);
}
if (brightnessLevel.equalsIgnoreCase("dull")) {
star = new DullStar();
starCache.put("dull",star);
}

}
// return star object for reuse
return star;
}


}

13 changes: 0 additions & 13 deletions patternBonus/src/com/premaseem/Client.java

This file was deleted.