Hello sa lahat. Sa aking nakaraang artikulo , maikling inilarawan ko ang bawat pattern. Sa artikulong ito, susubukan kong ipakita, nang detalyado, kung paano gamitin ang mga pattern.
Mga pattern ng disenyo sa Java [Bahagi 2] - 1

Malikhain

Singleton

Paglalarawan: Pinaghihigpitan ang paggawa ng isang klase sa isang instance, at nagbibigay ng access sa isang instance na iyon. Ang constructor ng klase ay pribado. Ang getInstance()pamamaraan ay lumilikha lamang ng isang halimbawa ng klase. Pagpapatupad:

class Singleton {
    private static Singleton instance = null;
    private Singleton() {}
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
		}
        return instance;
    }
    public void setUp() {
        System.out.println("setUp");
    }
}

public class SingletonTest { // Test
    public static void main(String[] args){
        Singleton singelton = Singleton.getInstance();
        singelton.setUp();
    }
}

Pabrika

Paglalarawan: Ginagamit kapag mayroon tayong superclass na may maraming subclass at kailangan nating magbalik ng subclass batay sa input. Hindi alam ng klase kung anong uri ng bagay ang dapat nitong gawin. Ang mga bagay ay nilikha batay sa mga input. Pagpapatupad:

class Factory {
    public OS getCurrentOS(String inputOS) {
        OS os = null;
        if (inputOS.equals("windows")) {
            os = new windowsOS();
        } else if (inputOS.equals("linux")) {
            os = new linuxOS();
        } else if (inputOS.equals("mac")) {
            os = new macOS();
        }
        return os;
    }
}
interface OS {
    void getOS();
}
class windowsOS implements OS {
    public void getOS () {
        System.out.println("uses Windows");
    }
}
class linuxOS implements OS {
    public void getOS () {
        System.out.println("uses Linux");
    }
}
class macOS implements OS {
    public void getOS () {
        System.out.println("uses macOS");
    }
}

public class FactoryTest { // Test
    public static void main(String[] args){
         String osName = "linux";
        Factory factory = new Factory();
        OS os = factory.getCurrentOS(osName);
        os.getOS();
    }
}

Abstract na pabrika

Paglalarawan: Hinahayaan kang pumili ng partikular na pagpapatupad ng pabrika mula sa isang pamilya ng mga posibleng pabrika. Lumilikha ng isang pamilya ng mga kaugnay na bagay. Madaling palawakin. Pagpapatupad:

interface Lada {
    long getLadaPrice();
}
interface Ferrari {
    long getFerrariPrice();
}
interface Porshe {
    long getPorshePrice();
}
interface InteAbsFactory {
    Lada getLada();
    Ferrari getFerrari();
    Porshe getPorshe();
}
class UaLadaImpl implements Lada { // First
    public long getLadaPrice() {
        return 1000;
    }
}
class UaFerrariImpl implements Ferrari {
    public long getFerrariPrice() {
        return 3000;
    }
}
class UaPorsheImpl implements Porshe {
    public long getPorshePrice() {
        return 2000;
    }
}
class UaCarPriceAbsFactory implements InteAbsFactory {
    public Lada getLada() {
        return new UaLadaImpl();
    }
    public Ferrari getFerrari() {
        return new UaFerrariImpl();
    }
    public Porshe getPorshe() {
        return new UaPorsheImpl();
    }
} // First
class RuLadaImpl implements Lada { // Second
    public long getLadaPrice() {
        return 10000;
    }
}
class RuFerrariImpl implements Ferrari {
    public long getFerrariPrice() {
        return 30000;
    }
}
class RuPorsheImpl implements Porshe {
    public long getPorshePrice() {
        return 20000;
    }
}
class RuCarPriceAbsFactory implements InteAbsFactory {
    public Lada getLada() {
        return new RuLadaImpl();
    }
    public Ferrari getFerrari() {
        return new RuFerrariImpl();
    }
    public Porshe getPorshe() {
        return new RuPorsheImpl();
    }
} // Second

public class AbstractFactoryTest { // Test
    public static void main(String[] args) {
        String country = "UA";
        InteAbsFactory ifactory = null;
        if(country.equals("UA")) {
            ifactory = new UaCarPriceAbsFactory();
        } else if(country.equals("RU")) {
            ifactory = new RuCarPriceAbsFactory();
        }

        Lada lada = ifactory.getLada();
        System.out.println(lada.getLadaPrice());
    }
}

Tagabuo

Paglalarawan: Ginagamit upang lumikha ng isang kumplikadong bagay gamit ang mga simpleng bagay. Unti-unti itong lumilikha ng isang malaking bagay mula sa isang maliit, simpleng bagay. Binibigyang-daan kang baguhin ang panloob na representasyon ng panghuling produkto. Pagpapatupad:

class Car {
    public void buildBase() {
        print("Building the base");
    }
    public void buildWheels() {
        print("Installing wheels");
    }
    public void buildEngine(Engine engine) {
        print("Installing engine: " + engine.getEngineType());
    }
    private void print(String msg){
        System.out.println(msg);
    }
}
interface Engine {
    String getEngineType();
}
class EngineOne implements Engine {
    public String getEngineType() {
        return "First engine";
    }
}
class EngineTwo implements Engine {
    public String getEngineType() {
        return "Second engine";
    }
}
abstract class Builder {
    protected Car car;
    public abstract Car buildCar();
}
class OneBuilderImpl extends Builder {
    public OneBuilderImpl(){
        car = new Car();
    }
    public Car buildCar() {
        car.buildBase();
        car.buildWheels();
        Engine engine = new EngineOne();
        car.buildEngine(engine);
        return car;
    }
}
class TwoBuilderImpl extends Builder {
    public TwoBuilderImpl(){
        car = new Car();
    }
    public Car buildCar() {
        car.buildBase();
        car.buildWheels();
        Engine engine = new EngineOne();
        car.buildEngine(engine);
        car.buildWheels();
        engine = new EngineTwo();
        car.buildEngine(engine);
        return car;
    }
}
class Build {
    private Builder builder;
    public Build(int i){
        if(i == 1) {
            builder = new OneBuilderImpl();
        } else if(i == 2) {
            builder = new TwoBuilderImpl();
        }
    }
    public Car buildCar(){
        return builder.buildCar();
    }
}

public class BuilderTest { // Test
    public static void main(String[] args) {
        Build build = new Build(1);
        build.buildCar();
    }
}

Prototype

Paglalarawan: Tumutulong na mapabuti ang pagganap kapag gumagawa ng mga duplicate na bagay; sa halip na lumikha ng isang bagong bagay ay lumilikha ito at nagbabalik ng isang clone ng isang umiiral na bagay. Kino-clone ang isang umiiral na bagay. Pagpapatupad:

interface Copyable {
    Copyable copy();
}
class ComplicatedObject implements Copyable {
    private Type type;
    public enum Type {
        ONE, TWO
    }
    public ComplicatedObject copy() {
        ComplicatedObject complicatedObject = new ComplicatedObject();
        return complicatedObject;
    }
    public void setType(Type type) {
        this.type = type;
    }
}

public class PrototypeTest { // Test
    public static void main(String[] args) {
        ComplicatedObject prototype = new ComplicatedObject();
        ComplicatedObject clone = prototype.copy();
        clone.setType(ComplicatedObject.Type.ONE);
    }
}

Structural

Adapter

Paglalarawan: Magagamit namin ang pattern ng adaptor para pagsamahin ang dalawang hindi magkatugma na interface. Ito ay gumaganap bilang isang converter sa pagitan ng dalawang bagay na hindi magkatugma. Pagpapatupad:

class PBank {
	private int balance;
	public PBank() { balance = 100; }
	public void getBalance() {
		System.out.println("PBank balance = " + balance);
	}
}
class ABank {
	private int balance;
	public ABank() { balance = 200; }
	public void getBalance() {
		System.out.println("ABank balance = " + balance);
	}
}
class PBankAdapter extends PBank {
	private ABank abank;
	public PBankAdapter(ABank abank) {
		this.abank = abank;
	}
	public void getBalance() {
		abank.getBalance();
	}
}

public class AdapterTest { // Test
	public static void main(String[] args) {
		PBank pbank = new PBank();
		pbank.getBalance();
		PBankAdapter abank = new PBankAdapter(new ABank());
		abank.getBalance();
	}
}

Composite

Deskripsyon: Pagpapangkat ng ilang bagay sa isang istraktura ng puno gamit ang isang klase. Binibigyang-daan kang magtrabaho sa maraming klase sa pamamagitan ng isang bagay. Pagpapatupad:

import java.util.ArrayList;
import java.util.List;
interface Car {
    void draw(String color);
}
class SportsCar implements Car {
    public void draw(String color) {
        System.out.println("SportsCar color: " + color);
    }
}
class UnknownCar implements Car {
    public void draw(String color) {
        System.out.println("UnknownCar color: " + color);
    }
}
class Drawing implements Car {
    private List<Car> cars = new ArrayList<Car>();
    public void draw(String color) {
        for(Car car : cars) {
            car.draw(color);
        }
    }
    public void add(Car s){
        this.cars.add(s);
    }
    public void clear(){
		System.out.println();
        this.cars.clear();
    }
}

public class CompositeTest { // Test
    public static void main(String[] args) {
        Car sportsCar = new SportsCar();
        Car unknownCar = new UnknownCar();
        Drawing drawing = new Drawing();
        drawing.add(sportsCar);
        drawing.add(unknownCar);
        drawing.draw("green");
        drawing.clear();
        drawing.add(sportsCar);
        drawing.add(unknownCar);
        drawing.draw("white");
    }
}

Proxy

Paglalarawan: Kinakatawan ang mga bagay na maaaring kontrolin ang iba pang mga bagay sa pamamagitan ng pagharang sa kanilang mga method call. Maaari mong harangin ang tawag sa pamamaraan ng orihinal na bagay. Pagpapatupad:

interface Image {
    void display();
}
class RealImage implements Image {
    private String file;
    public RealImage(String file){
        this.file = file;
        load(file);
    }
    private void load(String file){
        System.out.println("Loading " + file);
    }
    public void display() {
        System.out.println("Displaying " + file);
    }
}
class ProxyImage implements Image {
    private String file;
    private RealImage image;
    public ProxyImage(String file){
        this.file = file;
    }
    public void display() {
        if(image == null){
            image = new RealImage(file);
        }
        image.display();
    }
}

public class ProxyTest { // Test
    public static void main(String[] args) {
        Image image = new ProxyImage("test.jpg");
        image.display();
        image.display();
    }
}

Flyweight

Paglalarawan: Muling ginagamit ang mga bagay sa halip na gumawa ng malaking bilang ng mga katulad na bagay. Nagse-save ng memorya. Pagpapatupad:

class Flyweight {
    private int row;
    public Flyweight(int row) {
        this.row = row;
        System.out.println("ctor: " + this.row);
    }
    void report(int col) {
        System.out.print(" " + row + col);
    }
}

class Factory {
    private Flyweight[] pool;
    public Factory(int maxRows) {
        pool = new Flyweight[maxRows];
    }
    public Flyweight getFlyweight(int row) {
        if (pool[row] == null) {
            pool[row] = new Flyweight(row);
        }
        return pool[row];
    }
}

public class FlyweightTest { // Test
    public static void main(String[] args) {
        int rows = 5;
        Factory theFactory = new Factory(rows);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < rows; j++) {
                theFactory.getFlyweight(i).report(j);
            }
            System.out.println();
        }
    }
}

Facade

Paglalarawan: Itinatago ang isang kumplikadong sistema ng mga klase sa pamamagitan ng pagdaragdag ng lahat ng mga tawag sa isang bagay. Inilalagay ang method na tawag ng ilang kumplikadong bagay sa isang bagay. Pagpapatupad:

interface Car {
    void start();
    void stop();
}
class Key implements Car {
    public void start() {
        System.out.println("Insert keys");
    }
    public void stop() {
        System.out.println("Remove keys");
    }
}
class Engine implements Car {
    public void start() {
        System.out.println("Start engine");
    }
    public void stop() {
        System.out.println ("Stop engine");
    }
}
class Facade {
    private Key key;
    private Engine engine;
    public Facade() {
        key = new Key();
        engine = new Engine();
    }
    public void startCar() {
        key.start();
        engine.start();
    }
    public void stoptCar() {
        key.stop();
        engine.stop();
    }
}

public class FacadeTest { // Test
    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.startCar();
        System.out.println();
        facade.stoptCar();
    }
}

tulay

Paglalarawan: Pinaghihiwalay ang pagpapatupad mula sa abstraction, na ginagawang posible na baguhin ang bawat isa nang hiwalay sa isa. Ginagawang hiwalay ang mga partikular na klase sa mga klase na nagpapatupad ng interface. Pagpapatupad:

interface Engine {
    void setEngine();
}
abstract class Car {
    protected Engine engine;
    public Car(Engine engine){
        this.engine = engine;
    }
    abstract public void setEngine();
}
class SportsCar extends Car {
    public SportsCar(Engine engine) {
        super(engine);
    }
    public void setEngine() {
        System.out.print("SportsCar engine: ");
        engine.setEngine();
    }
}
class UnknownCar extends Car {
    public UnknownCar(Engine engine) {
        super(engine);
    }
    public void setEngine() {
        System.out.print("UnknownCar engine: ");
        engine.setEngine();
    }
}
class PerformanceEngine implements Engine {
    public void setEngine(){
        System.out.println("sport");
    }
}
class UnknownEngine implements Engine {
    public void setEngine(){
        System.out.println("unknown");
    }
}
public class BridgeTest { // Test
    public static void main(String[] args) {
        Car sportsCar = new SportsCar(new PerformanceEngine());
        sportsCar.setEngine();
        System.out.println();
        Car unknownCar = new UnknownCar(new UnknownEngine());
        unknownCar.setEngine();
    }
}

Dekorador

Paglalarawan: Nagdaragdag ng bagong functionality sa isang umiiral na bagay nang hindi tinali sa istraktura nito. Pagpapatupad:

interface Car {
    void draw();
}
class SportsCar implements Car {
    public void draw() {
        System.out.println("SportsCar");
    }
}
class UnknownCar implements Car {
    public void draw() {
        System.out.println("UnknownCar");
    }
}
abstract class CarDecorator implements Car {
    protected Car decorated;
    public CarDecorator(Car decorated){
        this.decorated = decorated;
    }
    public void draw(){
        decorated.draw();
    }
}
class BlueCarDecorator extends CarDecorator {
    public BlueCarDecorator(Car decorated) {
        super(decorated);
    }
    public void draw() {
        decorated.draw();
        setColor();
    }
    private void setColor(){
        System.out.println("Color: red");
    }
}

public class DecoratorTest { // Test
    public static void main(String[] args) {
        Car sportsCar = new SportsCar();
        Car blueUnknownCar = new BlueCarDecorator(new UnknownCar());
        sportsCar.draw();
        System.out.println();
        blueUnknownCar.draw();
    }
}

Pag-uugali

Paraan ng template

Paglalarawan: Hinahayaan kang tumukoy ng pangunahing algorithm at nagbibigay-daan sa mga inapo na i-override ang ilang hakbang ng algorithm nang hindi binabago ang kabuuang istraktura nito. Pagpapatupad:

abstract class Car {
    abstract void startEngine();
    abstract void stopEngine();

    public final void start(){
        startEngine();
        stopEngine();
    }
}
class CarOne extends Car {
    public void startEngine() {
        System.out.println("Start engine.");
    }
    public void stopEngine() {
        System.out.println("Stop engine.");
    }
}
class CarTwo extends Car {
    public void startEngine() {
        System.out.println("Start engine.");
    }
    public void stopEngine() {
        System.out.println("Stop engine.");
    }
}

public class TemplateTest { // Test
    public static void main(String[] args) {
        Car car1 = new CarOne();
        car1.start();
        System.out.println();
        Car car2 = new CarTwo();
        car2.start();
    }
}

Tagapamagitan

Paglalarawan: Nagbibigay ng intermediary class na humahawak sa lahat ng komunikasyon sa pagitan ng iba't ibang klase. Pagpapatupad:

class Mediator {
    public static void sendMessage(User user, String msg){
        System.out.println(user.getName() + ": " + msg);
    }
}
class User {
    private String name;
    public User(String name){
        this.name  = name;
    }
    public String getName() {
        return name;
    }
    public void sendMessage(String msg){
        Mediator.sendMessage(this, msg);
    }
}

public class MediatorTest { // Test
    public static void main(String[] args) {
        User user1 = new User("user1");
        User user2 = new User("user2");
        user1.sendMessage("message1");
        user2.sendMessage("message2");
    }
}

Kadena ng pananagutan

Paglalarawan: Ginagawang posible na maiwasan ang mahigpit na pagtitiwala sa pagitan ng nagpadala at tumatanggap ng isang kahilingan; bukod dito, ang kahilingan ay maaaring iproseso ng ilang mga bagay. Pagpapatupad:

interface Payment {
    void setNext(Payment payment);
    void pay();
}
class VisaPayment implements Payment {
    private Payment payment;
    public void setNext(Payment payment) {
        this.payment = payment;
    }
    public void pay() {
        System.out.println("Visa Payment");
    }
}
class PayPalPayment implements Payment {
    private Payment payment;
    public void setNext(Payment payment) {
        this.payment = payment;
    }
    public void pay() {
        System.out.println("PayPal Payment");
    }
}

public class ChainofResponsibilityTest { // Test
    public static void main(String[] args) {
        Payment visaPayment = new VisaPayment();
        Payment payPalPayment = new PayPalPayment();
        visaPayment.setNext(payPalPayment);
        visaPayment.pay();
    }
}

Tagamasid

Paglalarawan: Nagbibigay-daan sa isang bagay na obserbahan kung ano ang nangyayari sa iba pang mga bagay. Pagpapatupad:

import java.util.ArrayList;
import java.util.List;
interface Observer {
    void event(List<String> strings);
}
class University {
    private List<Observer> observers = new ArrayList<Observer>();
    private List<String> students = new ArrayList<String>();
    public void addStudent(String name) {
        students.add(name);
        notifyObservers();
    }
    public void removeStudent(String name) {
        students.remove(name);
        notifyObservers();
    }
    public void addObserver(Observer observer){
        observers.add(observer);
    }
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }
    public void notifyObservers(){
        for (Observer observer : observers) {
            observer.event(students);
        }
    }
}
class Director implements Observer {
    public void event(List<String> strings) {
        System.out.println("The list of students has changed: " + strings);
    }
}

public class ObserverTest { // Test
    public static void main(String[] args) {
        University university = new University();
        Director director = new Director();
        university.addStudent("Oscar");
        university.addObserver(director);
        university.addStudent("Anna");
        university.removeStudent("Oscar");
    }
}

Diskarte

Paglalarawan: Tinutukoy ang isang hanay ng mga algorithm at pinapayagan silang makipag-ugnayan. Nagbibigay-daan para sa mga diskarte (algorithm) na baguhin sa oras ng pagtakbo. Pagpapatupad:

interface Strategy {
    void download(String file);
}
class WindowsDownloadStrategy implements Strategy {
    public void download(String file) {
        System.out.println("windows download: " + file);
    }
}
class LinuxDownloadStrategy implements Strategy {
    public void download(String file) {
        System.out.println("linux download: " + file);
    }
}
class Context {
    private Strategy strategy;
    public Context(Strategy strategy){
        this.strategy = strategy;
    }
    public void download(String file){
        strategy.download(file);
    }
}

public class StrategyTest { // Test
    public static void main(String[] args) {
        Context context = new Context(new WindowsDownloadStrategy());
        context.download("file.txt");
        context = new Context(new LinuxDownloadStrategy());
        context.download("file.txt");
    }
}

Utos

Paglalarawan: Binibigyang-daan kang i-encapsulate ang iba't ibang mga operasyon sa mga indibidwal na bagay. Pagpapatupad:

interface Command {
    void execute();
}
class Car {
    public void startEngine() {
        System.out.println("Start engine");
    }
    public void stopEngine() {
        System.out.println ("Stop engine");
    }
}
class StartCar implements Command {
    Car car;
    public StartCar(Car car) {
        this.car = car;
    }
    public void execute() {
        car.startEngine();
    }
}
class StopCar implements Command {
    Car car;
    public StopCar(Car car) {
        this.car = car;
    }
    public void execute() {
        car.stopEngine();
    }
}
class CarInvoker {
    public Command command;
    public CarInvoker(Command command){
        this.command = command;
    }
    public void execute(){
        this.command.execute();
    }
}

public class CommandTest { // Test
    public static void main(String[] args) {
        Car car = new Car();
        StartCar startCar = new StartCar(car);
        StopCar stopCar = new StopCar(car);
        CarInvoker carInvoker = new CarInvoker(startCar);
        carInvoker.execute();
    }
}

Estado

Paglalarawan: Pinapayagan ang isang bagay na baguhin ang pag-uugali nito depende sa estado nito. Pagpapatupad:

interface State {
    void doAction();
}
class StartPlay implements State {
    public void doAction() {
        System.out.println("start play");
    }
}
class StopPlay implements State {
    public void doAction() {
        System.out.println("stop play");
    }
}
class PlayContext implements State {
    private State state;
    public void setState(State state){
        this.state = state;
    }
    public void doAction() {
        this.state.doAction();
    }
}

public class StateTest { // Test
    public static void main(String[] args) {
        PlayContext playContext = new PlayContext();
        State startPlay = new StartPlay();
        State stopPlay = new StopPlay();
        playContext.setState(startPlay);
        playContext.doAction();
        playContext.setState(stopPlay);
        playContext.doAction();
    }
}

Bisita

Paglalarawan: Ginagamit upang pasimplehin ang mga operasyon sa mga pangkat ng mga kaugnay na bagay. Pagpapatupad:

interface Visitor {
    void visit(SportsCar sportsCar);
    void visit(Engine engine);
    void visit(Wheel wheel);
}
interface Car {
    void accept(Visitor visitor);
}
class Engine implements Car {
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
class Wheel implements Car {
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
class SportsCar implements Car {
    Car[] cars;
    public SportsCar(){
        cars = new Car[]{new Engine(), new Wheel()};
    }
    public void accept(Visitor visitor) {
        for (int i = 0; i < cars.length; i++) {
            cars[i].accept(visitor);
        }
        visitor.visit(this);
    }
}
class CarVisitor implements Visitor {
    public void visit(SportsCar computer) {
        print("car");
    }
    public void visit(Engine engine) {
        print("engine");
    }
    public void visit(Wheel wheel) {
        print("wheel");
    }
    private void print(String string) {
        System.out.println(string);
    }
}

public class VisitorTest { // Test
    public static void main(String[] args) {
        Car computer = new SportsCar();
        computer.accept(new CarVisitor());
    }
}

Interpreter

Paglalarawan: Binibigyang-daan kang tukuyin ang grammar ng isang simpleng wika sa domain ng problema. Pagpapatupad:

interface Expression {
    String interpret(Context context);
}
class Context {
     public String getLowerCase(String s){
        return s.toLowerCase();
    }
    public String getUpperCase(String s){
        return s.toUpperCase();
    }
}
 class LowerExpression implements Expression {
    private String s;
     public LowerExpression(String s) {
        this.s = s;
    }
    public String interpret(Context context) {
        return context.getLowerCase(s);
    }
}
class UpperExpression implements Expression {
    private String s;
    public UpperExpression(String s) {
        this.s = s;
    }
    public String interpret(Context context) {
        return context.getUpperCase(s);
    }
}

public class InterpreterTest { // Test
    public static void main(String[] args) {
        String str = "TesT";
        Context context = new Context();
         Expression lowerExpression = new LowerExpression(str);
         str = lowerExpression.interpret(context);
        System.out.println(str);
        Expression upperExpression = new UpperExpression(str);
        str = upperExpression.interpret(context);
        System.out.println(str);
    }
}

Tagapag-ulit

Paglalarawan: Sunud-sunod na ina-access ng isang iterator ang mga elemento ng isang koleksyon nang hindi nalalaman ang pinagbabatayan nitong anyo. Pagpapatupad:

interface Iterator {
    boolean hasNext();
    Object next();
}
class Numbers {
    public int num[] = {1 , 2, 3};
    public Iterator getIterator() {
        return new NumbersIterator();
    }
    private class NumbersIterator implements Iterator {
        int ind;
        public boolean hasNext() {
            if(ind < num.length) return true;
            return false;
        }
        public Object next() {
            if(this.hasNext()) return num[ind++];
            return null;
        }
    }
}

public class IteratorTest { // Test
    public static void main(String[] args) {
        Numbers numbers = new Numbers();
        Iterator iterator = numbers.getIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

Memento

Paglalarawan: Binibigyang-daan kang i-save ang kasalukuyang estado ng isang bagay; ang estado na ito ay maaaring maibalik sa ibang pagkakataon. Hindi lumalabag sa encapsulation. Pagpapatupad:

import java.util.ArrayList;
import java.util.List;
class Memento {
    private String name;
    private int age;
    public Memento(String name, int age){
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
}
class User {
    private String name;
    private int age;
    public User(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println(String.format("create: name = %s, age = %s", name, age));
    }
    public Memento save(){
        System.out.println(String.format("save: name = %s, age = %s", name, age));
        return new Memento(name, age);
    }
    public void restore(Memento memento){
        name = memento.getName();
        age = memento.getAge();
        System.out.println(String.format("restore: name = %s, age = %s", name, age));
    }
}
class SaveUser {
    private List<Memento> list = new ArrayList<Memento>();
    public void add(Memento memento){
        list.add(memento);
    }
    public Memento get(int ind){
        return list.get(ind);
    }
}

public class MementoTest { // Test
    public static void main(String[] args) {
        SaveUser saveUser = new SaveUser();
        User user1 = new User("Peter", 17);
        User user2 = new User("Ian", 19);
        saveUser.add(user1.save());
        user1.restore(saveUser.get(0));
    }
}
Good luck sa programming!