├── DesignPatterns-Presentation.pdf └── README.md /DesignPatterns-Presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in28minutes/Design-Patterns-For-Beginners/f1d60908428220f562a0500c225c84a9c0464d40/DesignPatterns-Presentation.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Design Patterns For Beginners 2 | ## Todo 3 | - Add link to presentation 4 | 5 | ## Expectations 6 | - A little bit of experience with Java would help but not Mandatory. 7 | - Ability to understand simple bits of Object Oriented Code would help but not Mandatory. 8 | 9 | ## Let's have some fun 10 | - What are we waiting for? 11 | - Let's have some fun with Design Patterns. 12 | - I had fun creating this course and hope you would too. 13 | - Thanks for your interest in Our Course 14 | - I hope you’re as excited as I am! 15 | - If you’re ready to learn more and sign up for the course, 16 | - go ahead and hit that Enroll button, 17 | - or take a test drive by using the Free Preview feature. 18 | - See you in the course! 19 | 20 | ## Conclusion 21 | - I had fun creating this course and I'm sure you had some fun too. 22 | - Good Luck and Bye from the team here at in28Minutes 23 | - Do not forget to leave us a review. 24 | 25 | ## Example Java Files 26 | ### src/com/in28minutes/patterns/AdapterPattern.java 27 | ``` 28 | package com.in28minutes.patterns; 29 | 30 | import java.util.ArrayList; 31 | import java.util.Arrays; 32 | import java.util.HashMap; 33 | import java.util.List; 34 | import java.util.Map; 35 | 36 | /* 37 | * translates one interface for a class into a compatible interface.[1] An adapter allows classes to work together that normally could not because of incompatible interfaces 38 | */ 39 | public class AdapterPattern { 40 | static class CountriesInterface { 41 | public List getList() { 42 | List countries = Arrays.asList("IND", 43 | "PAK", "SL"); 44 | return countries; 45 | } 46 | } 47 | 48 | static class CountriesAdapter { 49 | private static Map countryCodeNameMapping = new HashMap(); 50 | static { 51 | countryCodeNameMapping.put("IND", "India"); 52 | countryCodeNameMapping.put("PAK", "Pakistan"); 53 | countryCodeNameMapping.put("SL", "Sri Lanka"); 54 | //...A lot of others as well 55 | } 56 | 57 | public List getTranslatedList() { 58 | List countryCodes = new CountriesInterface() 59 | .getList(); 60 | List countryNames = new ArrayList(); 61 | for (String country : countryCodes) { 62 | countryNames.add(countryCodeNameMapping 63 | .get(country)); 64 | } 65 | return countryNames; 66 | } 67 | } 68 | 69 | public static void main(String[] args) { 70 | System.out.println(new CountriesAdapter() 71 | .getTranslatedList()); 72 | } 73 | } 74 | ``` 75 | ### src/com/in28minutes/patterns/BuilderPattern.java 76 | ``` 77 | package com.in28minutes.patterns; 78 | 79 | public class BuilderPattern { 80 | 81 | static class Coffee { 82 | private Coffee(Builder builder) { 83 | this.type = builder.type; 84 | this.sugar = builder.sugar; 85 | this.milk = builder.milk; 86 | this.size = builder.size; 87 | } 88 | 89 | private String type;//Should be a enum Lazy bum 90 | private boolean sugar; 91 | private boolean milk; 92 | private String size;//Should be a enum Lazy bum 93 | 94 | public static class Builder { 95 | private String type;//Should be a enum Lazy bum 96 | private boolean sugar; 97 | private boolean milk; 98 | private String size;//Should be a enum Lazy bum 99 | 100 | public Builder(String type) { 101 | this.type = type; 102 | } 103 | 104 | public Builder sugar(boolean value) { 105 | sugar = value; 106 | return this; 107 | } 108 | 109 | public Builder milk(boolean value) { 110 | milk = value; 111 | return this; 112 | } 113 | 114 | public Builder size(String value) { 115 | size = value; 116 | return this; 117 | } 118 | 119 | public Coffee build() { 120 | return new Coffee(this); 121 | } 122 | } 123 | 124 | @Override 125 | public String toString() { 126 | return String 127 | .format("Coffee [type=%s, sugar=%s, milk=%s, size=%s]", 128 | type, sugar, milk, size); 129 | } 130 | 131 | } 132 | 133 | public static void main(String[] args) { 134 | Coffee coffee = new Coffee.Builder("Mocha").milk( 135 | true).sugar(false).size("Large").build(); 136 | System.out.println(coffee); 137 | 138 | //Simplifies Creation 139 | //More Readable Code 140 | //Values cannot be modified 141 | } 142 | } 143 | ``` 144 | ### src/com/in28minutes/patterns/FactoryPattern.java 145 | ``` 146 | package com.in28minutes.patterns; 147 | /* 148 | * instantiate an object from one among a set of classes based on some logic 149 | */ 150 | public class FactoryPattern { 151 | public static class PersonFactory { 152 | public static Person getPerson(String name, 153 | String gender) { 154 | if(gender.equalsIgnoreCase("M")){ 155 | return new Male(name); 156 | }else if(gender.equalsIgnoreCase("F")){ 157 | return new Female(name); 158 | } //So on.. 159 | return null; 160 | } 161 | } 162 | 163 | static abstract class Person { 164 | Person(String name){ 165 | this.name = name; 166 | } 167 | private String name; 168 | abstract String getSalutation(); 169 | String getNameAndSalutation(){ 170 | return getSalutation() + " " + name; 171 | } 172 | } 173 | 174 | static class Male extends Person{ 175 | public Male(String name) { 176 | super(name); 177 | } 178 | 179 | @Override 180 | String getSalutation() { 181 | return "Mr"; 182 | } 183 | 184 | } 185 | 186 | static class Female extends Person{ 187 | public Female(String name) { 188 | super(name); 189 | } 190 | 191 | @Override 192 | String getSalutation() { 193 | return "Miss/Mrs"; 194 | } 195 | 196 | } 197 | 198 | public static void main(String[] args) { 199 | Person male = PersonFactory.getPerson("Robinhood","M"); 200 | System.out.println(male.getNameAndSalutation()); 201 | Person female = PersonFactory.getPerson("Mary","F"); 202 | System.out.println(female.getNameAndSalutation()); 203 | } 204 | } 205 | ``` 206 | ### src/com/in28minutes/patterns/MySingletonUsingEnum.java 207 | ``` 208 | package com.in28minutes.patterns; 209 | 210 | public enum MySingletonUsingEnum { 211 | INSTANCE; 212 | private MySingletonUsingEnum() { 213 | System.out.println("Here"); 214 | } 215 | 216 | public String retrieveSomething() { 217 | return "DUMMY"; 218 | } 219 | 220 | } 221 | ``` 222 | ### src/com/in28minutes/patterns/ObserverPattern.java 223 | ``` 224 | package com.in28minutes.patterns; 225 | 226 | import java.util.ArrayList; 227 | import java.util.List; 228 | /* 229 | * You want to know when an event happens 230 | */ 231 | public class ObserverPattern { 232 | static class SachinCenturyNotifier{ 233 | List fans = new ArrayList(); 234 | void register(SachinFan fan){ 235 | fans.add(fan); 236 | } 237 | void sachinScoredACentury(){ 238 | for(SachinFan fan:fans){ 239 | fan.announce(); 240 | } 241 | } 242 | } 243 | 244 | static class SachinFan { 245 | private String name; 246 | SachinFan(String name){ 247 | this.name = name; 248 | } 249 | void announce(){ 250 | System.out.println(name + " notified"); 251 | } 252 | } 253 | 254 | public static void main(String[] args) { 255 | SachinCenturyNotifier notifier = new SachinCenturyNotifier(); 256 | notifier.register(new SachinFan("Ranga")); 257 | notifier.register(new SachinFan("Ramya")); 258 | notifier.register(new SachinFan("Veena")); 259 | notifier.sachinScoredACentury(); 260 | 261 | /* 262 | * Ranga notified 263 | Ramya notified 264 | Veena notified 265 | 266 | */ 267 | } 268 | } 269 | ``` 270 | ### src/com/in28minutes/patterns/SingletonPattern.java 271 | ``` 272 | package com.in28minutes.patterns; 273 | 274 | public class SingletonPattern { 275 | static class Singleton { 276 | private static Singleton instance = new Singleton(); 277 | 278 | private Singleton() { 279 | } 280 | 281 | public static Singleton getSingleInstance() { 282 | return instance; 283 | } 284 | 285 | } 286 | } 287 | ``` 288 | ### src/com/in28minutes/patterns/StatePattern.java 289 | ``` 290 | package com.in28minutes.patterns; 291 | 292 | /* 293 | */ 294 | public class StatePattern { 295 | static class FanWallControl { 296 | private SpeedLevel current; 297 | 298 | public FanWallControl() { 299 | current = new Off(); 300 | } 301 | 302 | public void set_state(SpeedLevel state) { 303 | current = state; 304 | } 305 | 306 | public void rotate() { 307 | current.rotate(this); 308 | } 309 | 310 | @Override 311 | public String toString() { 312 | return String.format( 313 | "FanWallControl [current=%s]", current); 314 | } 315 | } 316 | 317 | interface SpeedLevel { 318 | void rotate(FanWallControl fanWallControl); 319 | } 320 | 321 | static class Off implements SpeedLevel { 322 | public void rotate(FanWallControl fanWallControl) { 323 | fanWallControl.set_state(new SpeedLevel1()); 324 | } 325 | } 326 | 327 | static class SpeedLevel1 implements SpeedLevel { 328 | public void rotate(FanWallControl fanWallControl) { 329 | fanWallControl.set_state(new SpeedLevel2()); 330 | } 331 | } 332 | 333 | static class SpeedLevel2 implements SpeedLevel { 334 | public void rotate(FanWallControl fanWallControl) { 335 | fanWallControl.set_state(new SpeedLevel3()); 336 | } 337 | } 338 | 339 | static class SpeedLevel3 implements SpeedLevel { 340 | public void rotate(FanWallControl fanWallControl) { 341 | fanWallControl.set_state(new Off()); 342 | } 343 | } 344 | 345 | public static void main(String[] args) { 346 | FanWallControl fanControl = new FanWallControl(); 347 | System.out.println(fanControl); 348 | fanControl.rotate(); 349 | System.out.println(fanControl); 350 | fanControl.rotate(); 351 | System.out.println(fanControl); 352 | fanControl.rotate(); 353 | System.out.println(fanControl); 354 | fanControl.rotate(); 355 | 356 | /* 357 | * 358 | * FanWallControl [current=com.rithus.patterns.StatePattern$Off@7a6d084b] 359 | FanWallControl [current=com.rithus.patterns.StatePattern$SpeedLevel1@2352544e] 360 | FanWallControl [current=com.rithus.patterns.StatePattern$SpeedLevel2@457471e0] 361 | FanWallControl [current=com.rithus.patterns.StatePattern$SpeedLevel3@7ecec0c5] 362 | * 363 | */ 364 | } 365 | } 366 | ``` 367 | ### src/com/in28minutes/patterns/StrategyPattern.java 368 | ``` 369 | package com.in28minutes.patterns; 370 | 371 | /* 372 | * Separates Strategy - how you do something - into a separate class. 373 | * Allows easy change of strategy at a later point. 374 | */ 375 | public class StrategyPattern { 376 | interface Sortable { 377 | public int[] sort(int[] numbers); 378 | } 379 | 380 | static class BubbleSort implements Sortable { 381 | @Override 382 | public int[] sort(int[] numbers) { 383 | // Ideally the bubble sort is implemented completely here 384 | return numbers; 385 | } 386 | } 387 | 388 | static class QuickSort implements Sortable { 389 | @Override 390 | public int[] sort(int[] numbers) { 391 | // Ideally the quick sort is implemented completely here 392 | return numbers; 393 | } 394 | } 395 | 396 | static class ComplexClass { 397 | Sortable sorter; 398 | 399 | ComplexClass(Sortable sorter) { 400 | this.sorter = sorter; 401 | } 402 | 403 | void doAComplexThing() { 404 | int[] values = null; // get from somewhere.. 405 | // ..logic.. 406 | sorter.sort(values); 407 | // ..logic.. 408 | } 409 | } 410 | 411 | public static void main(String[] args) { 412 | ComplexClass complexClassInstance = new ComplexClass(new BubbleSort()); 413 | // This can also be a setter.. 414 | complexClassInstance.doAComplexThing(); 415 | } 416 | } 417 | ``` 418 | 419 | ## About in28Minutes 420 | - At in28Minutes, we ask ourselves one question everyday. How do we help you learn effectively - that is more quickly and retain more of what you have learnt? 421 | - We use Problem-Solution based Step-By-Step Hands-on Approach With Practical, Real World Application Examples. 422 | - Our success on Udemy and Youtube (2 Million Views & 12K Subscribers) speaks volumes about the success of our approach. 423 | - While our primary expertise is on Development, Design & Architecture Java & Related Frameworks (Spring, Struts, Hibernate) we are expanding into the front-end world (Bootstrap, JQuery, Angular JS). 424 | 425 | ### Our Beliefs 426 | - Best Courses are interactive and fun. 427 | - Foundations for building high quality applications are best laid down while learning. 428 | 429 | ### Our Approach 430 | - Problem Solution based Step by Step Hands-on Learning 431 | - Practical, Real World Application Examples. 432 | - We use 80-20 Rule. We discuss 20% things used 80% of time in depth. We touch upon other things briefly equipping you with enough knowledge to find out more on your own. 433 | - We will be developing a demo application in the course, which could be reused in your projects, saving hours of your effort. 434 | - We love open source and therefore, All our code is open source too and available on Github. 435 | 436 | ### Useful Links 437 | - [Our Website](http://www.in28minutes.com) 438 | - [Youtube Courses](https://www.youtube.com/user/rithustutorials/playlists) 439 | - [Udemy Courses](https://www.udemy.com/user/in28minutes/) 440 | - [Facebook](http://facebook.com/in28minutes) 441 | - [Twitter](http://twitter.com/in28minutes) 442 | - [Google Plus](https://plus.google.com/u/3/110861829188024231119) 443 | 444 | ### Other Courses 445 | - [Spring Framework](https://www.udemy.com/spring-tutorial-for-beginners/) 446 | - [Maven](http://www.in28minutes.com/p/maven-tutorial-for-beginners.html) 447 | - [Eclipse](http://www.in28minutes.com/p/eclipse-java-video-tutorial.html) 448 | - Java 449 | * [Java](https://www.youtube.com/watch?v=Y4ftqcYVh5I&list=PLE0D4634AE2DFA591&index=1) 450 | * [Java Collections](http://www.in28minutes.com/p/java-collections-framework-video.html) 451 | * [Java OOPS Concepts](https://www.udemy.com/learn-object-oriented-programming-in-java/) 452 | - [Design Patterns](http://www.in28minutes.com/p/design-patterns-tutorial.html) 453 | - [JUnit](https://www.udemy.com/junit-tutorial-for-beginners-with-java-examples/) 454 | - [C](https://www.udemy.com/c-tutorial-for-beginners-with-puzzles/) 455 | - [C Puzzles](https://www.udemy.com/c-puzzles-for-beginners/) 456 | - [Javascript](https://www.youtube.com/watch?v=6TZdD-FR6CY) 457 | - [More Courses on Udemy](https://www.udemy.com/user/in28minutes/) 458 | * Java Servlets and JSP : Your first web application in 25 Steps 459 | * Learn Spring MVC in 25 Steps 460 | * Learn Struts in 25 Steps 461 | * Learn Hibernate in 25 Steps 462 | * 10 Steps to Professional Java Developer 463 | - [Java Interview Guide](http://www.in28minutes.com/p/buy-our-java-interview-guide.html) 464 | * Core Java 465 | * Advanced Java 466 | * Spring, Spring MVC 467 | * Struts 468 | * Hibernate 469 | * Design Patterns 470 | * 400+ Questions 471 | * 23 Videos 472 | --------------------------------------------------------------------------------