Created
August 10, 2017 14:10
-
-
Save Horaddrim/58b757ddd0916203984bb1cab33e75e0 to your computer and use it in GitHub Desktop.
A simple demonstration of three of my favorites design pattens for Java | Kotlin apps :D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Decorator.RedShapeDecorator; | |
import Facade.ShapeMaker; | |
import FacadeKt.ShapeMakerKt; | |
import Models.Shape; | |
import Factory.ShapeFactory; | |
public class Main | |
{ | |
public static void main(String[] args) | |
{ | |
/* Using the factory pattern, we can create some classes! */ | |
ShapeFactory shapeFactory = new ShapeFactory(); | |
Shape shapeCircle = shapeFactory.getShape("Circle"); | |
shapeCircle.draw(); //Inside Circle::draw() method. | |
/*Using the decorator pattern, we can personalize them! */ | |
RedShapeDecorator decorator = new RedShapeDecorator(shapeCircle); | |
decorator.draw(); // Border Color: Red && Inside Circle::draw() method. | |
Shape shapeRectangle = shapeFactory.getShape("Rectangle"); | |
shapeRectangle.draw(); //Inside Rectangle::draw() method. | |
Shape shapeSquare = shapeFactory.getShape("Square"); | |
shapeSquare.draw(); // Inside Square::draw() method. | |
/* And my personal favorite is the facade, that allows us to use the classes, without really instanciate them! */ | |
ShapeMaker shapeMaker = new ShapeMaker(); | |
shapeMaker.drawCircle(); //Same result as shapeCircle::draw() method -> Inside Circle::draw() method. | |
shapeMaker.drawRectangle();// Same result as shapeRectangle::draw() method -> Inside Rectangle::draw() method. | |
shapeMaker.drawSquare();// Same result as shapeSquare::draw() method -> Inside Square::draw() method. | |
/* Here even after manipulate some Java classes, we can manipulate Kotlin classes too, what is amazing! */ | |
FactoryKt.ShapeFactory shapeFactoryKt = new FactoryKt.ShapeFactory(); | |
KotlinModels.Shape shapeCircleKt = shapeFactoryKt.getShape("Circle"); | |
KotlinModels.Shape shapeSquareKt = shapeFactoryKt.getShape("Square"); | |
KotlinModels.Shape shapeRectangleKt = shapeFactoryKt.getShape("Rectangle"); | |
DecoratorKt.RedShapeDecorator decoratorKt = new DecoratorKt.RedShapeDecorator(shapeCircleKt); | |
decoratorKt.draw(); | |
// Again my favorite one! I love this pattern! Really helps a lot on doc and modularization of any aplication, but for me | |
//especially in Android apps! :D | |
ShapeMakerKt shapeMakerKt = new ShapeMakerKt(); | |
shapeMakerKt.drawCircle();//Same result as shapeCircle::draw() method in Java -> Inside Circle::draw() method. | |
shapeMakerKt.drawRectangle();//Same result as shapeRectangle::draw() method in Java -> Inside Rectangle::draw() method. | |
shapeMakerKt.drawSquare();//Same result as shapeSquare::draw() method in Java -> Inside Square::draw() method. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to explore the entire project, visit the repo! And if you see anyhing wrong or with a bad pratice, please tell me! 🗡️
https://github.com/Horaddrim/design-patterns-kotlin-java