├── .gitignore ├── LICENSE.txt ├── README.md ├── exercises.iml ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── cc3002 │ │ ├── datastruct │ │ ├── ArrayListEx.java │ │ └── HashMapEx.java │ │ ├── java │ │ ├── exceptions │ │ │ ├── A.java │ │ │ ├── E.java │ │ │ ├── E1.java │ │ │ ├── E2.java │ │ │ ├── Example1.java │ │ │ └── Example2.java │ │ ├── exercises │ │ │ ├── accessibility │ │ │ │ └── Animal.java │ │ │ ├── classmethod │ │ │ │ ├── A.java │ │ │ │ └── B.java │ │ │ ├── constructors │ │ │ │ ├── A.java │ │ │ │ ├── B.java │ │ │ │ ├── C.java │ │ │ │ ├── D.java │ │ │ │ └── Main.java │ │ │ ├── methodlookup │ │ │ │ ├── A.java │ │ │ │ ├── B.java │ │ │ │ └── C.java │ │ │ ├── methodoverload │ │ │ │ ├── A.java │ │ │ │ └── B.java │ │ │ └── visibility │ │ │ │ ├── A.java │ │ │ │ ├── B.java │ │ │ │ ├── C.java │ │ │ │ ├── D.java │ │ │ │ ├── E.java │ │ │ │ ├── F.java │ │ │ │ └── G.java │ │ └── objects │ │ │ ├── A.java │ │ │ ├── B.java │ │ │ ├── Example1.java │ │ │ ├── Example2.java │ │ │ └── ObjectHolder.java │ │ ├── patterns │ │ ├── adapter │ │ │ ├── GImage │ │ │ │ ├── GColor.java │ │ │ │ ├── GImage.java │ │ │ │ └── GImageAdapter.java │ │ │ └── phonecharger │ │ │ │ ├── AndroidPhone.java │ │ │ │ ├── IPhone.java │ │ │ │ ├── Main.java │ │ │ │ └── MicroUSBToLighteningAdapter.java │ │ ├── composite │ │ │ ├── filesystem │ │ │ │ ├── Dir.java │ │ │ │ ├── File.java │ │ │ │ └── IFile.java │ │ │ └── filetree │ │ │ │ ├── Entry.java │ │ │ │ ├── FSEntry.java │ │ │ │ ├── File.java │ │ │ │ ├── Folder.java │ │ │ │ └── Main.java │ │ ├── doubledispatch │ │ │ ├── money │ │ │ │ ├── CLP.java │ │ │ │ ├── IMoney.java │ │ │ │ ├── JPY.java │ │ │ │ ├── Money.java │ │ │ │ └── USD.java │ │ │ └── socialmedia │ │ │ │ ├── Main.java │ │ │ │ ├── influencer │ │ │ │ ├── AbstractUser.java │ │ │ │ ├── Casual.java │ │ │ │ ├── Experienced.java │ │ │ │ ├── Influencer.java │ │ │ │ └── User.java │ │ │ │ └── media │ │ │ │ ├── AhoraGram.java │ │ │ │ ├── CaraBook.java │ │ │ │ ├── RIPGooglePlus.java │ │ │ │ ├── RojoDit.java │ │ │ │ ├── SocialMedia.java │ │ │ │ └── TuTube.java │ │ ├── factory │ │ │ ├── Main.java │ │ │ ├── MusicPlayer.java │ │ │ ├── codecs │ │ │ │ ├── Codec.java │ │ │ │ ├── DecodedSong.java │ │ │ │ ├── FlacDecoder.java │ │ │ │ ├── Mp3Decoder.java │ │ │ │ └── OggDecoder.java │ │ │ └── music │ │ │ │ ├── AbstractSong.java │ │ │ │ ├── FlacSong.java │ │ │ │ ├── Mp3Song.java │ │ │ │ ├── OggSong.java │ │ │ │ └── Song.java │ │ ├── observer │ │ │ ├── Client.java │ │ │ ├── Main.java │ │ │ └── Server.java │ │ ├── proxy │ │ │ ├── HTTPClient.java │ │ │ ├── HTTPProxy.java │ │ │ ├── HTTPServer.java │ │ │ ├── Main.java │ │ │ ├── UCursosClient.java │ │ │ └── UCursosServer.java │ │ ├── state │ │ │ ├── Main.java │ │ │ └── phone │ │ │ │ ├── CameraState.java │ │ │ │ ├── InHomeState.java │ │ │ │ ├── LockState.java │ │ │ │ ├── Smartphone.java │ │ │ │ └── State.java │ │ ├── template │ │ │ ├── recipe │ │ │ │ ├── after │ │ │ │ │ ├── CaffeineBeverage.java │ │ │ │ │ ├── Coffee.java │ │ │ │ │ ├── HotBeverage.java │ │ │ │ │ ├── Main.java │ │ │ │ │ └── Tea.java │ │ │ │ └── before │ │ │ │ │ ├── Coffe.java │ │ │ │ │ ├── Main.java │ │ │ │ │ └── Tea.java │ │ │ └── sorting │ │ │ │ ├── after │ │ │ │ ├── AbstractQuickSort.java │ │ │ │ ├── Main.java │ │ │ │ ├── QuickSort.java │ │ │ │ ├── QuickSortAscHigh.java │ │ │ │ ├── QuickSortAscLow.java │ │ │ │ ├── QuickSortAscMed.java │ │ │ │ └── QuickSortDescHigh.java │ │ │ │ └── before │ │ │ │ ├── QuickSortAscHigh.java │ │ │ │ ├── QuickSortAscLow.java │ │ │ │ ├── QuickSortAscMed.java │ │ │ │ └── QuickSortDescHigh.java │ │ └── visitor │ │ │ ├── ast │ │ │ ├── Main.java │ │ │ ├── bool │ │ │ │ ├── AndExp.java │ │ │ │ ├── BoolExp.java │ │ │ │ ├── Literal.java │ │ │ │ └── OrExp.java │ │ │ └── visitor │ │ │ │ ├── DenyVisitor.java │ │ │ │ ├── EvalVisitor.java │ │ │ │ ├── PrintVisitor.java │ │ │ │ └── Visitor.java │ │ │ ├── menu │ │ │ ├── FoodItem.java │ │ │ ├── FoodMenu.java │ │ │ ├── Main.java │ │ │ ├── Menu.java │ │ │ ├── MenuComponent.java │ │ │ ├── MenuItem.java │ │ │ └── operations │ │ │ │ ├── Visitor.java │ │ │ │ ├── search │ │ │ │ ├── AbstractSearch.java │ │ │ │ ├── ByPredicate.java │ │ │ │ ├── ExactSearch.java │ │ │ │ ├── ItemNumberLessEq.java │ │ │ │ └── Search.java │ │ │ │ └── sum │ │ │ │ ├── Sum.java │ │ │ │ ├── SumPrice.java │ │ │ │ ├── SumTotalPrice.java │ │ │ │ └── SumVegetarianPrice.java │ │ │ └── observer │ │ │ ├── Main.java │ │ │ ├── MaybeSeriousBroadcastingStation.java │ │ │ ├── NewsGenerator.java │ │ │ └── news │ │ │ ├── BreakingNews.java │ │ │ ├── CelebrityStuff.java │ │ │ ├── FunCatVideos.java │ │ │ ├── News.java │ │ │ ├── NewsPublisher.java │ │ │ └── Video.java │ │ └── threads │ │ └── singleton │ │ └── ThreadSafeSingleton.java └── resources │ └── .gitkeep └── test ├── java └── com │ └── cc3002 │ ├── doubledispatch │ └── TestDollar.java │ ├── exception │ └── ExceptionTest.java │ └── testannotation │ └── BasicAnnotations.java └── resources └── .gitkeep /.gitignore: -------------------------------------------------------------------------------- 1 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 2 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 3 | 4 | # User-specific stuff: 5 | .idea/ 6 | target/ 7 | 8 | # CMake 9 | cmake-build-debug/ 10 | cmake-build-release/ 11 | 12 | ## File-based project format: 13 | *.iws 14 | 15 | ## Plugin-specific files: 16 | 17 | # IntelliJ 18 | out/ 19 | 20 | # mpeltonen/sbt-idea plugin 21 | .idea_modules/ 22 | 23 | # JIRA plugin 24 | atlassian-ide-plugin.xml 25 | 26 | # Crashlytics plugin (for Android Studio and IntelliJ) 27 | com_crashlytics_export_strings.xml 28 | crashlytics.properties 29 | crashlytics-build.properties 30 | fabric.properties 31 | 32 | 33 | # Compiled class file 34 | *.class 35 | 36 | # Log file 37 | *.log 38 | 39 | # BlueJ files 40 | *.ctxt 41 | 42 | # Mobile Tools for Java (J2ME) 43 | .mtj.tmp/ 44 | 45 | # Package Files # 46 | *.jar 47 | *.war 48 | *.ear 49 | *.zip 50 | *.tar.gz 51 | *.rar 52 | 53 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 54 | hs_err_pid* 55 | .classpath 56 | .project 57 | 58 | .settings/ 59 | .vscode/launch.json 60 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Juan-Pablo Silva 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaExamples 2 | 3 | Examples that explain Java method lookup, visibility, and other Object Oriented properties. 4 | Presents implementations of various design patterns, examples and exercises. 5 | 6 | It is used in CC3002, Design and Programming Methodologies, course in DCC Universidad de Chile as a repository for solutions and exercises for the recitations. 7 | -------------------------------------------------------------------------------- /exercises.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.cc3002 8 | exercises 9 | 1.0-SNAPSHOT 10 | 11 | 12 | junit 13 | junit 14 | 4.12 15 | test 16 | 17 | 18 | Exercises In Java 19 | https://github.com/juanpablos/ExercisesInJava 20 | The projects includes various exercises for the course CC3002. 21 | 22 | 23 | 1.8 24 | 1.8 25 | 26 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/datastruct/ArrayListEx.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.datastruct; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class ArrayListEx { 7 | 8 | public static double sum(List grades) { 9 | double theSum = 0.0; 10 | for (Double grade : grades) { 11 | theSum += grade; 12 | } 13 | return theSum; 14 | } 15 | 16 | public static void main(String[] args) { 17 | List grades = new ArrayList<>(); 18 | grades.add(5.5); 19 | 20 | grades.add(3.0); 21 | grades.add(5.1); 22 | 23 | System.out.println("Mi peor nota es la segunda: " + grades.get(1)); 24 | 25 | System.out.println("Tengo " + grades.size() + " notas"); 26 | System.out.println("Mis notas suman: " + sum(grades)); 27 | System.out.println("Mi promedio es: " + sum(grades) / grades.size()); 28 | 29 | System.out.println("Me borraron el " + 3.0 + "!"); 30 | grades.remove(3.0); 31 | System.out.println("Mis nuevas notas: " + grades); 32 | 33 | System.out.println("Me saqué un 4.0 y un 7.0 y se borra el primer control"); 34 | grades.add(4.0); 35 | grades.add(7.0); 36 | grades.remove(0); 37 | 38 | System.out.println("Mis nuevas notas: " + grades); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/datastruct/HashMapEx.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.datastruct; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class HashMapEx { 7 | public static void main(String[] args) { 8 | Map calendar = new HashMap<>(); 9 | calendar.put(1, "Enero"); 10 | calendar.put(2, "Febrero"); 11 | calendar.put(3, "Marzo"); 12 | // ... 13 | calendar.put(12, "December"); 14 | calendar.put(13, "upps me equivoque"); 15 | 16 | System.out.println("Dame el mes 12: " + calendar.get(12)); 17 | System.out.println("¿Cómo se llama el primer mes? " + calendar.get(1)); 18 | 19 | calendar.replace(12, "Diciembre"); 20 | System.out.println("Ahora sí, dame el mes 12 en español: " + calendar.get(12)); 21 | 22 | calendar.remove(13); 23 | System.out.println("¿Mes 13? " + (calendar.get(13) == null ? "No" : "Sí")); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exceptions/A.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exceptions; 2 | 3 | public class A { 4 | public void method() throws Exception { 5 | throw new Exception("An exception"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exceptions/E.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exceptions; 2 | 3 | public class E extends Exception { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exceptions/E1.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exceptions; 2 | 3 | public class E1 extends E { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exceptions/E2.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exceptions; 2 | 3 | public class E2 extends E { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exceptions/Example1.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exceptions; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.IOException; 5 | 6 | public class Example1 { 7 | 8 | 9 | public void methodWithException(boolean b) throws E1, E2 { 10 | if (b) { 11 | throw new E1(); 12 | } else { 13 | throw new E2(); 14 | } 15 | } 16 | 17 | public void example(boolean b) { 18 | try { 19 | // Tried to execute a piece of code 20 | methodWithException(b); 21 | } catch (E1|E2 e) { 22 | // An error/event occurred and we can manage it 23 | } finally { 24 | // We must execute this part 25 | } 26 | } 27 | 28 | public void rethrow() { 29 | try { 30 | // code 31 | } catch (Exception e) { 32 | throw e; 33 | } 34 | } 35 | 36 | public void tryResources(String fileName) { 37 | try(FileInputStream file = new FileInputStream(fileName)) { 38 | // do things with file 39 | } catch (IOException e) { 40 | e.printStackTrace(); 41 | } 42 | // the file is automatically closed when try exits 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exceptions/Example2.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exceptions; 2 | 3 | public class Example2 { 4 | 5 | public static void m1() { 6 | System.out.println("A"); 7 | try { 8 | System.out.println("B"); 9 | throw new E(); 10 | } catch (E e) { 11 | System.out.println("C"); 12 | } finally { 13 | System.out.println("D"); 14 | } 15 | System.out.println("E"); 16 | } 17 | 18 | public static void m2(boolean b) { 19 | try { 20 | try { 21 | System.out.println("A"); 22 | if (b) { 23 | throw new E(); 24 | } 25 | } finally { 26 | System.out.println("B"); 27 | } 28 | System.out.println("C"); 29 | } catch (E e) { 30 | System.out.println("D"); 31 | } 32 | System.out.println("E"); 33 | } 34 | 35 | public static int m3() { 36 | try { 37 | return 10; 38 | } finally { 39 | return 100; 40 | } 41 | } 42 | 43 | public static int m4(boolean b) { 44 | try { 45 | if (b) { 46 | throw new E(); 47 | } 48 | return 10; 49 | } catch (E e) { 50 | return 100; 51 | } finally { 52 | return 1000; 53 | } 54 | } 55 | 56 | public static void main(String[] args) { 57 | System.out.println("M1"); 58 | m1(); 59 | System.out.println("\nM2 - true"); 60 | m2(true); 61 | System.out.println("\nM2 - false"); 62 | m2(false); 63 | System.out.println("\nM3\n" + m3()); 64 | System.out.println("\n\nM4 - true\n" + m4(true)); 65 | System.out.println("\n\nM4 - false\n" + m4(false)); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/accessibility/Animal.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.accessibility; 2 | 3 | public class Animal { 4 | private String name; 5 | 6 | public Animal(String name) { 7 | this.name = name; 8 | } 9 | 10 | private String getName() { 11 | return name; 12 | } 13 | 14 | public String getPair(Animal paired) { 15 | return this.getName() + " with " + paired.getName(); 16 | } 17 | 18 | public static void main(String[] args) { 19 | System.out.println("1. " + new Animal("Jirafa").getPair(new Animal("Antilope"))); 20 | System.out.println("2. " + new Animal("Tigre").getName()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/classmethod/A.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.classmethod; 2 | 3 | public class A { 4 | public static String method1() { 5 | return "A.method1()"; 6 | } 7 | 8 | public String method2() { 9 | return "A.method2() > " + method1(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/classmethod/B.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.classmethod; 2 | 3 | public class B extends A { 4 | public static String method1() { 5 | return "B.method1()"; 6 | } 7 | 8 | public static void main(String[] args) { 9 | System.out.println("1. " + new A().method2()); 10 | System.out.println("2. " + new B().method2()); 11 | System.out.println("3. " + A.method1()); 12 | System.out.println("4. " + B.method1()); 13 | System.out.println("5. " + method1()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/constructors/A.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.constructors; 2 | 3 | public class A { 4 | public A() { 5 | System.out.print("new A() > "); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/constructors/B.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.constructors; 2 | 3 | public class B extends A { 4 | public B() { 5 | System.out.print("new B() > "); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/constructors/C.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.constructors; 2 | 3 | public class C extends A { 4 | public C() { 5 | System.out.print("new C() > "); 6 | } 7 | 8 | public C(String name) { 9 | System.out.print("new C(" + name + ") > "); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/constructors/D.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.constructors; 2 | 3 | public class D extends C { 4 | public D() { 5 | this("dog"); 6 | System.out.print("new D() > "); 7 | } 8 | 9 | public D(String name) { 10 | super(name); 11 | System.out.print("new D(" + name + ") > "); 12 | } 13 | 14 | public D(int number) { 15 | super(); 16 | System.out.print("new D(" + number + ") > "); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/constructors/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.constructors; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | System.out.print("1. "); 6 | new A(); 7 | System.out.println("end."); 8 | System.out.print("2. "); 9 | new B(); 10 | System.out.println("end."); 11 | System.out.print("3. "); 12 | new C(); 13 | System.out.println("end."); 14 | System.out.print("4. "); 15 | new C("animal"); 16 | System.out.println("end."); 17 | System.out.print("5. "); 18 | new D(); 19 | System.out.println("end."); 20 | System.out.print("6. "); 21 | new D("jirafa"); 22 | System.out.println("end."); 23 | System.out.print("7. "); 24 | new D(3); 25 | System.out.println("end."); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/methodlookup/A.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.methodlookup; 2 | 3 | public class A { 4 | public String method1() { 5 | return "A.method1()"; 6 | } 7 | 8 | public String method2() { 9 | return "A.method2() > " + this.method1(); 10 | } 11 | 12 | public String method5() { 13 | return "A.method5() > " + this.method2(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/methodlookup/B.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.methodlookup; 2 | 3 | public class B extends A { 4 | public String method1() { 5 | return "B.method1()"; 6 | } 7 | 8 | public String method3() { 9 | return "B.method3() > " + super.method1(); 10 | } 11 | 12 | public String method4() { 13 | return "B.method4() > " + super.method2(); 14 | } 15 | 16 | public String method5() { 17 | return "B.method5() > " + super.method5(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/methodlookup/C.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.methodlookup; 2 | 3 | public class C extends B { 4 | public String method2() { 5 | return "C.method2() > " + this.method1(); 6 | } 7 | 8 | public static void main(String[] args) { 9 | /* basic method look-up */ 10 | System.out.println("1. " + new C().method1()); 11 | System.out.println("2. " + new B().method1()); 12 | System.out.println("3. " + new A().method1()); 13 | 14 | /* this */ 15 | System.out.println("4. " + new C().method2()); 16 | System.out.println("5. " + new B().method2()); 17 | System.out.println("6. " + new A().method2()); 18 | 19 | /* super */ 20 | System.out.println("7. " + new B().method3()); 21 | System.out.println("8. " + new C().method4()); 22 | System.out.println("9. " + new C().method5()); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/methodoverload/A.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.methodoverload; 2 | 3 | public class A { 4 | String m(A o1, B o2) { 5 | return "A.m(A,B)"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/methodoverload/B.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.methodoverload; 2 | 3 | public class B extends A { 4 | String m(A o1, A o2) { 5 | return "B.m(A,A)"; 6 | } 7 | 8 | public static void main(String[] argv) { 9 | System.out.println("1. " + new B().m(new A(), new A())); 10 | System.out.println("2. " + new B().m(new A(), new B())); 11 | 12 | A object1 = new B(); 13 | A object2 = new B(); 14 | 15 | System.out.println("3. " + new B().m(object1, object2)); 16 | System.out.println("4. " + new B().m((B) object1, object2)); 17 | System.out.println("5. " + new B().m((B) object1, (B) object2)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/visibility/A.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.visibility; 2 | 3 | public class A { 4 | private String method1() { 5 | return "A.method1()"; 6 | } 7 | 8 | public String method2() { 9 | return "A.method2() > " + this.method1(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/visibility/B.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.visibility; 2 | 3 | public class B extends A { 4 | public String method1() { 5 | return "B.method1()"; 6 | } 7 | 8 | public static void main(String[] args) { 9 | System.out.println(new B().method2()); 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/visibility/C.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.visibility; 2 | 3 | public class C { 4 | protected String method1() { 5 | return "C.method1()"; 6 | } 7 | 8 | public String method2() { 9 | return "C.method2() > " + this.method1(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/visibility/D.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.visibility; 2 | 3 | public class D extends C { 4 | public String method1() { 5 | return "D.method1()"; 6 | } 7 | 8 | public static void main(String[] args) { 9 | System.out.println(new D().method2()); 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/visibility/E.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.visibility; 2 | 3 | public class E { 4 | public String method1() { 5 | return "E.method1()"; 6 | } 7 | 8 | public String method2() { 9 | return "E.method2() > " + this.method1(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/visibility/F.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.visibility; 2 | 3 | public class F extends E { 4 | public String method1() { 5 | return "F.method1()"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/exercises/visibility/G.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.exercises.visibility; 2 | 3 | public class G { 4 | public static void main(String[] args) { 5 | System.out.println("1. " + new A().method2()); 6 | System.out.println("2. " + new B().method2()); 7 | System.out.println("3. " + new C().method2()); 8 | System.out.println("4. " + new D().method2()); 9 | System.out.println("5. " + new E().method2()); 10 | System.out.println("6. " + new F().method2()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/objects/A.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.objects; 2 | 3 | public class A { 4 | protected int number; 5 | 6 | public A() { 7 | this(0); 8 | } 9 | 10 | public A(int number) { 11 | this.number = number; 12 | } 13 | 14 | public int getNumber() { 15 | return number; 16 | } 17 | } -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/objects/B.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.objects; 2 | 3 | import java.util.Objects; 4 | 5 | public class B extends A { 6 | 7 | public B() { 8 | } 9 | 10 | public B(int number) { 11 | super(number); 12 | } 13 | 14 | @Override 15 | public int hashCode() { 16 | return Objects.hash(number); 17 | } 18 | 19 | @Override 20 | public boolean equals(Object obj) { 21 | if (obj instanceof B) { 22 | return ((B) obj).number == this.number; 23 | } 24 | return false; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/objects/Example1.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.objects; 2 | 3 | public class Example1 { 4 | public static void main(String[] args) { 5 | String string1 = "Hello"; 6 | String string2 = "Bye"; 7 | 8 | System.out.println("Hello == Bye: " + (string1 == string2)); 9 | System.out.println("Hello.equals(Bye): " + string1.equals(string2)); 10 | System.out.println("Objetos distintos, no hay sorpresa"); 11 | 12 | System.out.println("---------------------\n"); 13 | 14 | String hello1 = new String("Hello"); 15 | String hello2 = new String("Hello"); 16 | System.out.println("new Hello == new Hello: " + (hello1 == hello2)); 17 | System.out.println("new Hello.equals(new Hello): " + hello1.equals(hello2)); 18 | System.out.println("Objetos distintos, pero con el mismo contenido"); 19 | 20 | System.out.println("---------------------\n"); 21 | 22 | String hola1 = "Hola"; 23 | String hola2 = "Hola"; 24 | 25 | System.out.println("Hola == Hola: " + (hola1 == hola2)); 26 | System.out.println("Hola.equals(Hola): " + hola1.equals(hola2)); 27 | System.out.println("Literales string, el compilador arregla la referencia"); 28 | 29 | System.out.println("---------------------\n"); 30 | 31 | ObjectHolder container = new ObjectHolder("This is a string"); 32 | Object variable = container.getVariable(); 33 | System.out.println(container.checkSameReference(variable)); 34 | System.out.println(container.checkEqualVariable(variable)); 35 | System.out.println("No se crea un objeto nuevo"); 36 | 37 | System.out.println("---------------------"); 38 | 39 | System.out.println(container.checkSameReference(container.getVariable())); 40 | System.out.println(container.checkEqualVariable(container.getVariable())); 41 | System.out.println("Claramente todo es igual"); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/objects/Example2.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.objects; 2 | 3 | public class Example2 { 4 | public static void main(String[] args) { 5 | A a1 = new A(); 6 | A a2 = new A(10); 7 | 8 | System.out.println("new A() == new A(10): " + (a1 == a2)); 9 | System.out.println("new A().equals(new A(10)): " + a1.equals(a2)); 10 | System.out.println("Objetos distintos, no hay sorpresa"); 11 | 12 | System.out.println("---------------------\n"); 13 | 14 | a1 = new A(); 15 | a2 = new A(); 16 | 17 | System.out.println("new A() == new A(): " + (a1 == a2)); 18 | System.out.println("new A().equals(new A()): " + a1.equals(a2)); 19 | System.out.println("Objetos distintos, con el mismo contenido, pero no son iguales?"); 20 | 21 | System.out.println("---------------------\n"); 22 | 23 | // We define B that does implement equals 24 | // in this case: 25 | // An object b1, instance of class B, is equal to another b2 iff b2 is instance of B and b1.number == b2.number 26 | B b1 = new B(); 27 | B b2 = new B(); 28 | B b3 = new B(10); 29 | 30 | System.out.println("new B() == new B(): " + (b1 == b2)); // false because they are not the same object 31 | System.out.println("new B().equals(new B()): " + b1.equals(b2)); // true because they fulfill the condition 32 | System.out.println("new B().equals(new B(10)): " + b1.equals(b3)); // false because it does not fulfill b1.number == b2.number 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/java/objects/ObjectHolder.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.java.objects; 2 | 3 | public class ObjectHolder { 4 | private Object variable; 5 | 6 | public ObjectHolder(Object variable) { 7 | this.variable = variable; 8 | } 9 | 10 | public Object getVariable() { 11 | return variable; 12 | } 13 | 14 | public boolean checkSameReference(Object var) { 15 | return variable == var; 16 | } 17 | 18 | public boolean checkEqualVariable(Object var) { 19 | return variable.equals(var); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/adapter/GImage/GColor.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.adapter.GImage; 2 | 3 | public class GColor { 4 | private int red; 5 | private int blue; 6 | private int green; 7 | 8 | public GColor(int red, int blue, int green) { 9 | this.red = red; 10 | this.blue = blue; 11 | this.green = green; 12 | } 13 | 14 | public int getRed() { 15 | return red; 16 | } 17 | 18 | public void setRed(int red) { 19 | this.red = red; 20 | } 21 | 22 | public int getBlue() { 23 | return blue; 24 | } 25 | 26 | public void setBlue(int blue) { 27 | this.blue = blue; 28 | } 29 | 30 | public int getGreen() { 31 | return green; 32 | } 33 | 34 | public void setGreen(int green) { 35 | this.green = green; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/adapter/GImage/GImage.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.adapter.GImage; 2 | 3 | import java.awt.*; 4 | 5 | public interface GImage { 6 | GColor getPixel(int x, int y); 7 | 8 | void setPixel(GColor c, int x, int y); 9 | 10 | int height(); 11 | 12 | int width(); 13 | 14 | void draw(Graphics g); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/adapter/GImage/GImageAdapter.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.adapter.GImage; 2 | 3 | import java.awt.*; 4 | import java.awt.image.BufferedImage; 5 | 6 | public class GImageAdapter implements GImage { 7 | 8 | private BufferedImage image; 9 | 10 | public GImageAdapter(Image img) { 11 | image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); 12 | Graphics g = image.getGraphics(); 13 | g.drawImage(img, 0, 0, null); 14 | } 15 | 16 | public GImageAdapter(int width, int height) { 17 | image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 18 | } 19 | 20 | public int height() { 21 | return image.getHeight(null); 22 | } 23 | 24 | public int width() { 25 | return image.getWidth(null); 26 | } 27 | 28 | public void setPixel(GColor c, int x, int y) { 29 | Color javaColor = new Color(c.getRed(), c.getGreen(), c.getBlue()); 30 | image.setRGB(x, y, javaColor.getRGB()); 31 | } 32 | 33 | public GColor getPixel(int x, int y) { 34 | Color javaColor = new Color(image.getRGB(x, y)); 35 | return new GColor(javaColor.getRed(), 36 | javaColor.getBlue(), 37 | javaColor.getGreen()); 38 | } 39 | 40 | public void draw(Graphics g) { 41 | g.drawImage(image, 0, 0, null); 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/adapter/phonecharger/AndroidPhone.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.adapter.phonecharger; 2 | 3 | public class AndroidPhone { 4 | private boolean connected; 5 | 6 | public AndroidPhone() { 7 | this.connected = false; 8 | } 9 | 10 | public void recharge() { 11 | if (connected) { 12 | System.out.println("Android Phone is charging."); 13 | System.out.println("Android - Done recharging."); 14 | } else { 15 | System.out.println("You should connect a Micro USB Cable."); 16 | } 17 | } 18 | 19 | public void useMicroUSB() { 20 | System.out.println("Connected Micro USB ---"); 21 | connected = true; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/adapter/phonecharger/IPhone.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.adapter.phonecharger; 2 | 3 | public class IPhone { 4 | private boolean connected; 5 | 6 | public IPhone() { 7 | this.connected = false; 8 | } 9 | 10 | public void recharge() { 11 | if (connected) { 12 | System.out.println("IPhone is charging."); 13 | System.out.println("IPhone - Done recharging."); 14 | } else { 15 | System.out.println("You should connect a Lightening Cable."); 16 | } 17 | } 18 | 19 | public void useLightening() { 20 | System.out.println("Connected Lightening ---"); 21 | connected = true; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/adapter/phonecharger/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.adapter.phonecharger; 2 | 3 | public class Main { 4 | 5 | public static void chargeAndroid() { 6 | AndroidPhone androidPhone = new AndroidPhone(); 7 | androidPhone.useMicroUSB(); 8 | androidPhone.recharge(); 9 | 10 | } 11 | 12 | public static void chargeIPhone() { 13 | IPhone iPhone = new IPhone(); 14 | iPhone.useLightening(); 15 | iPhone.recharge(); 16 | } 17 | 18 | public static void chargeIPhoneWithMicroUSB() { 19 | IPhone iPhone = new IPhone(); 20 | MicroUSBToLighteningAdapter adapter = new MicroUSBToLighteningAdapter(iPhone); 21 | adapter.useMicroUSB(); 22 | adapter.recharge(); 23 | } 24 | 25 | public static void main(String[] args) { 26 | chargeAndroid(); 27 | System.out.println(); 28 | chargeIPhone(); 29 | System.out.println(); 30 | chargeIPhoneWithMicroUSB(); 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/adapter/phonecharger/MicroUSBToLighteningAdapter.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.adapter.phonecharger; 2 | 3 | public class MicroUSBToLighteningAdapter { 4 | private IPhone phone; 5 | 6 | public MicroUSBToLighteningAdapter(IPhone phone) { 7 | this.phone = phone; 8 | } 9 | 10 | public void recharge() { 11 | phone.recharge(); 12 | } 13 | 14 | public void useMicroUSB() { 15 | System.out.println("Connected Micro USB ---"); 16 | phone.useLightening(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/composite/filesystem/Dir.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.composite.filesystem; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Dir implements IFile { 7 | 8 | private List content = new ArrayList<>(); 9 | private String name; 10 | 11 | public Dir(String name) { 12 | this.name = name; 13 | } 14 | 15 | public void mkdir(String name) { 16 | content.add(new Dir(name)); 17 | } 18 | 19 | public void touch(String filename) { 20 | content.add(new File(filename)); 21 | } 22 | 23 | public IFile find(String name) { 24 | if (name.equals(this.name)) return this; 25 | for (IFile f : content) { 26 | IFile found = f.find(name); 27 | if (found != null) return found; 28 | } 29 | return null; 30 | } 31 | 32 | public void rmr(String name) { 33 | if (name.equals(this.name)) { 34 | delete(); 35 | } 36 | IFile deadman = find(name); 37 | if (deadman != null) deadman.delete(); 38 | } 39 | 40 | public void delete() { 41 | for (IFile f : content) { 42 | f.delete(); 43 | } 44 | content = null; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/composite/filesystem/File.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.composite.filesystem; 2 | 3 | public class File implements IFile { 4 | 5 | private String filename; 6 | 7 | public File(String filename) { 8 | this.filename = filename; 9 | } 10 | 11 | public void mkdir(String name) { 12 | } 13 | 14 | public void touch(String filename) { 15 | } 16 | 17 | public IFile find(String name) { 18 | if (name.equals(filename)) return this; 19 | return null; 20 | } 21 | 22 | public void rmr(String name) { 23 | if (name.equals(filename)) delete(); 24 | } 25 | 26 | public void delete() { 27 | filename = null; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/composite/filesystem/IFile.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.composite.filesystem; 2 | 3 | public interface IFile { 4 | void mkdir(String name); 5 | 6 | void touch(String filename); 7 | 8 | IFile find(String name); 9 | 10 | void rmr(String name); 11 | 12 | void delete(); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/composite/filetree/Entry.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.composite.filetree; 2 | 3 | import java.util.Collections; 4 | 5 | public abstract class Entry implements FSEntry { 6 | 7 | public void showTree() { 8 | showTree(0); 9 | } 10 | 11 | @Override 12 | public void showTree(int indentLevel) { 13 | String line = String.join("", Collections.nCopies(indentLevel, "| ")) + "|--"; 14 | System.out.println(line + this); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/composite/filetree/FSEntry.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.composite.filetree; 2 | 3 | public interface FSEntry { 4 | void showTree(int indentLevels); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/composite/filetree/File.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.composite.filetree; 2 | 3 | public class File extends Entry { 4 | private String name; 5 | private long size; 6 | 7 | public File(String name, long size) { 8 | this.name = name; 9 | this.size = size; 10 | } 11 | 12 | @Override 13 | public String toString() { 14 | return name + " (" + size + " bytes)"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/composite/filetree/Folder.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.composite.filetree; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Folder extends Entry { 7 | private String name; 8 | private List contents; 9 | 10 | public Folder(String name) { 11 | this.name = name; 12 | this.contents = new ArrayList<>(); 13 | } 14 | 15 | public void add(FSEntry... entries) { 16 | for (FSEntry entry : entries) { 17 | contents.add(entry); 18 | } 19 | // contents.addAll(Arrays.asList(entries)); 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return name + " (Folder)"; 25 | } 26 | 27 | @Override 28 | public void showTree(int indentLevels) { 29 | super.showTree(indentLevels); 30 | for (FSEntry entry : contents) { 31 | entry.showTree(indentLevels + 1); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/composite/filetree/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.composite.filetree; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Folder userFolder = createMockFolder(); 7 | userFolder.showTree(); 8 | } 9 | 10 | public static Folder createMockFolder() { 11 | Folder documentos = new Folder("Documents"); 12 | File doge1 = new File("doge1.png", 21842); 13 | File fogs = new File("fogs.png", 21842); 14 | File diagrama = new File("diagrama.png", 21842); 15 | File tarea = new File("tarea_2_metodologias_final_v3.0_entregable.zip", 583140); 16 | File serie = new File("Game_Of_Thrones_S07E06_leak_full_1_link_1080P_full_HD.mkv", 1874298173); 17 | File chifaja = new File("chifaja.mp3", 21888); 18 | Folder imagenes = new Folder("Images"); 19 | imagenes.add(doge1, fogs, diagrama); 20 | Folder videos = new Folder("Videos"); 21 | videos.add(serie); 22 | Folder tareas = new Folder("Homework"); 23 | tareas.add(tarea); 24 | Folder musica = new Folder("Music"); 25 | Folder musica2 = new Folder("Favorita"); 26 | musica.add(musica2); 27 | Folder musica3 = new Folder("Albumes Completos"); 28 | musica2.add(musica3); 29 | musica3.add(chifaja); 30 | documentos.add(imagenes, videos, tareas, musica); 31 | return documentos; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/money/CLP.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.money; 2 | 3 | public class CLP extends Money { 4 | public CLP(double value) { 5 | super(value); 6 | } 7 | 8 | @Override 9 | public IMoney add(IMoney money) { 10 | return money.addToCLP(this); 11 | } 12 | 13 | @Override 14 | public USD addToUSD(USD usd) { 15 | return new USD(usd.getMoney() + getMoney() * 0.00165); 16 | } 17 | 18 | @Override 19 | public CLP addToCLP(CLP clp) { 20 | return new CLP(getMoney() + clp.getMoney()); 21 | } 22 | 23 | @Override 24 | public JPY addToJPY(JPY jpy) { 25 | return new JPY(jpy.getMoney() + getMoney() * 0.17419); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/money/IMoney.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.money; 2 | 3 | public interface IMoney { 4 | double getMoney(); 5 | IMoney add(IMoney money); 6 | USD addToUSD(USD usd); 7 | CLP addToCLP(CLP clp); 8 | JPY addToJPY(JPY jpy); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/money/JPY.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.money; 2 | 3 | public class JPY extends Money { 4 | public JPY(double value) { 5 | super(value); 6 | } 7 | 8 | @Override 9 | public IMoney add(IMoney money) { 10 | return money.addToJPY(this); 11 | } 12 | 13 | @Override 14 | public USD addToUSD(USD usd) { 15 | return new USD(usd.getMoney() + getMoney() * 0.0094); 16 | } 17 | 18 | @Override 19 | public CLP addToCLP(CLP clp) { 20 | return new CLP(clp.getMoney() + getMoney() * 5.74081); 21 | } 22 | 23 | @Override 24 | public JPY addToJPY(JPY jpy) { 25 | return new JPY(getMoney() + jpy.getMoney()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/money/Money.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.money; 2 | 3 | public abstract class Money implements IMoney { 4 | private double value; 5 | 6 | public Money(double value) { 7 | this.value = value; 8 | } 9 | 10 | @Override 11 | public double getMoney() { 12 | return value; 13 | } 14 | 15 | @Override 16 | public abstract IMoney add(IMoney money); 17 | 18 | @Override 19 | public abstract USD addToUSD(USD usd); 20 | 21 | @Override 22 | public abstract CLP addToCLP(CLP clp); 23 | 24 | @Override 25 | public abstract JPY addToJPY(JPY jpy); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/money/USD.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.money; 2 | 3 | public class USD extends Money { 4 | public USD(double value) { 5 | super(value); 6 | } 7 | 8 | @Override 9 | public IMoney add(IMoney money) { 10 | return money.addToUSD(this); 11 | } 12 | 13 | @Override 14 | public USD addToUSD(USD usd) { 15 | return new USD(getMoney() + usd.getMoney()); 16 | } 17 | 18 | @Override 19 | public CLP addToCLP(CLP clp) { 20 | return new CLP(clp.getMoney() + getMoney() * 606.29005); 21 | } 22 | 23 | @Override 24 | public JPY addToJPY(JPY jpy) { 25 | return new JPY(jpy.getMoney() + getMoney() * 105.61062); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia; 2 | 3 | public class Main { 4 | // How can we execute this example? 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/influencer/AbstractUser.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.influencer; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.media.*; 4 | 5 | public abstract class AbstractUser implements User { 6 | private String username; 7 | 8 | public AbstractUser(String username) { 9 | this.username = username; 10 | } 11 | 12 | @Override 13 | public String getUsername() { 14 | return username; 15 | } 16 | 17 | @Override 18 | public void publish(SocialMedia socialMedia) { 19 | socialMedia.publishBy(this); 20 | } 21 | 22 | @Override 23 | public abstract void publishOnAhoraGram(AhoraGram ahoraGram); 24 | 25 | @Override 26 | public abstract void publishOnCaraBook(CaraBook caraBook); 27 | 28 | @Override 29 | public void publishOnRIPGooglePlus(RIPGooglePlus ripGooglePlus) { 30 | // do nothing 31 | } 32 | 33 | @Override 34 | public void publishOnRojoDit(RojoDit rojoDit) { 35 | // do nothing 36 | } 37 | 38 | @Override 39 | public abstract void publishOnTuTube(TuTube tuTube); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/influencer/Casual.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.influencer; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.media.AhoraGram; 4 | import com.cc3002.patterns.doubledispatch.socialmedia.media.CaraBook; 5 | import com.cc3002.patterns.doubledispatch.socialmedia.media.TuTube; 6 | 7 | public class Casual extends AbstractUser { 8 | public Casual(String username) { 9 | super(username); 10 | } 11 | 12 | @Override 13 | public void publishOnAhoraGram(AhoraGram ahoraGram) { 14 | ahoraGram.publishPhoto(); 15 | } 16 | 17 | @Override 18 | public void publishOnCaraBook(CaraBook caraBook) { 19 | caraBook.publishLifeEvent(); 20 | } 21 | 22 | @Override 23 | public void publishOnTuTube(TuTube tuTube) { 24 | tuTube.likeCatVideo(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/influencer/Experienced.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.influencer; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.media.AhoraGram; 4 | import com.cc3002.patterns.doubledispatch.socialmedia.media.CaraBook; 5 | import com.cc3002.patterns.doubledispatch.socialmedia.media.RojoDit; 6 | import com.cc3002.patterns.doubledispatch.socialmedia.media.TuTube; 7 | 8 | public class Experienced extends AbstractUser { 9 | public Experienced(String username) { 10 | super(username); 11 | } 12 | 13 | @Override 14 | public void publishOnAhoraGram(AhoraGram ahoraGram) { 15 | ahoraGram.publishStory(); 16 | ahoraGram.publishPhoto(); 17 | ahoraGram.publishVideo(); 18 | } 19 | 20 | @Override 21 | public void publishOnCaraBook(CaraBook caraBook) { 22 | caraBook.publishRant(); 23 | } 24 | 25 | @Override 26 | public void publishOnRojoDit(RojoDit rojoDit) { 27 | rojoDit.publishPost(); 28 | } 29 | 30 | @Override 31 | public void publishOnTuTube(TuTube tuTube) { 32 | tuTube.publishVideo(); 33 | tuTube.likeCatVideo(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/influencer/Influencer.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.influencer; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.media.AhoraGram; 4 | import com.cc3002.patterns.doubledispatch.socialmedia.media.CaraBook; 5 | import com.cc3002.patterns.doubledispatch.socialmedia.media.TuTube; 6 | 7 | public class Influencer extends AbstractUser { 8 | public Influencer(String username) { 9 | super(username); 10 | } 11 | 12 | @Override 13 | public void publishOnAhoraGram(AhoraGram ahoraGram) { 14 | ahoraGram.publishPhoto(); 15 | ahoraGram.publishStory(); 16 | } 17 | 18 | @Override 19 | public void publishOnCaraBook(CaraBook caraBook) { 20 | caraBook.publishLifeEvent(); 21 | caraBook.marketing(); 22 | } 23 | 24 | @Override 25 | public void publishOnTuTube(TuTube tuTube) { 26 | tuTube.publishDailyVlog(); 27 | tuTube.publishVideo(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/influencer/User.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.influencer; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.media.*; 4 | 5 | public interface User { 6 | void publish(SocialMedia socialMedia); 7 | void publishOnAhoraGram(AhoraGram ahoraGram); 8 | void publishOnCaraBook(CaraBook caraBook); 9 | void publishOnRIPGooglePlus(RIPGooglePlus ripGooglePlus); 10 | void publishOnRojoDit(RojoDit rojoDit); 11 | void publishOnTuTube(TuTube tuTube); 12 | 13 | String getUsername(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/media/AhoraGram.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.media; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.influencer.User; 4 | 5 | public class AhoraGram implements SocialMedia { 6 | @Override 7 | public void publishBy(User user) { 8 | user.publishOnAhoraGram(this); 9 | } 10 | 11 | public void publishStory() { 12 | // blabla 13 | } 14 | 15 | public void publishVideo() { 16 | // blabla 17 | } 18 | 19 | public void publishPhoto() { 20 | // blabla 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/media/CaraBook.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.media; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.influencer.User; 4 | 5 | public class CaraBook implements SocialMedia { 6 | @Override 7 | public void publishBy(User user) { 8 | user.publishOnCaraBook(this); 9 | } 10 | 11 | public void publishRant() { 12 | // blabla 13 | } 14 | 15 | public void marketing() { 16 | // blabla 17 | } 18 | 19 | public void publishLifeEvent() { 20 | // blabla 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/media/RIPGooglePlus.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.media; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.influencer.User; 4 | 5 | public class RIPGooglePlus implements SocialMedia { 6 | @Override 7 | public void publishBy(User user) { 8 | user.publishOnRIPGooglePlus(this); 9 | } 10 | 11 | public void rip() { 12 | // rip 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/media/RojoDit.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.media; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.influencer.User; 4 | 5 | public class RojoDit implements SocialMedia { 6 | @Override 7 | public void publishBy(User user) { 8 | user.publishOnRojoDit(this); 9 | } 10 | 11 | public void publishPost() { 12 | // blabla 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/media/SocialMedia.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.media; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.influencer.User; 4 | 5 | public interface SocialMedia { 6 | void publishBy(User user); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/doubledispatch/socialmedia/media/TuTube.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.doubledispatch.socialmedia.media; 2 | 3 | import com.cc3002.patterns.doubledispatch.socialmedia.influencer.User; 4 | 5 | public class TuTube implements SocialMedia { 6 | @Override 7 | public void publishBy(User user) { 8 | user.publishOnTuTube(this); 9 | } 10 | 11 | public void publishDailyVlog() { 12 | // blabla 13 | } 14 | 15 | public void publishVideo() { 16 | // blabla 17 | } 18 | 19 | public void likeCatVideo() { 20 | // blabla 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory; 2 | 3 | import com.cc3002.patterns.factory.music.FlacSong; 4 | import com.cc3002.patterns.factory.music.Mp3Song; 5 | import com.cc3002.patterns.factory.music.OggSong; 6 | 7 | public class Main { 8 | public static void main(String[] args) { 9 | MusicPlayer player = new MusicPlayer(); 10 | player.addSongs( 11 | new Mp3Song("Feather - Nujabes"), 12 | new OggSong("feelings - Sky.High"), 13 | new FlacSong("Dreamin - Xori")); 14 | 15 | while (!player.emptyPlayList()) { 16 | player.playSong(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/MusicPlayer.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory; 2 | 3 | import com.cc3002.patterns.factory.codecs.Codec; 4 | import com.cc3002.patterns.factory.codecs.FlacDecoder; 5 | import com.cc3002.patterns.factory.codecs.Mp3Decoder; 6 | import com.cc3002.patterns.factory.codecs.OggDecoder; 7 | import com.cc3002.patterns.factory.music.Song; 8 | 9 | import java.util.Arrays; 10 | import java.util.LinkedList; 11 | 12 | public class MusicPlayer { 13 | private LinkedList songs; 14 | 15 | public MusicPlayer() { 16 | songs = new LinkedList<>(); 17 | } 18 | 19 | public void addSongs(Song... songs){ 20 | this.songs.addAll(Arrays.asList(songs)); 21 | } 22 | 23 | public void playSong() { 24 | Codec decoder; 25 | Song song = songs.pop(); 26 | switch (song.getFormat()) { 27 | case "mp3": 28 | decoder = new Mp3Decoder(song); 29 | break; 30 | case "ogg": 31 | decoder = new OggDecoder(song); 32 | break; 33 | case "flac": 34 | decoder = new FlacDecoder(song); 35 | break; 36 | default: 37 | throw new RuntimeException("Format is not supported"); 38 | } 39 | 40 | System.out.println("Now playing: " + decoder.getDecodedSong() + " with " + decoder); 41 | 42 | } 43 | 44 | public boolean emptyPlayList() { 45 | return songs.isEmpty(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/codecs/Codec.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory.codecs; 2 | 3 | public interface Codec { 4 | DecodedSong getDecodedSong(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/codecs/DecodedSong.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory.codecs; 2 | 3 | public class DecodedSong { 4 | private String name; 5 | 6 | public DecodedSong(String name) { 7 | this.name = name; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | return name; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/codecs/FlacDecoder.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory.codecs; 2 | 3 | import com.cc3002.patterns.factory.music.Song; 4 | 5 | public class FlacDecoder implements Codec { 6 | private Song song; 7 | 8 | public FlacDecoder(Song song) { 9 | this.song = song; 10 | } 11 | 12 | @Override 13 | public DecodedSong getDecodedSong() { 14 | return new DecodedSong(song.getName()); 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "FlacDecoder{" + 20 | "song=" + song + 21 | '}'; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/codecs/Mp3Decoder.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory.codecs; 2 | 3 | import com.cc3002.patterns.factory.music.Song; 4 | 5 | public class Mp3Decoder implements Codec { 6 | private Song song; 7 | 8 | public Mp3Decoder(Song song) { 9 | this.song = song; 10 | } 11 | 12 | @Override 13 | public DecodedSong getDecodedSong() { 14 | return new DecodedSong(song.getName()); 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "Mp3Decoder{" + 20 | "song=" + song + 21 | '}'; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/codecs/OggDecoder.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory.codecs; 2 | 3 | import com.cc3002.patterns.factory.music.Song; 4 | 5 | public class OggDecoder implements Codec { 6 | private Song song; 7 | 8 | public OggDecoder(Song song) { 9 | this.song = song; 10 | } 11 | 12 | @Override 13 | public DecodedSong getDecodedSong() { 14 | return new DecodedSong(song.getName()); 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "OggDecoder{" + 20 | "song=" + song + 21 | '}'; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/music/AbstractSong.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory.music; 2 | 3 | public abstract class AbstractSong implements Song { 4 | private String name; 5 | 6 | public AbstractSong(String name) { 7 | this.name = name; 8 | } 9 | 10 | @Override 11 | public String getName() { 12 | return name; 13 | } 14 | 15 | @Override 16 | public abstract String getFormat(); 17 | 18 | @Override 19 | public String toString() { 20 | return name; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/music/FlacSong.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory.music; 2 | 3 | public class FlacSong extends AbstractSong { 4 | public FlacSong(String name) { 5 | super(name); 6 | } 7 | 8 | @Override 9 | public String getFormat() { 10 | return "flac"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/music/Mp3Song.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory.music; 2 | 3 | public class Mp3Song extends AbstractSong { 4 | public Mp3Song(String name) { 5 | super(name); 6 | } 7 | 8 | @Override 9 | public String getFormat() { 10 | return "mp3"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/music/OggSong.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory.music; 2 | 3 | public class OggSong extends AbstractSong { 4 | public OggSong(String name) { 5 | super(name); 6 | } 7 | 8 | @Override 9 | public String getFormat() { 10 | return "ogg"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/factory/music/Song.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.factory.music; 2 | 3 | public interface Song { 4 | String getName(); 5 | String getFormat(); 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/observer/Client.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.observer; 2 | 3 | import java.util.Observable; 4 | import java.util.Observer; 5 | 6 | public class Client implements Observer { 7 | private int id; 8 | 9 | public Client(int i) { 10 | id = i; 11 | } 12 | 13 | @Override 14 | public void update(Observable o, Object arg) { 15 | if (arg instanceof String) { 16 | String message = (String) arg; 17 | System.out.println("[Client " + id + "]: " + message); 18 | } 19 | } 20 | 21 | public int getId() { 22 | return id; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/observer/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.observer; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Client client1 = new Client(1); 7 | Client client2 = new Client(2); 8 | Client client3 = new Client(3); 9 | Client client4 = new Client(4); 10 | 11 | Server server = new Server(); 12 | 13 | server.connect(client1, client2, client3, client4); 14 | Thread serverThread = new Thread(server); 15 | serverThread.start(); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/observer/Server.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.observer; 2 | 3 | import java.util.Observable; 4 | 5 | public class Server extends Observable implements Runnable { 6 | @Override 7 | public void run() { 8 | try { 9 | publishMessage("Bienvenidos al servidor!"); 10 | Thread.sleep(1000); 11 | publishMessage("Les voy a mandar spam cada 2 segundos."); 12 | Thread.sleep(2000); 13 | publishMessage("*****SPAM*****"); 14 | Thread.sleep(2000); 15 | publishMessage("El Servicio de Impuestos Internos, se ha percatado que en diversos " + 16 | "despachos alrededor del pais, Ud. ha propuesto esquemas para evadir el pago de impuestos"); 17 | Thread.sleep(2000); 18 | publishMessage("y hemos detectado anomalias en su situacion fiscal. Para evitar una " + 19 | "sancion en su contra que puede ser una multa de hasta 75 UTM."); 20 | Thread.sleep(2000); 21 | publishMessage("le recomendamos seguir nuestro manual de recomendaciones en [LINK].exe"); 22 | Thread.sleep(5000); 23 | publishMessage("... no somos spam... es .exe porque es interactivo..."); 24 | } catch (InterruptedException e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | 29 | public void connect(Client... clients) { 30 | for (Client client : clients) { 31 | addObserver(client); 32 | System.out.println("[Server] Client " + client.getId() + " connected to server."); 33 | } 34 | } 35 | 36 | public void publishMessage(String message) { 37 | setChanged(); 38 | System.out.println("[Server] " + "Message to publish: " + message); 39 | notifyObservers(message); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/proxy/HTTPClient.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.proxy; 2 | 3 | public interface HTTPClient { 4 | void receiveContents(String contentType, String message); 5 | 6 | String getUserName(); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/proxy/HTTPProxy.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.proxy; 2 | 3 | public class HTTPProxy implements HTTPServer, HTTPClient { 4 | 5 | private HTTPServer server; 6 | private HTTPClient user; 7 | 8 | public HTTPProxy(HTTPServer server) { 9 | this.server = server; 10 | } 11 | 12 | @Override 13 | public void receiveContents(String contentType, String message) { 14 | System.out.println("[Proxy Intercepting " + user.getUserName() + "]: " + message); 15 | user.receiveContents(contentType, message); 16 | } 17 | 18 | @Override 19 | public String getUserName() { 20 | return user.getUserName(); 21 | } 22 | 23 | @Override 24 | public void sendContents(String contentType, HTTPClient client) { 25 | user = client; 26 | System.out.println("[Proxy Intercepting " + user.getUserName() + " Request]: " + contentType); 27 | server.sendContents(contentType, this); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/proxy/HTTPServer.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.proxy; 2 | 3 | public interface HTTPServer { 4 | void sendContents(String contentType, HTTPClient client); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/proxy/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.proxy; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | HTTPServer ucursos = new UCursosServer(); 6 | 7 | HTTPClient juanito = new UCursosClient("Juanito"); 8 | HTTPClient maria = new UCursosClient("Maria"); 9 | 10 | ucursos.sendContents("Mark", juanito); 11 | ucursos.sendContents("Image", maria); 12 | ucursos.sendContents("Info", maria); 13 | 14 | System.out.println("----------------"); 15 | 16 | ucursos = new HTTPProxy(ucursos); 17 | ucursos.sendContents("Mark", juanito); 18 | ucursos.sendContents("Image", maria); 19 | ucursos.sendContents("Info", maria); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/proxy/UCursosClient.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.proxy; 2 | 3 | public class UCursosClient implements HTTPClient { 4 | private String name; 5 | 6 | public UCursosClient(String name) { 7 | this.name = name; 8 | } 9 | 10 | public String getUserName() { 11 | return name; 12 | } 13 | 14 | @Override 15 | public void receiveContents(String contentType, String message) { 16 | switch (contentType) { 17 | case "Mark": 18 | System.out.println("[Client " + name + "]: I got a: " + message); 19 | break; 20 | case "Image": 21 | System.out.println("[Client " + name + "]: Image path is: " + message); 22 | break; 23 | default: 24 | System.out.println("[Client " + name + " - Info]: " + message); 25 | break; 26 | } 27 | } 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/proxy/UCursosServer.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.proxy; 2 | 3 | import java.util.Random; 4 | 5 | public class UCursosServer implements HTTPServer { 6 | private String todaysInfo = "No deje que le roben su dinero del banco del pais!"; 7 | private String images = "/cats.jpg"; 8 | 9 | public void setTodaysInfo(String todaysInfo) { 10 | this.todaysInfo = todaysInfo; 11 | } 12 | 13 | public void setImages(String images) { 14 | this.images = images; 15 | } 16 | 17 | @Override 18 | public void sendContents(String contentType, HTTPClient client) { 19 | String message; 20 | switch (contentType) { 21 | case "Mark": 22 | message = Integer.toString(new Random().nextInt(7) + 1); 23 | System.out.println("[UCursos - Marks]: " + client.getUserName() + ": " + message); 24 | break; 25 | case "Image": 26 | message = images; 27 | System.out.println("[UCursos - Image]: " + client.getUserName() + ": " + message); 28 | break; 29 | default: 30 | message = todaysInfo; 31 | System.out.println("[UCursos - Info]: " + client.getUserName() + ": " + message); 32 | break; 33 | } 34 | 35 | client.receiveContents(contentType, message); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/state/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.state; 2 | 3 | import com.cc3002.patterns.state.phone.Smartphone; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Smartphone smartphone = new Smartphone("123"); 8 | smartphone.homeButton(); 9 | smartphone.lockPhone(); 10 | smartphone.unlockPhone("1234"); 11 | 12 | smartphone.unlockPhone("123"); 13 | System.out.println(smartphone.getVolume()); 14 | smartphone.volumeUp(); 15 | smartphone.volumeUp(); 16 | System.out.println(smartphone.getVolume()); 17 | 18 | smartphone.openCamera(); 19 | smartphone.volumeUp(); 20 | 21 | smartphone.lockPhone(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/state/phone/CameraState.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.state.phone; 2 | 3 | public class CameraState extends State { 4 | public void pressLock() { 5 | phone.setState(new LockState()); 6 | } 7 | 8 | public void pressHome() { 9 | phone.setState(new InHomeState()); 10 | } 11 | 12 | public void pressVolumeUp() { 13 | phone.takePhoto(); 14 | } 15 | 16 | public void pressVolumeDown() { 17 | phone.takePhoto(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/state/phone/InHomeState.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.state.phone; 2 | 3 | public class InHomeState extends State { 4 | @Override 5 | public void pressCamera() { 6 | phone.setState(new CameraState()); 7 | } 8 | 9 | @Override 10 | public void pressLock() { 11 | phone.setState(new LockState()); 12 | } 13 | 14 | @Override 15 | public void pressHome() { 16 | System.out.println("ALREADY IN HOME"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/state/phone/LockState.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.state.phone; 2 | 3 | public class LockState extends State { 4 | 5 | @Override 6 | public void unlock(String password) { 7 | if (phone.getPassword().equals(password)) { 8 | phone.setState(new InHomeState()); 9 | } else { 10 | System.out.println("INCORRECT PASSWORD"); 11 | } 12 | } 13 | 14 | @Override 15 | public void pressHome() { 16 | System.out.println("PHONE IS LOCKED - Insert password"); 17 | } 18 | 19 | @Override 20 | public void pressLock() { 21 | System.out.println("PHONE IS LOCKED - Insert password"); 22 | } 23 | 24 | @Override 25 | public void pressCamera() { 26 | phone.setState(new CameraState()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/state/phone/Smartphone.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.state.phone; 2 | 3 | public class Smartphone { 4 | private State state; 5 | private String password; 6 | private int volume = 10; 7 | 8 | public Smartphone(String password) { 9 | this.password = password; 10 | setState(new LockState()); 11 | } 12 | 13 | void setState(State state) { 14 | this.state = state; 15 | state.setPhone(this); 16 | } 17 | 18 | public void volumeUp() { 19 | state.pressVolumeUp(); 20 | } 21 | 22 | public void volumeDown() { 23 | state.pressVolumeDown(); 24 | } 25 | 26 | public void homeButton() { 27 | state.pressHome(); 28 | } 29 | 30 | public void unlockPhone(String password) { 31 | state.unlock(password); 32 | } 33 | 34 | public void lockPhone() { 35 | state.pressLock(); 36 | } 37 | 38 | public void openCamera() { 39 | state.pressCamera(); 40 | } 41 | 42 | public int getVolume() { 43 | return volume; 44 | } 45 | 46 | String getPassword() { 47 | return password; 48 | } 49 | 50 | void changeVolume(int volume) { 51 | this.volume = volume; 52 | } 53 | 54 | void takePhoto() { 55 | System.out.println("Photo taken"); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/state/phone/State.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.state.phone; 2 | 3 | public class State { 4 | Smartphone phone; 5 | 6 | public void setPhone(Smartphone phone) { 7 | this.phone = phone; 8 | } 9 | 10 | public void unlock(String password) { 11 | 12 | } 13 | 14 | public void pressLock() { 15 | 16 | } 17 | 18 | public void pressHome() { 19 | 20 | } 21 | 22 | public void pressVolumeUp() { 23 | phone.changeVolume(phone.getVolume() + 1); 24 | } 25 | 26 | public void pressVolumeDown() { 27 | phone.changeVolume(phone.getVolume() - 1); 28 | } 29 | 30 | public void pressCamera() { 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/recipe/after/CaffeineBeverage.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.recipe.after; 2 | 3 | 4 | public abstract class CaffeineBeverage implements HotBeverage { 5 | /** 6 | * template method 7 | */ 8 | @Override 9 | public void prepareRecipe() { 10 | boilWater(); 11 | brew(); 12 | pourInCup(); 13 | addCondiments(); 14 | } 15 | 16 | public abstract void brew(); 17 | 18 | public abstract void addCondiments(); 19 | 20 | public void boilWater() { 21 | System.out.println("Boiling water"); 22 | } 23 | 24 | public void pourInCup() { 25 | System.out.println("Pouring into cup"); 26 | } 27 | } -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/recipe/after/Coffee.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.recipe.after; 2 | 3 | 4 | public class Coffee extends CaffeineBeverage { 5 | 6 | @Override 7 | public void brew() { 8 | System.out.println("Dripping Coffee through filter"); 9 | } 10 | 11 | @Override 12 | public void addCondiments() { 13 | System.out.println("Adding Sugar and Milk"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/recipe/after/HotBeverage.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.recipe.after; 2 | 3 | public interface HotBeverage { 4 | void prepareRecipe(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/recipe/after/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.recipe.after; 2 | 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | HotBeverage coffee = new Coffee(); 7 | HotBeverage tea = new Tea(); 8 | 9 | coffee.prepareRecipe(); 10 | System.out.println("------------"); 11 | tea.prepareRecipe(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/recipe/after/Tea.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.recipe.after; 2 | 3 | 4 | public class Tea extends CaffeineBeverage { 5 | 6 | @Override 7 | public void brew() { 8 | System.out.println("Steeping the tea"); 9 | } 10 | 11 | @Override 12 | public void addCondiments() { 13 | System.out.println("Adding Lemon"); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/recipe/before/Coffe.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.recipe.before; 2 | 3 | 4 | public class Coffe { 5 | 6 | public void prepareCoffe() { 7 | boilWater(); 8 | brewCoffe(); 9 | pourInCup(); 10 | addSugarAndMilk(); 11 | } 12 | 13 | public void boilWater() { 14 | System.out.println("Boiling water"); 15 | } 16 | 17 | public void pourInCup() { 18 | System.out.println("Pouring into cup"); 19 | } 20 | 21 | public void brewCoffe() { 22 | System.out.println("Dripping Coffee through filter"); 23 | 24 | } 25 | 26 | public void addSugarAndMilk() { 27 | System.out.println("Adding Sugar and Milk"); 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/recipe/before/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.recipe.before; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Coffe coffe = new Coffe(); 6 | Tea tea = new Tea(); 7 | 8 | coffe.prepareCoffe(); 9 | System.out.println("------------"); 10 | tea.prepareTea(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/recipe/before/Tea.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.recipe.before; 2 | 3 | 4 | public class Tea { 5 | 6 | public void prepareTea() { 7 | boilWater(); 8 | brewTea(); 9 | pourInCup(); 10 | addLemon(); 11 | } 12 | 13 | public void boilWater() { 14 | System.out.println("Boiling water"); 15 | } 16 | 17 | public void pourInCup() { 18 | System.out.println("Pouring into cup"); 19 | } 20 | 21 | public void brewTea() { 22 | System.out.println("Steeping the tea"); 23 | } 24 | 25 | public void addLemon() { 26 | System.out.println("Adding Lemon"); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/after/AbstractQuickSort.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.after; 2 | 3 | public abstract class AbstractQuickSort implements QuickSort { 4 | 5 | public abstract int getPivot(int arr[], int low, int high); 6 | 7 | public abstract boolean shouldSwapForward(int a, int b); 8 | 9 | public abstract boolean shouldSwapBackward(int a, int b); 10 | 11 | protected void swap(int arr[], int i, int j) { 12 | int temp = arr[i]; 13 | arr[i] = arr[j]; 14 | arr[j] = temp; 15 | } 16 | 17 | @Override 18 | public void sort(int arr[], int low, int high) { 19 | int pivot = getPivot(arr, low, high); 20 | int i = low; 21 | int j = high; 22 | while (i <= j) { 23 | while (shouldSwapForward(arr[i], pivot)) 24 | i++; 25 | while (shouldSwapBackward(arr[j], pivot)) 26 | j--; 27 | if (i <= j) { 28 | swap(arr, i, j); 29 | i++; 30 | j--; 31 | } 32 | } 33 | if (low < j) 34 | sort(arr, low, j); 35 | if (i < high) 36 | sort(arr, i, high); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/after/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.after; 2 | 3 | public class Main { 4 | 5 | public static void printArray(int arr[]) { 6 | for (int anArr : arr) { 7 | System.out.print(anArr + " "); 8 | } 9 | System.out.println(); 10 | } 11 | 12 | public static void main(String[] args) { 13 | int arr[] = {48, 1, 6, 4, 6, 41, 8, 4, 6, 2, 1, 56, 6, 4}; 14 | int n = arr.length; 15 | 16 | System.out.println("Quick Asc High"); 17 | QuickSort qs = new QuickSortAscHigh(); 18 | int arr2[] = arr.clone(); 19 | qs.sort(arr2, 0, n - 1); 20 | printArray(arr2); 21 | 22 | System.out.println("Quick Asc Low"); 23 | qs = new QuickSortAscLow(); 24 | arr2 = arr.clone(); 25 | qs.sort(arr2, 0, n - 1); 26 | printArray(arr2); 27 | 28 | System.out.println("Quick Asc Med"); 29 | qs = new QuickSortAscMed(); 30 | arr2 = arr.clone(); 31 | qs.sort(arr2, 0, n - 1); 32 | printArray(arr2); 33 | 34 | System.out.println("Quick Desc High"); 35 | qs = new QuickSortDescHigh(); 36 | arr2 = arr.clone(); 37 | qs.sort(arr2, 0, n - 1); 38 | printArray(arr2); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/after/QuickSort.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.after; 2 | 3 | public interface QuickSort { 4 | void sort(int arr[], int low, int high); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/after/QuickSortAscHigh.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.after; 2 | 3 | public class QuickSortAscHigh extends AbstractQuickSort { 4 | @Override 5 | public int getPivot(int[] arr, int low, int high) { 6 | return arr[high]; 7 | } 8 | 9 | @Override 10 | public boolean shouldSwapForward(int a, int b) { 11 | return a < b; 12 | } 13 | 14 | @Override 15 | public boolean shouldSwapBackward(int a, int b) { 16 | return a > b; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/after/QuickSortAscLow.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.after; 2 | 3 | public class QuickSortAscLow extends AbstractQuickSort { 4 | @Override 5 | public int getPivot(int[] arr, int low, int high) { 6 | return arr[low]; 7 | } 8 | 9 | @Override 10 | public boolean shouldSwapForward(int a, int b) { 11 | return a < b; 12 | } 13 | 14 | @Override 15 | public boolean shouldSwapBackward(int a, int b) { 16 | return a > b; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/after/QuickSortAscMed.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.after; 2 | 3 | public class QuickSortAscMed extends AbstractQuickSort { 4 | @Override 5 | public int getPivot(int[] arr, int low, int high) { 6 | int center = (low + high) / 2; 7 | 8 | if (arr[low] > arr[center]) 9 | swap(arr, low, center); 10 | 11 | if (arr[low] > arr[high]) 12 | swap(arr, low, high); 13 | 14 | if (arr[center] > arr[high]) 15 | swap(arr, center, high); 16 | 17 | swap(arr, center, high - 1); 18 | return arr[high - 1]; 19 | } 20 | 21 | @Override 22 | public boolean shouldSwapForward(int a, int b) { 23 | return a < b; 24 | } 25 | 26 | @Override 27 | public boolean shouldSwapBackward(int a, int b) { 28 | return a > b; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/after/QuickSortDescHigh.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.after; 2 | 3 | public class QuickSortDescHigh extends AbstractQuickSort { 4 | @Override 5 | public int getPivot(int[] arr, int low, int high) { 6 | return arr[high]; 7 | } 8 | 9 | @Override 10 | public boolean shouldSwapForward(int a, int b) { 11 | return a > b; 12 | } 13 | 14 | @Override 15 | public boolean shouldSwapBackward(int a, int b) { 16 | return a < b; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/before/QuickSortAscHigh.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.before; 2 | 3 | 4 | public class QuickSortAscHigh { 5 | 6 | public void sort(int arr[], int low, int high) { 7 | int pivot = arr[high]; 8 | int i = low; 9 | int j = high; 10 | while (i <= j) { 11 | while (arr[i] < pivot) 12 | i++; 13 | while (arr[j] > pivot) 14 | j--; 15 | if (i <= j) { 16 | int temp = arr[i]; 17 | arr[i] = arr[j]; 18 | arr[j] = temp; 19 | i++; 20 | j--; 21 | } 22 | } 23 | if (low < j) 24 | sort(arr, low, j); 25 | if (i < high) 26 | sort(arr, i, high); 27 | } 28 | 29 | public static void printArray(int arr[]) { 30 | for (int anArr : arr) { 31 | System.out.print(anArr + " "); 32 | } 33 | System.out.println(); 34 | } 35 | 36 | public static void main(String[] args) { 37 | int arr[] = {48, 1, 6, 4, 6, 41, 8, 4, 6, 2, 1, 56, 6, 4}; 38 | int n = arr.length; 39 | 40 | QuickSortAscHigh qs = new QuickSortAscHigh(); 41 | qs.sort(arr, 0, n - 1); 42 | printArray(arr); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/before/QuickSortAscLow.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.before; 2 | 3 | 4 | public class QuickSortAscLow { 5 | 6 | public void sort(int arr[], int low, int high) { 7 | int pivot = arr[low]; 8 | int i = low; 9 | int j = high; 10 | while (i <= j) { 11 | while (arr[i] < pivot) 12 | i++; 13 | while (arr[j] > pivot) 14 | j--; 15 | if (i <= j) { 16 | int temp = arr[i]; 17 | arr[i] = arr[j]; 18 | arr[j] = temp; 19 | i++; 20 | j--; 21 | } 22 | } 23 | if (low < j) 24 | sort(arr, low, j); 25 | if (i < high) 26 | sort(arr, i, high); 27 | } 28 | 29 | public static void printArray(int arr[]) { 30 | for (int anArr : arr) { 31 | System.out.print(anArr + " "); 32 | } 33 | System.out.println(); 34 | } 35 | 36 | public static void main(String[] args) { 37 | int arr[] = {48, 1, 6, 4, 6, 41, 8, 4, 6, 2, 1, 56, 6, 4}; 38 | int n = arr.length; 39 | 40 | QuickSortAscLow qs = new QuickSortAscLow(); 41 | qs.sort(arr, 0, n - 1); 42 | printArray(arr); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/before/QuickSortAscMed.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.before; 2 | 3 | 4 | public class QuickSortAscMed { 5 | 6 | public void sort(int arr[], int low, int high) { 7 | int temp; 8 | int center = (low + high) >>> 1; 9 | 10 | if (arr[low] > arr[center]) { 11 | temp = arr[low]; 12 | arr[low] = arr[center]; 13 | arr[center] = temp; 14 | } 15 | 16 | if (arr[low] > arr[high]) { 17 | temp = arr[low]; 18 | arr[low] = arr[high]; 19 | arr[high] = temp; 20 | } 21 | 22 | if (arr[center] > arr[high]) { 23 | temp = arr[center]; 24 | arr[center] = arr[high]; 25 | arr[high] = temp; 26 | } 27 | 28 | temp = arr[center]; 29 | arr[center] = arr[high - 1]; 30 | arr[high - 1] = temp; 31 | 32 | 33 | int pivot = arr[high - 1]; 34 | 35 | int i = low; 36 | int j = high; 37 | while (i <= j) { 38 | while (arr[i] < pivot) 39 | i++; 40 | while (arr[j] > pivot) 41 | j--; 42 | if (i <= j) { 43 | int temp2 = arr[i]; 44 | arr[i] = arr[j]; 45 | arr[j] = temp2; 46 | i++; 47 | j--; 48 | } 49 | } 50 | if (low < j) 51 | sort(arr, low, j); 52 | if (i < high) 53 | sort(arr, i, high); 54 | } 55 | 56 | public static void printArray(int arr[]) { 57 | for (int anArr : arr) { 58 | System.out.print(anArr + " "); 59 | } 60 | System.out.println(); 61 | } 62 | 63 | public static void main(String[] args) { 64 | int arr[] = {48, 1, 6, 4, 6, 41, 8, 4, 6, 2, 1, 56, 6, 4}; 65 | int n = arr.length; 66 | 67 | QuickSortAscMed qs = new QuickSortAscMed(); 68 | qs.sort(arr, 0, n - 1); 69 | printArray(arr); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/template/sorting/before/QuickSortDescHigh.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.template.sorting.before; 2 | 3 | 4 | public class QuickSortDescHigh { 5 | 6 | public void sort(int arr[], int low, int high) { 7 | int pivot = arr[high]; 8 | int i = low; 9 | int j = high; 10 | while (i <= j) { 11 | while (arr[i] > pivot) 12 | i++; 13 | while (arr[j] < pivot) 14 | j--; 15 | if (i <= j) { 16 | int temp = arr[i]; 17 | arr[i] = arr[j]; 18 | arr[j] = temp; 19 | i++; 20 | j--; 21 | } 22 | } 23 | if (low < j) 24 | sort(arr, low, j); 25 | if (i < high) 26 | sort(arr, i, high); 27 | } 28 | 29 | public static void printArray(int arr[]) { 30 | for (int anArr : arr) { 31 | System.out.print(anArr + " "); 32 | } 33 | System.out.println(); 34 | } 35 | 36 | public static void main(String[] args) { 37 | int arr[] = {48, 1, 6, 4, 6, 41, 8, 4, 6, 2, 1, 56, 6, 4}; 38 | int n = arr.length; 39 | 40 | QuickSortDescHigh qs = new QuickSortDescHigh(); 41 | qs.sort(arr, 0, n - 1); 42 | printArray(arr); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/ast/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.ast; 2 | 3 | import com.cc3002.patterns.visitor.ast.bool.AndExp; 4 | import com.cc3002.patterns.visitor.ast.bool.BoolExp; 5 | import com.cc3002.patterns.visitor.ast.bool.Literal; 6 | import com.cc3002.patterns.visitor.ast.bool.OrExp; 7 | import com.cc3002.patterns.visitor.ast.visitor.DenyVisitor; 8 | import com.cc3002.patterns.visitor.ast.visitor.EvalVisitor; 9 | import com.cc3002.patterns.visitor.ast.visitor.PrintVisitor; 10 | 11 | public class Main { 12 | public static void main(String[] args) { 13 | BoolExp root = 14 | new AndExp( 15 | new OrExp( 16 | new OrExp( 17 | new Literal(true), 18 | new OrExp( 19 | new Literal(false), 20 | new Literal(true))), 21 | new AndExp( 22 | new Literal(true), 23 | new Literal(false))), 24 | new Literal(true)); 25 | PrintVisitor printer = new PrintVisitor(); 26 | root.accept(printer); 27 | printer.print(); 28 | 29 | DenyVisitor denier = new DenyVisitor(); 30 | root.accept(denier); 31 | BoolExp deniedRoot = denier.getDenied(); 32 | printer = new PrintVisitor(); 33 | deniedRoot.accept(printer); 34 | printer.print(); 35 | 36 | EvalVisitor eval = new EvalVisitor(); 37 | root.accept(eval); 38 | System.out.println(eval.getValue()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/ast/bool/AndExp.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.ast.bool; 2 | 3 | import com.cc3002.patterns.visitor.ast.visitor.Visitor; 4 | 5 | public class AndExp implements BoolExp { 6 | private BoolExp left, right; 7 | 8 | public AndExp(BoolExp left, BoolExp right) { 9 | this.left = left; 10 | this.right = right; 11 | } 12 | 13 | @Override 14 | public boolean eval() { 15 | if (left.eval()) { 16 | return right.eval(); 17 | } 18 | return false; 19 | } 20 | 21 | @Override 22 | public void accept(Visitor v) { 23 | v.visitAnd(this); 24 | } 25 | 26 | public BoolExp getLeft() { 27 | return left; 28 | } 29 | 30 | public BoolExp getRight() { 31 | return right; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/ast/bool/BoolExp.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.ast.bool; 2 | 3 | import com.cc3002.patterns.visitor.ast.visitor.Visitor; 4 | 5 | public interface BoolExp { 6 | boolean eval(); 7 | 8 | void accept(Visitor v); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/ast/bool/Literal.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.ast.bool; 2 | 3 | import com.cc3002.patterns.visitor.ast.visitor.Visitor; 4 | 5 | public class Literal implements BoolExp { 6 | private boolean value; 7 | 8 | public Literal(boolean value) { 9 | this.value = value; 10 | } 11 | 12 | @Override 13 | public boolean eval() { 14 | return value; 15 | } 16 | 17 | @Override 18 | public void accept(Visitor v) { 19 | v.visitLiteral(this); 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return "" + value; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/ast/bool/OrExp.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.ast.bool; 2 | 3 | import com.cc3002.patterns.visitor.ast.visitor.Visitor; 4 | 5 | public class OrExp implements BoolExp { 6 | private BoolExp left, right; 7 | 8 | public OrExp(BoolExp left, BoolExp right) { 9 | this.left = left; 10 | this.right = right; 11 | } 12 | 13 | @Override 14 | public boolean eval() { 15 | if (left.eval()) { 16 | return true; 17 | } 18 | return right.eval(); 19 | } 20 | 21 | @Override 22 | public void accept(Visitor v) { 23 | v.visitOr(this); 24 | } 25 | 26 | public BoolExp getLeft() { 27 | return left; 28 | } 29 | 30 | public BoolExp getRight() { 31 | return right; 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/ast/visitor/DenyVisitor.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.ast.visitor; 2 | 3 | import com.cc3002.patterns.visitor.ast.bool.AndExp; 4 | import com.cc3002.patterns.visitor.ast.bool.BoolExp; 5 | import com.cc3002.patterns.visitor.ast.bool.Literal; 6 | import com.cc3002.patterns.visitor.ast.bool.OrExp; 7 | 8 | public class DenyVisitor implements Visitor { 9 | private BoolExp denied; 10 | 11 | @Override 12 | public void visitLiteral(Literal l) { 13 | denied = new Literal(!l.eval()); 14 | } 15 | 16 | @Override 17 | public void visitAnd(AndExp and) { 18 | and.getLeft().accept(this); 19 | BoolExp noleft = denied; 20 | and.getRight().accept(this); 21 | BoolExp noright = denied; 22 | denied = new OrExp(noleft, noright); 23 | } 24 | 25 | @Override 26 | public void visitOr(OrExp or) { 27 | or.getLeft().accept(this); 28 | BoolExp noleft = denied; 29 | or.getRight().accept(this); 30 | BoolExp noright = denied; 31 | denied = new AndExp(noleft, noright); 32 | } 33 | 34 | public BoolExp getDenied() { 35 | return denied; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/ast/visitor/EvalVisitor.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.ast.visitor; 2 | 3 | import com.cc3002.patterns.visitor.ast.bool.AndExp; 4 | import com.cc3002.patterns.visitor.ast.bool.Literal; 5 | import com.cc3002.patterns.visitor.ast.bool.OrExp; 6 | 7 | public class EvalVisitor implements Visitor { 8 | private boolean value; 9 | 10 | @Override 11 | public void visitLiteral(Literal l) { 12 | value = l.eval(); 13 | } 14 | 15 | @Override 16 | public void visitAnd(AndExp and) { 17 | and.getLeft().accept(this); 18 | if (!value) { 19 | value = false; 20 | return; 21 | } 22 | boolean left = value; 23 | and.getRight().accept(this); 24 | boolean right = value; 25 | value = left && right; 26 | } 27 | 28 | @Override 29 | public void visitOr(OrExp or) { 30 | or.getLeft().accept(this); 31 | if (value) return; 32 | or.getRight().accept(this); 33 | } 34 | 35 | public boolean getValue() { 36 | return value; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/ast/visitor/PrintVisitor.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.ast.visitor; 2 | 3 | import com.cc3002.patterns.visitor.ast.bool.AndExp; 4 | import com.cc3002.patterns.visitor.ast.bool.Literal; 5 | import com.cc3002.patterns.visitor.ast.bool.OrExp; 6 | 7 | public class PrintVisitor implements Visitor { 8 | private StringBuilder builder = new StringBuilder(); 9 | 10 | @Override 11 | public void visitLiteral(Literal l) { 12 | builder.append(l.toString()); 13 | } 14 | 15 | @Override 16 | public void visitAnd(AndExp and) { 17 | builder.append("("); 18 | and.getLeft().accept(this); 19 | builder.append(" AND "); 20 | and.getRight().accept(this); 21 | builder.append(")"); 22 | } 23 | 24 | @Override 25 | public void visitOr(OrExp or) { 26 | builder.append("("); 27 | or.getLeft().accept(this); 28 | builder.append(" OR "); 29 | or.getRight().accept(this); 30 | builder.append(")"); 31 | } 32 | 33 | public void print() { 34 | System.out.println(builder.toString()); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/ast/visitor/Visitor.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.ast.visitor; 2 | 3 | import com.cc3002.patterns.visitor.ast.bool.AndExp; 4 | import com.cc3002.patterns.visitor.ast.bool.Literal; 5 | import com.cc3002.patterns.visitor.ast.bool.OrExp; 6 | 7 | public interface Visitor { 8 | void visitLiteral(Literal l); 9 | 10 | void visitAnd(AndExp and); 11 | 12 | void visitOr(OrExp or); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/FoodItem.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu; 2 | 3 | import com.cc3002.patterns.visitor.menu.operations.Visitor; 4 | 5 | public class FoodItem implements MenuItem { 6 | private String name; 7 | private String description; 8 | private boolean vegetarian; 9 | private double price; 10 | 11 | public FoodItem(String name, String description, boolean vegetarian, double price) { 12 | this.name = name; 13 | this.description = description; 14 | this.vegetarian = vegetarian; 15 | this.price = price; 16 | } 17 | 18 | @Override 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | @Override 24 | public String getDescription() { 25 | return description; 26 | } 27 | 28 | @Override 29 | public double getPrice() { 30 | return price; 31 | } 32 | 33 | @Override 34 | public boolean isVegetarian() { 35 | return vegetarian; 36 | } 37 | 38 | @Override 39 | public void accept(Visitor visitor) { 40 | visitor.visitFoodItem(this); 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "FoodItem{" + 46 | "name='" + name + '\'' + 47 | ", description='" + description + '\'' + 48 | ", vegetarian=" + vegetarian + 49 | ", price=" + price + 50 | '}'; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/FoodMenu.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu; 2 | 3 | import com.cc3002.patterns.visitor.menu.operations.Visitor; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class FoodMenu implements Menu { 9 | private String name; 10 | private List items = new ArrayList<>(); 11 | 12 | public FoodMenu(String name) { 13 | this.name = name; 14 | } 15 | 16 | @Override 17 | public long size() { 18 | return items.size(); 19 | } 20 | 21 | @Override 22 | public void addItem(MenuComponent menuComponent) { 23 | items.add(menuComponent); 24 | } 25 | 26 | @Override 27 | public List getItems() { 28 | return items; 29 | } 30 | 31 | @Override 32 | public String getName() { 33 | return name; 34 | } 35 | 36 | @Override 37 | public void accept(Visitor visitor) { 38 | visitor.visitFoodMenu(this); 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "FoodMenu{" + 44 | "name='" + name + '\'' + 45 | '}'; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu; 2 | 3 | import com.cc3002.patterns.visitor.menu.operations.search.ExactSearch; 4 | import com.cc3002.patterns.visitor.menu.operations.search.ItemNumberLessEq; 5 | import com.cc3002.patterns.visitor.menu.operations.search.Search; 6 | import com.cc3002.patterns.visitor.menu.operations.sum.Sum; 7 | import com.cc3002.patterns.visitor.menu.operations.sum.SumTotalPrice; 8 | import com.cc3002.patterns.visitor.menu.operations.sum.SumVegetarianPrice; 9 | 10 | public class Main { 11 | public static void main(String[] args) { 12 | FoodMenu mainMenu = new FoodMenu("Main Menu"); 13 | 14 | FoodMenu subMenu1 = new FoodMenu("Sub Menu 1"); 15 | subMenu1.addItem(new FoodItem("SubMenu1 Item1", "Item1", false, 100)); 16 | subMenu1.addItem(new FoodItem("SubMenu1 Item2", "Item2", true, 200)); 17 | subMenu1.addItem(new FoodItem("SubMenu1 Item3", "Item3", true, 50)); 18 | subMenu1.addItem(new FoodItem("SubMenu1 Item4", "Item4", false, 1000)); 19 | 20 | FoodMenu subMenu2 = new FoodMenu("Sub Menu 2"); 21 | subMenu2.addItem(new FoodItem("SubMenu2 Item1", "Item1", false, 1000)); 22 | subMenu2.addItem(new FoodItem("SubMenu2 Item2", "Item2", true, 2000)); 23 | 24 | mainMenu.addItem(subMenu1); 25 | mainMenu.addItem(subMenu2); 26 | 27 | Search exactName1 = new ExactSearch("SubMenu1 Item2"); 28 | mainMenu.accept(exactName1); 29 | System.out.println(exactName1.getMatching()); 30 | 31 | Search exactName2 = new ExactSearch("Sub Menu 1"); 32 | mainMenu.accept(exactName2); 33 | System.out.println(exactName2.getMatching()); 34 | 35 | Search lessEq3 = new ItemNumberLessEq(3); 36 | mainMenu.accept(lessEq3); 37 | System.out.println(lessEq3.getMatching()); 38 | 39 | Sum totalPrice = new SumTotalPrice(); 40 | mainMenu.accept(totalPrice); 41 | System.out.println(totalPrice.getPrice()); 42 | 43 | Sum totalPriceSub = new SumTotalPrice(); 44 | subMenu2.accept(totalPriceSub); 45 | System.out.println(totalPriceSub.getPrice()); 46 | 47 | Sum totalPriceVeg = new SumVegetarianPrice(); 48 | mainMenu.accept(totalPriceVeg); 49 | System.out.println(totalPriceVeg.getPrice()); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/Menu.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu; 2 | 3 | import java.util.List; 4 | 5 | public interface Menu extends MenuComponent { 6 | long size(); 7 | 8 | void addItem(MenuComponent menuComponent); 9 | 10 | List getItems(); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/MenuComponent.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu; 2 | 3 | import com.cc3002.patterns.visitor.menu.operations.Visitor; 4 | 5 | public interface MenuComponent { 6 | String getName(); 7 | 8 | void accept(Visitor visitor); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/MenuItem.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu; 2 | 3 | public interface MenuItem extends MenuComponent { 4 | String getDescription(); 5 | 6 | double getPrice(); 7 | 8 | boolean isVegetarian(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/operations/Visitor.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu.operations; 2 | 3 | import com.cc3002.patterns.visitor.menu.FoodItem; 4 | import com.cc3002.patterns.visitor.menu.FoodMenu; 5 | 6 | public interface Visitor { 7 | void visitFoodMenu(FoodMenu foodMenu); 8 | 9 | void visitFoodItem(FoodItem foodItem); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/operations/search/AbstractSearch.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu.operations.search; 2 | 3 | import com.cc3002.patterns.visitor.menu.FoodItem; 4 | import com.cc3002.patterns.visitor.menu.FoodMenu; 5 | import com.cc3002.patterns.visitor.menu.MenuComponent; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | public abstract class AbstractSearch implements Search { 11 | List matching = new ArrayList<>(); 12 | 13 | @Override 14 | public void visitFoodMenu(FoodMenu foodMenu) { 15 | for (MenuComponent item : foodMenu.getItems()) { 16 | item.accept(this); 17 | } 18 | } 19 | 20 | @Override 21 | public void visitFoodItem(FoodItem foodItem) { 22 | 23 | } 24 | 25 | @Override 26 | public abstract boolean match(MenuComponent menuComponent); 27 | 28 | @Override 29 | public List getMatching() { 30 | return matching; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/operations/search/ByPredicate.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu.operations.search; 2 | 3 | import com.cc3002.patterns.visitor.menu.FoodItem; 4 | import com.cc3002.patterns.visitor.menu.FoodMenu; 5 | import com.cc3002.patterns.visitor.menu.MenuComponent; 6 | 7 | public abstract class ByPredicate extends AbstractSearch { 8 | String predicate; 9 | 10 | public ByPredicate(String predicate) { 11 | this.predicate = predicate; 12 | } 13 | 14 | @Override 15 | public void visitFoodMenu(FoodMenu foodMenu) { 16 | if (match(foodMenu)) { 17 | matching.add(foodMenu); 18 | } 19 | super.visitFoodMenu(foodMenu); 20 | } 21 | 22 | @Override 23 | public void visitFoodItem(FoodItem foodItem) { 24 | if (match(foodItem)) { 25 | matching.add(foodItem); 26 | } 27 | } 28 | 29 | @Override 30 | public abstract boolean match(MenuComponent menuComponent); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/operations/search/ExactSearch.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu.operations.search; 2 | 3 | import com.cc3002.patterns.visitor.menu.MenuComponent; 4 | 5 | public class ExactSearch extends ByPredicate { 6 | public ExactSearch(String predicate) { 7 | super(predicate); 8 | } 9 | 10 | @Override 11 | public boolean match(MenuComponent menuComponent) { 12 | return menuComponent.getName().equals(predicate); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/operations/search/ItemNumberLessEq.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu.operations.search; 2 | 3 | import com.cc3002.patterns.visitor.menu.FoodMenu; 4 | import com.cc3002.patterns.visitor.menu.MenuComponent; 5 | 6 | public class ItemNumberLessEq extends AbstractSearch { 7 | private int minNumber; 8 | 9 | public ItemNumberLessEq(int minNumber) { 10 | this.minNumber = minNumber; 11 | } 12 | 13 | @Override 14 | public void visitFoodMenu(FoodMenu foodMenu) { 15 | if (match(foodMenu)) { 16 | matching.add(foodMenu); 17 | } 18 | super.visitFoodMenu(foodMenu); 19 | } 20 | 21 | @Override 22 | public boolean match(MenuComponent menuComponent) { 23 | return ((FoodMenu) menuComponent).size() <= minNumber; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/operations/search/Search.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu.operations.search; 2 | 3 | import com.cc3002.patterns.visitor.menu.MenuComponent; 4 | import com.cc3002.patterns.visitor.menu.operations.Visitor; 5 | 6 | import java.util.List; 7 | 8 | public interface Search extends Visitor { 9 | boolean match(MenuComponent menuComponent); 10 | 11 | List getMatching(); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/operations/sum/Sum.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu.operations.sum; 2 | 3 | import com.cc3002.patterns.visitor.menu.operations.Visitor; 4 | 5 | public interface Sum extends Visitor { 6 | double getPrice(); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/operations/sum/SumPrice.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu.operations.sum; 2 | 3 | import com.cc3002.patterns.visitor.menu.FoodItem; 4 | import com.cc3002.patterns.visitor.menu.FoodMenu; 5 | import com.cc3002.patterns.visitor.menu.MenuComponent; 6 | 7 | public abstract class SumPrice implements Sum { 8 | double price; 9 | 10 | @Override 11 | public void visitFoodMenu(FoodMenu foodMenu) { 12 | for (MenuComponent item : foodMenu.getItems()) { 13 | item.accept(this); 14 | } 15 | } 16 | 17 | @Override 18 | public abstract void visitFoodItem(FoodItem foodItem); 19 | 20 | @Override 21 | public double getPrice() { 22 | return price; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/operations/sum/SumTotalPrice.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu.operations.sum; 2 | 3 | import com.cc3002.patterns.visitor.menu.FoodItem; 4 | 5 | public class SumTotalPrice extends SumPrice { 6 | 7 | @Override 8 | public void visitFoodItem(FoodItem foodItem) { 9 | price += foodItem.getPrice(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/menu/operations/sum/SumVegetarianPrice.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.menu.operations.sum; 2 | 3 | import com.cc3002.patterns.visitor.menu.FoodItem; 4 | 5 | public class SumVegetarianPrice extends SumPrice { 6 | 7 | @Override 8 | public void visitFoodItem(FoodItem foodItem) { 9 | if (foodItem.isVegetarian()) { 10 | price += foodItem.getPrice(); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/observer/Main.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.observer; 2 | 3 | import com.cc3002.patterns.visitor.observer.news.Video; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | MaybeSeriousBroadcastingStation station = new MaybeSeriousBroadcastingStation(); 8 | NewsGenerator news = new NewsGenerator(); 9 | news.subscribe(station); 10 | 11 | news.newActualNews("/AAAAAAAAAAAAA EVERYONE RUN!!!"); 12 | 13 | news.newCatVideo(new Video("LOL this is g8")); 14 | 15 | news.newWhatever(); 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/observer/MaybeSeriousBroadcastingStation.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.observer; 2 | 3 | import com.cc3002.patterns.visitor.observer.news.*; 4 | 5 | import java.util.Observable; 6 | import java.util.Observer; 7 | 8 | public class MaybeSeriousBroadcastingStation implements Observer, NewsPublisher { 9 | 10 | @Override 11 | public void update(Observable o, Object arg) { 12 | if (arg instanceof News) { 13 | ((News) arg).accept(this); 14 | } 15 | } 16 | 17 | @Override 18 | public void publishCelebrityNews(CelebrityStuff celebrityStuff) { 19 | System.out.println("Nobody cares..."); 20 | } 21 | 22 | @Override 23 | public void publishBreakingNews(BreakingNews breakingNews) { 24 | System.out.println("BREAKING NEWS!!!"); 25 | System.out.println("\t" + breakingNews.getInformation()); 26 | } 27 | 28 | @Override 29 | public void publishFunCatVideos(FunCatVideos funCatVideos) { 30 | System.out.println("HAHA " + funCatVideos.getVideo()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/observer/NewsGenerator.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.observer; 2 | 3 | import com.cc3002.patterns.visitor.observer.news.BreakingNews; 4 | import com.cc3002.patterns.visitor.observer.news.CelebrityStuff; 5 | import com.cc3002.patterns.visitor.observer.news.FunCatVideos; 6 | import com.cc3002.patterns.visitor.observer.news.Video; 7 | 8 | import java.util.Observable; 9 | import java.util.Observer; 10 | 11 | public class NewsGenerator extends Observable { 12 | public void subscribe(Observer station) { 13 | addObserver(station); 14 | } 15 | 16 | public void newCatVideo(Video video) { 17 | setChanged(); 18 | notifyObservers(new FunCatVideos(video)); 19 | } 20 | 21 | public void newWhatever() { 22 | setChanged(); 23 | notifyObservers(new CelebrityStuff("THEY KISSED!!!!!!")); 24 | } 25 | 26 | public void newActualNews(String information) { 27 | setChanged(); 28 | notifyObservers(new BreakingNews(information)); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/observer/news/BreakingNews.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.observer.news; 2 | 3 | public class BreakingNews implements News { 4 | private String information; 5 | 6 | public BreakingNews(String information) { 7 | this.information = information; 8 | } 9 | 10 | @Override 11 | public void accept(NewsPublisher newsPublisher) { 12 | newsPublisher.publishBreakingNews(this); 13 | } 14 | 15 | public String getInformation() { 16 | return information; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/observer/news/CelebrityStuff.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.observer.news; 2 | 3 | public class CelebrityStuff implements News { 4 | private String scoop; 5 | 6 | public CelebrityStuff(String scoop) { 7 | this.scoop = scoop; 8 | } 9 | 10 | @Override 11 | public void accept(NewsPublisher newsPublisher) { 12 | newsPublisher.publishCelebrityNews(this); 13 | } 14 | 15 | public String getScoop() { 16 | return scoop; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/observer/news/FunCatVideos.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.observer.news; 2 | 3 | public class FunCatVideos implements News { 4 | private Video video; 5 | 6 | public FunCatVideos(Video video) { 7 | this.video = video; 8 | } 9 | 10 | @Override 11 | public void accept(NewsPublisher newsPublisher) { 12 | newsPublisher.publishFunCatVideos(this); 13 | } 14 | 15 | public Video getVideo() { 16 | return video; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/observer/news/News.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.observer.news; 2 | 3 | public interface News { 4 | void accept(NewsPublisher newsPublisher); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/observer/news/NewsPublisher.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.observer.news; 2 | 3 | public interface NewsPublisher { 4 | void publishCelebrityNews(CelebrityStuff celebrityStuff); 5 | 6 | void publishBreakingNews(BreakingNews breakingNews); 7 | 8 | void publishFunCatVideos(FunCatVideos funCatVideos); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/patterns/visitor/observer/news/Video.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.patterns.visitor.observer.news; 2 | 3 | public class Video { 4 | private String videoName; 5 | 6 | public Video(String videoName) { 7 | this.videoName = videoName; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | return videoName; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cc3002/threads/singleton/ThreadSafeSingleton.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.threads.singleton; 2 | 3 | public class ThreadSafeSingleton { 4 | private static volatile ThreadSafeSingleton instance; 5 | // private static final ThreadSafeSingleton instance 6 | 7 | private ThreadSafeSingleton() {} 8 | 9 | public static ThreadSafeSingleton getInstance() { 10 | if (instance == null) { 11 | synchronized (ThreadSafeSingleton.class) { 12 | if (instance == null) { 13 | instance = new ThreadSafeSingleton(); 14 | } 15 | } 16 | } 17 | return instance; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/resources/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanpablos/ExercisesInJava/6c7be441c05070efdf8b6147e4a684fd5b03dcaa/src/main/resources/.gitkeep -------------------------------------------------------------------------------- /src/test/java/com/cc3002/doubledispatch/TestDollar.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.doubledispatch; 2 | 3 | import com.cc3002.patterns.doubledispatch.money.CLP; 4 | import com.cc3002.patterns.doubledispatch.money.IMoney; 5 | import com.cc3002.patterns.doubledispatch.money.USD; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import static junit.framework.TestCase.assertEquals; 10 | import static junit.framework.TestCase.assertNotNull; 11 | 12 | public class TestDollar { 13 | private IMoney dollar100; 14 | 15 | @Before 16 | public void setUp() { 17 | dollar100 = new USD(100); 18 | } 19 | 20 | @Test 21 | public void testNotNull() { 22 | assertNotNull(dollar100); 23 | } 24 | 25 | @Test 26 | public void testDollarValue() { 27 | assertEquals(100., dollar100.getMoney()); 28 | } 29 | 30 | @Test 31 | public void testDollarDollar() { 32 | IMoney dollar50 = new USD(50); 33 | IMoney dollar150 = dollar100.add(dollar50); 34 | assertEquals(150., dollar150.getMoney()); 35 | } 36 | 37 | @Test 38 | public void testDollarChile() { 39 | IMoney clp1500 = new CLP(1500); 40 | IMoney appDollar102 = dollar100.add(clp1500); 41 | assertEquals(102.5, appDollar102.getMoney(), 0.1); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/com/cc3002/exception/ExceptionTest.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.exception; 2 | 3 | import com.cc3002.java.exceptions.A; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import static junit.framework.TestCase.assertTrue; 8 | 9 | public class ExceptionTest { 10 | private A a; 11 | 12 | @Before 13 | public void setUp() { 14 | a = new A(); 15 | } 16 | 17 | @Test 18 | public void exceptionTest1() throws Exception { 19 | boolean thrown = false; 20 | try { 21 | a.method(); 22 | } catch (Exception e) { 23 | thrown = true; 24 | } 25 | assertTrue(thrown); 26 | } 27 | 28 | @Test(expected = Exception.class) 29 | public void exceptionTest2() throws Exception { 30 | a.method(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/com/cc3002/testannotation/BasicAnnotations.java: -------------------------------------------------------------------------------- 1 | package com.cc3002.testannotation; 2 | 3 | import org.junit.*; 4 | 5 | public class BasicAnnotations { 6 | @BeforeClass 7 | public static void beforeClass() { 8 | System.out.println("BeforeClass - Run first only one time\n"); 9 | } 10 | @AfterClass 11 | public static void afterClass() { 12 | System.out.println("AfterClass - Run last only one time"); 13 | } 14 | @Before 15 | public void beforeMethod() { 16 | System.out.println("Before - Run before each @Test"); 17 | } 18 | @After 19 | public void afterMethod() { 20 | System.out.println("After - Run after each @Test\n"); 21 | } 22 | 23 | @Test 24 | public void testMethodOne() { 25 | System.out.println("Test - Test number 1"); 26 | } 27 | 28 | @Test 29 | public void testMethodTwo() { 30 | System.out.println("Test - Test number 2"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/resources/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanpablos/ExercisesInJava/6c7be441c05070efdf8b6147e4a684fd5b03dcaa/src/test/resources/.gitkeep --------------------------------------------------------------------------------