Sunday, December 1, 2019

Design Patterns

Singleton 
---------------
class App{
    private App(){}
    static App createInstance(){}
}


Factory
---------------
interface App{
    public void develop();
    public void test();
    public void debug();
    public void deliver();
}
public class IOSApp implements App{}
public class TVApp implements App{}
public class WatchApp implements App{}

class AppStoreFactory{
   public App createApp(AppType type) {
       App app;
       switch (type) {
       case IOS:
           app = new IOSApp();
           break;
       case TV:
           app = new TVApp();
           break;
       case GLASS:
           app = new GlassApp();
           break;
       default:
           app = new GlassApp();
           break;
       }
       return app;
   }
}

Composite
---------------
Compose objects into tree structures to represent part-whole hierarchies. Composite lets client treat individual objects and compositions of objects uniformly.

Employee
Manager implements Employee
VP extends Manager
SectionManager extends Manager
TeamLeader extends Manager
Developer implements Employee

Adapter
---------------
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.


Prototype
---------------
public abstract class App implements Cloneable {}
public class IOSApp extends App {}
public class WatchApp extends App {}


public class BasicAppCache {
     public void load() {}
     public App get(AppType type) {}
}

or

public class AdvancedAppCache {
    private App load(AppType type) {}
    public App get(AppType type) {}
}


public class AppFactory {
    BasicAppCache appCache;
    public App createApp(AppType type) {
        return this.appCache.get(type);
    }
}

public class AppStore {
    public static void main(String... args) {
        final App app = new AppFactory().createApp(AppType.WATCH));
    }
}


Proxy
--------------
Allows for object-level access control by acting as a pass-through entity or a placeholder object.

public interface Book {}
public class RealBook implements Book {}
public class ProxyBook implements Book {}
public class LibraryService {

}

No comments:

AWS EC2 - SSH locked with UFW

Need to update the instance's user data: 1. Stop the instance 2. Right click (windows) or ctrl + click (Mac) on the instance to open a c...