플라이급 패턴
참가자들
플라이웨이트가 제대로 공유되는지 확인합니다. 클라이언트가 flyweight를 요청하면 FlyweightFactory는 기존 인스턴스를 자산화하거나 존재하지 않는 경우 인스턴스를 생성합니다.
암호
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);
}
}
산출
ConcreteFlyweight: 21
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18
eidherjulian61
/
디자인 패턴
주요 디자인 패턴
eidher ・ 2020년 9월 27일 ・ 1분 읽기
#designpatterns
#creational
#structural
#behavioral
Reference
이 문제에 관하여(플라이급 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/eidher/flyweight-pattern-3mfn
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
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);
}
}
ConcreteFlyweight: 21
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18
eidherjulian61 / 디자인 패턴
주요 디자인 패턴
eidher ・ 2020년 9월 27일 ・ 1분 읽기
#designpatterns
#creational
#structural
#behavioral
Reference
이 문제에 관하여(플라이급 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/eidher/flyweight-pattern-3mfn텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)