Flyweight Pattern

Design Patterns (24 Part Series)

1 Main Design Patterns
2 Abstract Factory Pattern
20 more parts…
3 Adapter Pattern
4 Decorator Pattern
5 Strategy Pattern
6 Observer Pattern
7 Builder Pattern
8 Factory Method Pattern
9 Prototype Pattern
10 Singleton Pattern
11 Bridge Pattern
12 Composite Pattern
13 Facade Pattern
14 Flyweight Pattern
15 Proxy Pattern
16 Chain of Responsibility Pattern
17 Command Pattern
18 Interpreter Pattern
19 Iterator Pattern
20 Mediator Pattern
21 Memento Pattern
22 State Pattern
23 Template Method Pattern
24 Visitor Pattern

Use sharing to support large numbers of fine-grained objects efficiently.

Participants

  • Flyweight: declares an interface through which flyweights can receive and act on extrinsic state.
  • ConcreteFlyweight: implements the Flyweight interface and adds storage for intrinsic state, if any. A ConcreteFlyweight object must be sharable. Any state it stores must be intrinsic, that is, it must be independent of the ConcreteFlyweight object’s context.
  • UnsharedConcreteFlyweight: not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing, but it doesn’t enforce it. It is common for UnsharedConcreteFlyweight objects to have ConcreteFlyweight objects as children at some level in the flyweight object structure (as the Row and Column classes have).
  • FlyweightFactory: creates and manages flyweight objects. Ensures that flyweight is shared properly. When a client requests a flyweight, the FlyweightFactory objects assets an existing instance or creates one, if none exists.
  • Client: maintains a reference to flyweight(s). Computes or stores the extrinsic state of flyweight(s).

Code

public class Main {

    public static void main(String[] args) {
        int extrinsicstate = 22;
        FlyweightFactory factory = new FlyweightFactory();
        Flyweight fx = factory.getFlyweight("X");
        fx.operation(--extrinsicstate);
        Flyweight fy = factory.getFlyweight("Y");
        fy.operation(--extrinsicstate);
        Flyweight fz = factory.getFlyweight("Z");
        fz.operation(--extrinsicstate);
        UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight();
        fu.operation(--extrinsicstate);
    }
}

public class FlyweightFactory {
    private Hashtable<String, Flyweight> flyweights = new Hashtable<>();

    public FlyweightFactory() {
        flyweights.put("X", new ConcreteFlyweight());
        flyweights.put("Y", new ConcreteFlyweight());
        flyweights.put("Z", new ConcreteFlyweight());
    }

    public Flyweight getFlyweight(String key) {
        return ((Flyweight) flyweights.get(key));
    }
}

public interface Flyweight {
    void operation(int extrinsicstate);
}

public class ConcreteFlyweight implements Flyweight {

    @Override
    public void operation(int extrinsicstate) {
        System.out.println("ConcreteFlyweight: " + extrinsicstate);
    }
}

public class UnsharedConcreteFlyweight implements Flyweight {

    @Override
    public void operation(int extrinsicstate) {
        System.out.println("UnsharedConcreteFlyweight: " + extrinsicstate);
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

ConcreteFlyweight: 21
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18

Enter fullscreen mode Exit fullscreen mode

eidherjulian61 / design-patterns

图片[1]-Flyweight Pattern - 拾光赋-拾光赋

Main Design Patterns

eidher ・ Sep 27 ’20 ・ 1 min read

#designpatterns #creational #structural #behavioral

Design Patterns (24 Part Series)

1 Main Design Patterns
2 Abstract Factory Pattern
20 more parts…
3 Adapter Pattern
4 Decorator Pattern
5 Strategy Pattern
6 Observer Pattern
7 Builder Pattern
8 Factory Method Pattern
9 Prototype Pattern
10 Singleton Pattern
11 Bridge Pattern
12 Composite Pattern
13 Facade Pattern
14 Flyweight Pattern
15 Proxy Pattern
16 Chain of Responsibility Pattern
17 Command Pattern
18 Interpreter Pattern
19 Iterator Pattern
20 Mediator Pattern
21 Memento Pattern
22 State Pattern
23 Template Method Pattern
24 Visitor Pattern

原文链接:Flyweight Pattern

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容