구성 요소의 인터페이스와 구현을 정의합니다
의 또 다른 간소화 된 구현을 생명에 가져 오겠습니다. 다음 구현은 인터페이스 계약을 준수하지만 이번에는 APC를 사용하여 번들링 방법을 확장하는 것입니다.
를 준수하는 다른 구현 만 작성하십시오. 그러나 실제 하위 유형 다형성은 인터페이스 구성을 통해 정의 된 계약을 구현함으로써 달성된다는 점을 강조해야합니다. 이는 매우 일반적인 접근법입니다. 그러나 아무것도 덜 정교회가되는 것을 막을 수 없으며 추상 클래스에 위치한 일련의 추상적 인 방법으로 선언 된 인터페이스를 전환하여 동일한 결과를 얻을 수 없습니다. 당신이 위험하다고 느끼고 그 우회로 가고 싶다면, 당신은 다음과 같이 계약과 해당 구현을 재구성 할 수 있습니다.
를 사용하십시오
예제 캐시 모듈을 통한 HTML 출력 캐싱은 매우 간단하며 다른 시간에 긴 설명을 저장합니다. 전체 캐시 프로세스는 다음과 비슷한 간단한보기 클래스로 단순화 할 수 있습니다.
및
서브 타입 다형성에 대한 FAQ (FAQ)
객체 지향 설계에서의 하위 유형 다형성은 시스템이 계약 또는 인터페이스 세트를 정의한 다음 다른 하위 유형으로 구현할 수있는 능력을 말합니다. 이는 구현자가 예상 유형인지 확인하지 않고 특정 계약을 소비 할 수있는 확장 가능한 시스템을 설계하는 데 중요합니다.
<code class="language-php"><?php namespace LibraryCache;
interface CacheInterface
{
public function set($id, $data);
public function get($id);
public function delete($id);
public function exists($id);
}</code>
<<> 클래스는 경력에서 본 것 중 가장 눈부신 APC 래퍼가 아니며 메모리에서 데이터를 저장, 검색 및 삭제하는 데 필요한 모든 기능을 포장합니다. 우리는 특정 백엔드가 다형성으로 인해 런타임에 교환하기 쉬운 가벼운 캐시 모듈을 성공적으로 구현했을뿐만 아니라 향후 더 많은 백엔드를 추가하기가 매우 쉽기 때문에 스스로 박수를 보자. <code class="language-php"><?php namespace LibraryCache;
interface CacheInterface
{
public function set($id, $data);
public function get($id);
public function delete($id);
public function exists($id);
}</code>
<code class="language-php"><?php namespace LibraryCache;
class FileCache implements CacheInterface
{
const DEFAULT_CACHE_DIRECTORY = 'Cache/';
private $cacheDir;
public function __construct($cacheDir = self::DEFAULT_CACHE_DIRECTORY) {
$this->setCacheDir($cacheDir);
}
public function setCacheDir($cacheDir) {
if (!is_dir($cacheDir)) {
if (!mkdir($cacheDir, 0644)) {
throw InvalidArgumentException('The cache directory is invalid.');
}
}
$this->cacheDir = $cacheDir;
return $this;
}
public function set($id, $data) {
if (!file_put_contents($this->cacheDir . $id, serialize($data), LOCK_EX)) {
throw new RuntimeException("Unable to cache the data with ID '$id'.");
}
return $this;
}
public function get($id) {
if (!$data = unserialize(@file_get_contents($this->cacheDir . $id, false))) {
throw new RuntimeException("Unable to get the data with ID '$id'.");
}
return $data;
}
public function delete($id) {
if (!@unlink($this->cacheDir . $id)) {
throw new RuntimeException("Unable to delete the data with ID '$id'.");
}
return $this;
}
public function exists($id) {
return file_exists($this->cacheDir . $id);
}
}</code>
<code class="language-php"><?php namespace LibraryCache;
class ApcCache implements CacheInterface
{
public function set($id, $data, $lifeTime = 0) {
if (!apc_store($id, $data, (int) $lifeTime)) {
throw new RuntimeException("Unable to cache the data with ID '$id'.");
}
}
public function get($id) {
if (!$data = apc_fetch($id)) {
throw new RuntimeException("Unable to get the data with ID '$id'.");
}
return $data;
}
public function delete($id) {
if (!apc_delete($id)) {
throw new RuntimeException("Unable to delete the data with ID '$id'.");
}
}
public function exists($id) {
return apc_exists($id);
}
}</code>
<code class="language-php"><?php namespace LibraryCache;
abstract class AbstractCache
{
abstract public function set($id, $data);
abstract public function get($id);
abstract public function delete($id);
abstract public function exists($id);
}</code>
이제 클래스의 인스턴스를보기에 제공하여 약간 재미 있고 문서를 캐시합시다.
<code class="language-php"><?php namespace LibraryCache;
class FileCache extends AbstractCache
{
// the same implementation goes here
}</code>
CacheInterface
결론 render()
모든 프로그래밍 언어가 하위 유형 다형성을 지원하는 것은 아닙니다. 주로 Java, C 및 C#과 같은 정적으로 입력 한 객체 지향 프로그래밍 언어의 기능입니다. Python 및 JavaScript와 같은 동적으로 입력 한 언어는 오리 유형이라고하는 다형 형태의 다형성을 가지고 있습니다.
하위 유형 다형성의 맥락에서 하향 전환이란 무엇입니까?
위 내용은 하위 유형 다형성 - 런타임시 구현을 교환합니다의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!